Repository模式与实体框架 [英] Repository pattern with Entity framework

查看:343
本文介绍了Repository模式与实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

库模式用于抽象从特定的数据库和对象关系映射使用的技术(如EF)。所以,我可以很容易地取代(例如)我的实体框架映射使用LINQ在未来的SQL如果我决定这样做。

Repository pattern is used to abstract from particular database and object-relation-mapping technology(like EF) used. So I can easily replace (for example) my Entity framework mappings with Linq to SQL in the future if I decide to do so.

但是当我使用EF我有我的实体类的模型 - 这是他们从视觉图生成。如果我在我的仓库使用生成的实体类,然后再决定用别的东西来代替EF话,我会删除可视实体图,这也意味着要删除类吧?

But when I use EF I have my entity classes from the model - that is they are generated from that visual diagram. If I use this generated entity classes in my repository and then decide to replace EF by something else then I will delete that visual entity diagram and that means also to delete classes right ?

在一点上,我解决的是,我的仓库将取决于实体框架,这是因为它将使用EF生成的类数据访问层。

The point I'm addressing is that my repository will be dependent on Entity framework, that is on data access layer because it will use classes generated by EF.

我如何消除这种依赖关系?

How do I remove this dependency ?

另外请注意,我用的主要是因为其产生的一切从视觉图能力EF - 我只是设计图,并让它生成数据库对我所有的外键等等。我对此很感兴趣,并没有想也别想SQL命令。

Also note that I use EF primarily because of its ability to generate everything from that visual diagram - I just design the diagram and let it generate database for me with all the foreign keys etc. I like that very much and don't want to even think about SQL commands.

推荐答案

一个资源库始终是依赖于数据访问技术。这就是为什么人们正在使用的存储库的原因 - 包装数据访问依赖于单独的图层。如果你决定改变数据访问技术(恕我直言,这就像1%的机会,你这样做),你必须创建实现相同的接口,前者的新的仓库。

A repository is always dependent on data access technology. It is the reason why people are using repositories - to wrap data access dependency to separate layer. If you decide to change data access technology (imho it is like 1% chance that you do it) you will have to create new repositories implementing the same interfaces as former ones.

介绍repositoris会增加复杂性的一个新图层。存储库都有各自的优点和缺点。介绍他们只是因为的你可以在未来改变数据访问方法的是一个坏的理由。不要设计的,因为事情会发生你的应用程序。设计基于目前的实际应用要求(敏捷方法)和重构你的code如果需要改变 - 那就是如何在市场竞争激烈的唯一途径。特点是卖你的软件不是它的开放式架构的任何类型的变化(好吧,也有例外,但在这样的情况下,即开放式体系结构是一个顶层要求)。

Introducing repositoris will add a new layer of complexity. Repositories have their pros and cons. Introducing them just because "you can change data access approach in the future" is a bad reason. Do not design your application because of something can happen. Design the application based on current real requirements (an agile way) and refactor your code if a change is needed - that is the only way how to be competitive in the market. Features are selling your SW not its open architecture for any type of change (ok, there are exceptions but in such cases that open architecture is a top level requirement).

在使用EF你有多种选择如何创建实体:

When using EF you have several choices how to create entities:

  • 使用cutom工具生成的实体对象。这是一个创建文件EDMX的背后,code默认的态度。这是EFv1(.NET 3.5 SP1)的唯一可用的解决方案。
  • 使用T4模板生成的实体对象,波苏斯,国营或任何自定义实体类型(可以修改生成逻辑)。这通常用于具有EFv4
  • 写波苏斯你们自己。这可以用EFv4使用,它总是使用与EF 4.1 code第一种方式。

如果你期望的数据访问施工技术的可以在以后的使用第二或与波苏斯第三种方法改变。在T4模板的情况下,你可以简单地复制生成波苏斯或修改项目文件,这样你就不会删除EDMX文件后,失去他们。

If you expect that data access technolgy can change in the future use either the second or the third approach with POCOs. In the case of T4 templates you can simply copy generated POCOs or modify a project file so you will not lose them after deleting EDMX file.

如果您不能确定,如果第二或第三种方法适合你检查我的这些问题的答案:

If you are not sure if either second or third approach suits you check my answers to these questions:

  • <一个href="http://stackoverflow.com/questions/5340990/ado-net-dbcontext-generator-vs-ado-net-poco-entity-generator">ADO.NET的DbContext发电机与ADO.NET POCO实体生成器
  • <一个href="http://stackoverflow.com/questions/5086548/ef-poco-$c$c-only-vs-ef-poco-with-entity-data-model">EF POCO code仅VS EF POCO与实体数据模型
  • ADO.NET DbContext Generator vs. ADO.NET POCO Entity Generator
  • EF POCO code only VS EF POCO with Entity Data Model

由于不知何故,我同意@ Patko的回答,你也应该检查 Ayende的博客。他写了几个职位有关通过使用存储库,并在设计应用程序。他在书面方式有关NHibernate的,但类似的decissions可与EF进行。唯一的区别是,NHibernate的提供了更好的抽象所以使用直接code NHibernate的是更好的测试。

Because I somehow agree with @Patko's answer you should also check Ayende's blog. He has written several posts about over using repository and over architecting applications. He is writting about NHibernate but similar decissions can be made with EF. The only difference is that NHibernate offers better abstraction so the code using directly NHibernate is better testable.

这篇关于Repository模式与实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆