实体框架代码是第一个包装器还是存储库? [英] Entity Framework Code First wrapper or repository?

查看:103
本文介绍了实体框架代码是第一个包装器还是存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到它有时会提到,Repository Pattern是通过DbSet和DbContext对象内置到Entity Framework Code First中的.

I've seen it mentioned sometimes that Repository Pattern is built into Entity Framework Code First via the DbSet and DbContext objects.

但是仍然存在一些问题:

However that leaves a few problems:

1)注入-由于界面不清晰,难以注入

1) Injection - Hard to inject as there isn't a clear cut Interface

2)模拟-与上面相同

2) Mocking - Same as above

3)对EnitityFramework.dll的多次引用-假设我先在自己的程序集/项目中创建我的代码,然后要引用该引用,而在另一个地方我也必须引用不存在任何包装器的EntityFramework.dll

3) Multiple references to EnitityFramework.dll - Let's say I create my Code First in it's own assembly/project and then want to reference that in another place I also have to reference entityFramework.dll without some wrapper present

您是否同意这一点?您认为最好的解决方案是什么?

Do you aggree with this and what do you think is the best solution if you do?

推荐答案

  1. DbSet具有接口,通常您可以实现派生自DbContext的上下文类,因此它也可以实现自己的接口,使您可以毫无问题地处理注入.
  2. 这是更复杂的问题.模拟上下文没有意义,模拟IDbSet也没有意义,但同时模拟任何公开了IQueryable或接受传递给Linq-to-entities的Expression<Func<>>的存储库或包装器都没有意义两者之一(此处是简单的示例).因此,是的,存储库可以处理此问题,但是您将不得不为此付出更多的努力,并且您将不会使用Linq从调用存储库的代码中查询数据库.如果希望上层使用声明性查询(如使用存储库时所期望的那样),则必须实现自己的规范.
  3. 如果在GAC中没有EntityFramework.dll,并且您将引用新解决方案中的第一个程序集,您仍然会添加对EntityFramework.dll的引用,以确保它与您的代码一起部署.否则你是对的.没有包装,您需要参考.
  1. DbSet has interface and you usually implement your own context class derived from DbContext so it can also implement your own interface allowing you to deal with injection without any problem.
  2. This is more complex issue. Mocking context doesn't make sense, mocking IDbSet doesn't make sense as well but in the same time mocking any repository or wrapper exposing IQueryable or accepting Expression<Func<>> passed to Linq-to-entities doesn't make sense either (here is simple example why). So yes repository can handle this but you will have to put more effort into this and you will not use Linq to query database from code calling your repository. If you want your upper layer to use declarative queries (as expected when using repository) you must implement your own specifications.
  3. Imho if you don't have EntityFramework.dll in GAC and you will reference your first assembly from new solution you will still add reference to EntityFramework.dll to make sure that it is deployed with your code. Otherwise you are right. Without wrapper you need a reference.

这篇关于实体框架代码是第一个包装器还是存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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