Entity framework 与工作单元相关的存储库
我上了10节POCO课。 我使用的是一个简单的存储库模式,工作单元带有IRespoitory接口和UnitOf工作类 将我所有的IRepository放在一个UnitOfWork实例中是否正确(正常) 即: 10 POCO类-IRepository的10个实例-只有一个UnitOfWork类包含所有10个存储库Entity framework 与工作单元相关的存储库,entity-framework,c#-4.0,repository-pattern,unit-of-work,Entity Framework,C# 4.0,Repository Pattern,Unit Of Work,我上了10节POCO课。 我使用的是一个简单的存储库模式,工作单元带有IRespoitory接口和UnitOf工作类 将我所有的IRepository放在一个UnitOfWork实例中是否正确(正常) 即: 10 POCO类-IRepository的10个实例-只有一个UnitOfWork类包含所有10个存储库 UnitOfWork { IRepository<Customer> CustomerRepository {get; set;} IRepository<Custom
UnitOfWork
{
IRepository<Customer> CustomerRepository {get; set;}
IRepository<Customer> CustomerRepository {get; set;}
IRepository<Customer> CustomerRepository {get; set;}
// the same for all others 7 POCo class
// ..other stff
}
工作单元
{
IRepository CustomerRepository{get;set;}
IRepository CustomerRepository{get;set;}
IRepository CustomerRepository{get;set;}
//其他所有7类POCo也一样
//…其他stff
}
它有点像EF DataContext
EntityFramework的数据上下文是一个工作单元,有点像存储库(或存储库的集合)
我更喜欢将这些东西分开,并使用依赖注入框架(如structuremap)
您可以向structuremap询问IRepository
,它将为您提供实例
从存储库中分离UoW
您可以有一个UoW类(使用
SubmitChanges
)和存储库(每个存储库都使用Add,Delete,
)等方法)这有点像EF DataContext
EntityFramework的数据上下文是一个工作单元,有点像存储库(或存储库的集合)
我更喜欢将这些东西分开,并使用依赖注入框架(如structuremap)
您可以向structuremap询问IRepository
,它将为您提供实例
从存储库中分离UoW
您可以有一个UoW类(使用
SubmitChanges
)和存储库(每个存储库使用Add,Delete,
)是的,您的方法是正确的(正常的),一个工作单元类/实例包含所有存储库(POCO类)
UoW为我带来了两件重要的事情/优势
public interface IUnitOfWork
{
IRepository<Customer> Customers { get; }
IRepository<Order> Orders { get; }
// the same for all others 8 POCO class
Task<int> SaveAsync();
}
是的,您的方法是正确的(正常的),一个工作单元类/实例包含所有存储库(POCO类) UoW为我带来了两件重要的事情/优势
public interface IUnitOfWork
{
IRepository<Customer> Customers { get; }
IRepository<Order> Orders { get; }
// the same for all others 8 POCO class
Task<int> SaveAsync();
}
_uow.Customers.Add(new Customer());
_uow.Ordres.Update(order);
_uow.SaveAsync();