不使用存储库模式,使用ORM的是(EF) [英] NOT using repository pattern, use the ORM as is (EF)

查看:173
本文介绍了不使用存储库模式,使用ORM的是(EF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用存储库模式但我的最新项目,我想看看我是否能完美的使用它,我的实行单位工作的。我开始挖我开始问自己这个问题的更多:?难道我真的需要它

I always used Repository pattern but for my latest project I wanted to see if I could perfect the use of it and my implementation of "Unit Of Work". The more I started digging I started asking myself the question: "Do I really need it?"

现在这一切都开始于一个#1夫妇的意见有一丝Ayende Rahien的在他的博客中,有2个具体的,

Now this all starts with a couple of comments on Stackoverflow with a trace to Ayende Rahien's post on his blog, with 2 specific,

这也许可以永远永远谈论它依赖于不同的应用。请告诉我我想知道,

This could probably be talked about forever and ever and it depends on different applications. Whats I like to know,


  1. 将这个方法是适合的实体框架项目?

  2. 使用这种方法是将业务逻辑的服务层,或扩展方法仍在继续(如下面解释,我知道,扩展的方法是使用NHib会话)?

这是很容易使用的扩展方法进行。清洁,简单,可重复使用。

public static IEnumerable GetAll(
    this ISession instance, Expression<Func<T, bool>> where) where T : class
{
    return instance.QueryOver().Where(where).List();
}

使用这种方法和 Ninject 为DI,做我需要做的上下文 A接口,并注入在我的控制器?

Using this approach and Ninject as DI, do I need to make the Context a interface and inject that in my controllers?

推荐答案

我已经走了许多路,创造了仓库的很多实现对不同的项目和...我已经抛出的毛巾,并放弃它,这里的原因。

I've gone down many paths and created many implementations of repositories on different projects and... I've thrown the towel in and given up on it, here's why.

编码为异常

你code为1%的几率使你的数据库将会从一种技术切换到另一个?如果你想你的企业的未来状态,说是这是一种可能性则a)他们必须有大量的资金负担做迁移到另一个数据库技术或b)你选择一个DB技术为乐趣或c )出了一些可怕的错误与您决定使用第一种技术。

Do you code for the 1% chance your database is going to change from one technology to another? If you're thinking about your business's future state and say yes that's a possibility then a) they must have a lot of money to afford to do a migration to another DB technology or b) you're choosing a DB technology for fun or c) something has gone horribly wrong with the first technology you decided to use.

为什么要扔掉丰富的LINQ语法?

LINQ和EF被开发,所以你可以做奇妙的东西用它来读取和遍历对象图。创建和维护版本库,可以给你同样的灵活性,这样做是一个可怕的任务。在我的经验,我已经创建了一个库我已经随时始终有业务逻辑泄露到存储库层,要么使查询的详细执行,蚂蚁和/或减少点击次数到数据库。

LINQ and EF were developed so you could do neat stuff with it to read and traverse object graphs. Creating and maintain a repository that can give you the same flexibility to do that is a monstrous task. In my experience any time I've created a repository I've ALWAYS had business logic leak into the repository layer to either make queries more perform-ant and/or reduce the number of hits to the database.

我不想为一个查询我写的每一个排列创建一个方法。我还不如写存储过程。我不想GetOrder,GetOrderWithOrderItem,GetOrderWithOrderItemWithOrderActivity,GetOrderByUserId,等等......我只想让主实体和遍历,包括对象图,我所以请。

I don't want to create a method for every single permutation of a query that I have to write. I might as well write stored procedures. I don't want GetOrder, GetOrderWithOrderItem, GetOrderWithOrderItemWithOrderActivity, GetOrderByUserId, and so on... I just want to get the main entity and traverse and include the object graph as I so please.

大部分资料库的例子是废话

除非你真的是开发一些最基本的像一个博客或一些查询永远不会像你周围的存储库模式在互联网上找到的例子中90%那么简单。我不能强调这就够了!这是什么,一个人在泥泞中爬弄清楚。总会有一个查询,打破你已经创建了完全想出来的资料库/解决方案,它不是直到地步,你猜2次自己和技术债务/侵蚀开始。

Unless you are developing something REALLY bare-bones like a blog or something your queries are never going to be as simple as 90% of the examples you find on the internet surrounding the repository pattern. I cannot stress this enough! This is something that one has to crawl through the mud to figure out. There will always be that one query that breaks your perfectly thought out repository/solution that you've created, and its not until that point where you 2nd guess yourself and the technical debt/erosion begins.

不要进行单元测试我的兄弟

但对于单元测试,如果我没有一个仓库?我怎么嘲弄?简单的你不知道。让我们看一下它从两个角度:

But what about unit testing if I don't have a repository? How will I mock? Simple you don't. Lets look at it from both angles:

没有资源库 - 您可以使用IDbContext或其他一些技巧嘲笑的DbContext但你真的单元测试的LINQ to Objects和因为查询在运行时确定没有LINQ到实体... OK所以这不是好!所以,现在其高达集成测试涵盖这一点。

No repository - You can mock the DbContext using an IDbContext or some other tricks but then you're really unit testing LINQ to Objects and not LINQ to Entities because the query is determined at runtime... OK so that's not good! So now its up to the integration test to cover this.

使用存储库 - 您现在可以嘲笑你的资料库和单元测试之间的层(S)。大吧?那么不是真的...在上面,你有泄漏的逻辑到存储库层,使查询的详细执行,蚂蚁和/或更少的点击到数据库的情况下,如何在你的单元测试覆盖?它现在在回购层和你不想要测试IQueryable的吧?也让说实话,你的单元测试不打算覆盖具有20行查询。凡()子句和 .INCLUDE() SA一堆的关系,并再次访问数据库做这一切其他的东西,胡说,胡说,胡说反正是因为在运行时生成的查询。此外,由于您创建了一个信息库,以保持高层持久性无知,如果现在你想改变你的数据库技术,对不起你的单元测试挑衅不会保证同样的结果在运行时,回集成测试。所以库的整点似乎不可思议。

With repository - You can now mock your repositories and unit test the layer(s) in between. Great right? Well not really... In the cases above where you have to leak logic into the repository layer to make queries more perform-ant and/or less hits to the database, how can your unit tests cover that? It's now in the repo layer and you don't want to test IQueryable right? Also lets be honest, your unit tests aren't going to cover the queries that have a 20 line .Where() clause and .Include()'s a bunch of relationships and hits the database again to do all this other stuff, blah, blah, blah anyway's because the query is generated at runtime. Also since you created a repository to keep the upper layers persistence ignorant, if you now you want to change your database technology, sorry your unit tests are defiantly not going to guarantee the same results at runtime, back to integration tests. So the whole point of the repository seems weird..

2美分

我们使用EF通过纯存储过程(批量插入,批量删除,CTE的,等等)的时候,但我也美元的C#C $ç所以我不必键入二进制已经失去了很多的功能和语法。我们使用EF所以我们可以有使用不同供应商的可能性,并与对象图在一个不错的方式相关的以及很多工作的事情。某些抽象是有用的,有些则不是。

We already lose a lot of functionality and syntax when using EF over plain stored procedures (bulk inserts, bulk deletes, CTE's, etc.) but I also code in C# so I don't have to type binary. We use EF so we can have the possibility of using different providers and to work with object graphs in a nice related way amongst many things. Certain abstractions are useful and some are not.

我希望这有助于在互联网络有人的地方...

I hope this helps someone on the internets somewhere...

这篇关于不使用存储库模式,使用ORM的是(EF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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