实体框架+存储库+工作单位 [英] Entity Framework + Repository + Unit of Work
问题描述
http://tdryan.blogspot.com/2011/03 /another-entity-framework-4-repository_15.html 和 http://blogs.msdn.com/b/adonet/archive/2009/06/16/using- repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx )
我正在使用第一个(第1部分,第2部分和第3部分)。他们非常相似。
在这种情况下,我是新手。我在这两个职位之间混乱。我已经创建了一切,但我不知道如何开始使用上下文并添加一些实体。我发布了第二个链接,因为发布了一种方法来实现它。 ObjectContext
源自 IUnitOfWork
,所以我很困惑,选择这两个中哪一个更好地使用。 / p>
你的问题不是愚蠢!开始使用 UnitOfWork
和存储库
模式需要一些时间。
首先,要获得一些terminoloy权利。一个
UnitOfWork
封装一组动作并将它们组合在一起。因此,您可以在一个逻辑组中创建客户,产品和相应的订单。 A 存储库
给你一个访问实体的点,大部分时间都有一些特定的检索数据的方法。
多个存储库可以在一个单独的事务中使用,这就是为什么他们共享一个 UnitOfWork
。
在您发布的示例中,T4文件创建一些存储库
接口。一个是只读方式来选择实体,但另一个存储库
具有方法,如添加
和删除
。
所以如果你想添加一个实体,你需要先构建一个 UnitOfWork
然后为您使用的实体类型( CustomerRepository
或实例化
)。然后,您可以使用 Repository
例如ProductRepository 添加
方法将实体添加到资源库
。当您完成使用您的存储库后,您将调用 UnitOfWork.Commit()
将您的更改保存到数据库。
IUnitOfWork unitOfWork = new EFUnitOfWork();
IRepository< Customer> customerRepository = new CustomerEFRepository(unitOfWork);
客户c = new Customer();
// init客户
customerRepository.Add(c);
unitOfWork.Commit();
在您发布的示例中,使用了具有StructureMap的依赖注入。这是一个完整的其他主题,但这意味着您不直接构建 UnitOfWork
和 Repository
使用您设置的某些配置注入到您的代码中。
I'm thinking about starting a new project using EF 4 and going through some articles, I found some articles about EF with repository pattern and unit of work
( http://tdryan.blogspot.com/2011/03/another-entity-framework-4-repository_15.html and http://blogs.msdn.com/b/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx)
I'm using the first one (part1, part2 and part3). They are very similar.
I'm a newbie in this scenario. I'm confusing between these two posts. I've create everything but I have no idea how can I start using the context and add some entities to it. I posted the second link because posted a way to implement it. The ObjectContext
is derived from IUnitOfWork
, so I'm confused to chose which of these two is better to use.
Your question is not stupid! Getting started with UnitOfWork
and the Repository
patterns takes some time.
First, to get some terminoloy right. A UnitOfWork
encapsulates a set of actions and groups them together. So you can for example create a customer, a product and a corresponding order in one logical group.
A Repository
gives you a single point of access to entities and most of the time has some specific methods for retrieving data.
Multiple repositories can be used in one single transaction, that's why they share a UnitOfWork
.
In the example you posted, the T4 files create some Repository
interfaces. One is a readonly with methods to select entities but the other Repository
has methods like Add
and Delete
.
So if you want to add an entity, you need to first construct a UnitOfWork
and then instantiate a Repository
for the entity type your working with (CustomerRepository
or ProductRepository
for example). You can then use the Add
method to add entities to a Repository
. When you're done working with your repositories you would call UnitOfWork.Commit()
to save your changes to the database.
IUnitOfWork unitOfWork = new EFUnitOfWork();
IRepository<Customer> customerRepository = new CustomerEFRepository(unitOfWork);
Customer c = new Customer();
// init customer
customerRepository.Add(c);
unitOfWork.Commit();
In the example you posted, Dependency Injection with StructureMap is used. This is a whole other topic, but it means that you don't construct the UnitOfWork
and Repository
directly but that they are 'injected' into your code using some configuration that you've setup.
这篇关于实体框架+存储库+工作单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!