我正在尝试使用存储库模式和工作单元模式构建一个可重用的代码来访问Azure表存储。
我想要实现的是实例化一个工作单元,然后将其用于不同的存储库。
这样的事情:
MyUnitOfWork uow = new MyUnitOfWork();
uow.Users.Add(User user);
uow.Users.GetAllUsers();
uow.Users.FindUser(User user);
uow.Users.Delete(User user);
.......
uow.Products.Add(Product product);
uow.Products.Delete(Product product);
.......
uow.Invoices.Add(Invoice invoice);
.......
uow.Save();
我有一些问题需要了解如何使用TableServiceContext
来实现它这是我到目前为止所做的......
模特:
public class User : TableServiceEntity
{
public User(string partitionKey, string rowKey)
: base(partitionKey, rowKey)
{
}
public User()
: this(Guid.NewGuid().ToString(), String.Empty)
{
}
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
IRepository Interface:
public interface IRepository<T> where T : TableServiceEntity
{
IQueryable<T> Query { get; }
//bool CreateIfNotExist();
//bool DeleteIfExist();
//void AddEntity(T obj);
//void AddEntity(IEnumerable<T> objs);
//void AddOrUpdateEntity(T obj);
//void AddOrUpdateEntity(IEnumerable<T> objs);
//void DeleteEntity(T obj);
//void DeleteEntity(IEnumerable<T> objs);
}
界面工作单位:
public interface ITableUow
{
void Save();
IUserRepository Users { get; }
}
Interface User Repository:
public interface IUserRepository: IRepository<User>
{
IQueryable<User> GetUsers();
}
Azure表存储库:
public class AzureTableRepository<T> : IRepository<T> where T : TableServiceEntity
{
public AzureTableRepository(TableServiceContext context)
{
if (context != null)
{
TableContext = context;
}
}
protected TableServiceContext TableContext { get; set; }
public IQueryable<T> Query
{
get { throw new NotImplementedException(); }
}
}
用户存储库:
public class UserRepository : AzureTableRepository<User>, IUserRepository
{
public UserRepository(TableServiceContext context)
: base(context)
{
}
public IQueryable<User> GetUsers()
{
throw new NotImplementedException();
}
}
Azure表工作单元:
public class AzureTableUow: ITableUow
{
public void Save()
{
}
public IUserRepository Users
{
get { throw new NotImplementedException(); }
}
}