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

编程入门 行业动态 更新时间:2024-10-27 22:32:08
本文介绍了实体框架+存储库+工作单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在考虑使用EF 4开始一个新项目,并通过一些文章,我发现一些关于EF的文章,具有存储库模式和工作单位

tdryan.blogspot/2011/03 /another-entity-framework-4-repository_15.html 和 blogs.msdn/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

( tdryan.blogspot/2011/03/another-entity-framework-4-repository_15.html and blogs.msdn/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.

更多推荐

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

本文发布于:2023-11-06 22:19:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   单位   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!