【发布时间】:2015-07-16 21:19:25
【问题描述】:
我想在我的应用程序中实现简单的 IGenericRepository 和 IUnitOfWork 接口,但我不确定最好的方法是什么。
据我所知,UnitOfWork 应该用于写入,而 Repository 应该用于读取。我遇到过一种架构,我真的很喜欢它,但我只找到了接口而不是实现,我不知道我应该如何实现这些。
public interface IGenericRepository : IDisposable
{
IUnitOfWork CreateUnitOfWork();
T FirstOrDefault<T>(Expression<Func<T, bool>> predicate) where T : class, IBaseEntity;
IQueryable<T> Get<T>(Expression<Func<T, bool>> predicate = null, Func<IQueryable<T>, IOrderedQueryable<T>> sorter = null, params string[] includeProperties) where T : class, IBaseEntity;
IQueryable<T> GetAll<T>() where T : class, IBaseEntity;
IDbContext GetDbContext();
}
public interface IUnitOfWork : IDisposable
{
int Commit();
bool Delete<T>(T entity) where T : class, IBaseEntity;
int DeleteItems<T>(IList<T> entities) where T : class, IBaseEntity;
bool Insert<T>(T entity) where T : class, IBaseEntity;
int InsertItems<T>(IList<T> entities) where T : class, IBaseEntity;
bool Update<T>(T entity) where T : class, IBaseEntity;
int UpdateItems<T>(IList<T> entities) where T : class, IBaseEntity;
}
我不确定这些应该如何工作。我应该在存储库中使用 IDbContextFactory 在 Repository 和 UnitOfWork 之间共享 DbContext 还是它们应该有单独的 DbContexts?如果我实现 UnitOfWork 用于写入和 Repository 用于读取,是否应该有 UnitOfWorks DbContext 用于写入和 Repositorys DbContext 用于读取,或者它们应该共享相同的 DbContext?
我非常感谢 DbContext 和 UnitOfWork/Repository 应该如何工作的很好的解释。
这些将在服务中以这种方式实现:
public CustomerService(IGenericRepository repository)
{
this.repository = repository;
this.context = this.repository.GetDbContext();
}
public void UpdateCustomer(Customer customer)
{
var uow = this.repository.CreateUnitOfWork();
uow.AddForSave(customer);
uow.Commit();
}
public List<Customer> GetAll()
{
return this.repository.GetAll<Customer>();
}
任何关于 DbContext 和 UoW/Repository 关系的帮助、解释或类似于此实现的优秀教程都会有所帮助。
问候。
【问题讨论】:
-
UnitOfWork should be used for writing, while Repository should be used for reading我没听说过 -
不应该作为“必须”,但建议用于许多实体更新(可以使用存储库更新单个实体)。虽然有很多很好的模式可以同时使用存储库,但我想坚持使用这个。
-
@Disappointed 为您提供了一个很好的链接。但请注意,UoW 和存储库模式备受争议,有些人会说已弃用。当然在 EF/MVC 环境中,DbContext 和 DbSet 已经实现了大部分功能。
-
UoW 和存储库模式的(主要)目的是将数据访问实现与应用程序的其余部分分离。此处介绍了将这些模式与 EF 结合使用的非常好的观点:stackoverflow.com/a/21361903/1942895
-
这是一个很好的阅读链接。但我注意到那个答案是那边的少数派观点。
标签: c# asp.net-mvc entity-framework repository unit-of-work