简体   繁体   English

实体框架+存储库+工作单元

[英]Entity Framework + Repository + Unit of Work

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 我正在考虑使用EF 4开始一个新项目并浏览一些文章,我发现了一些关于EF的文章和存储库模式以及工作单元

( 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 ) http://tdryan.blogspot.com/2011/03/another-entity-framework-4-repository_15.htmlhttp://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). 我正在使用第一个(第1部分,第2部分和第3部分)。 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. ObjectContext派生自IUnitOfWork ,所以我很难选择哪两个更好用。

Your question is not stupid! 你的问题不是傻瓜! Getting started with UnitOfWork and the Repository patterns takes some time. UnitOfWorkRepository模式入门需要一些时间。

First, to get some terminoloy right. 首先,要使一些终端合适。 A UnitOfWork encapsulates a set of actions and groups them together. UnitOfWork封装了一组操作并将它们组合在一起。 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. Repository为您提供单点访问实体,并且大多数时间都有一些特定的方法来检索数据。

Multiple repositories can be used in one single transaction, that's why they share a UnitOfWork . 多个存储库可以在一个事务中使用,这就是它们共享UnitOfWork的原因。

In the example you posted, the T4 files create some Repository interfaces. 在您发布的示例中,T4文件创建了一些Repository接口。 One is a readonly with methods to select entities but the other Repository has methods like Add and Delete . 一个是readonly,其中包含选择实体的方法,但另一个Repository具有AddDelete等方法。

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). 因此,如果要添加实体,则需要首先构造UnitOfWork ,然后为您使用的实体类型(例如CustomerRepositoryProductRepository )实例化Repository You can then use the Add method to add entities to a Repository . 然后,您可以使用Add方法将实体添加到Repository When you're done working with your repositories you would call UnitOfWork.Commit() to save your changes to the database. 当您使用完储存库后,您可以调用UnitOfWork.Commit()将更改保存到数据库中。

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. 在您发布的示例中,使用了使用StructureMap的依赖注入。 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. 这是一个完整的其他主题,但这意味着您不直接构造UnitOfWorkRepository ,而是使用您设置的某些配置将它们“注入”到您的代码中。

If your project is web make a handler that start a transaction on a request and end it at the last step. 如果您的项目是Web,则创建一个处理程序,在请求上启动事务并在最后一步结束它。

I think a much simpler example can be found here: https://github.com/ayende/CourseSampleApp Also you can find other samples on nhibernate which can satisfy your need. 我想在这里可以找到一个更简单的例子: https//github.com/ayende/CourseSampleApp您还可以在nhibernate上找到其他可满足您需求的样本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架,存储库模式,工作单元和测试 - Entity Framework, Repository Pattern, Unit of Work and Testing 使用Entity Framework创建简单的工作单元,无需存储库 - Creating a simple unit of work with Entity Framework and no repository 具有通用存储库和工作单元存储的实体框架显示旧数据 - Entity framework with generic repository and Unit of Work store showing old data 使用Entity Framework 5和存储库模式和工作单元过滤内部集合 - Filtering inner collection with Entity Framework 5 and Repository pattern and Unit of Work 使用Unity for Work of Unit / Repository模式创建Entity Framework对象 - Creating Entity Framework objects with Unity for Unit of Work/Repository pattern 使用实体框架使用存储库和单元工作模式正确处置? - Correct disposing using Repository and Unit Work patterns with Entity Framework? 如何使用Entity Framework + Repository + Working of Work启动应用程序? - How to start an application using Entity Framework + Repository + Unit of Work? 实体框架-信息库和工作单元-我这样做正确吗? - Entity Framework - Repository and Unit of Work - Am I doing this right? 实体框架CTP5存储库+ Winform的工作单元 - Entity Framework CTP5 Repository + Unit of Work for winform 单元测试实体框架存储库 - Unit Testing Entity Framework Repository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM