Entity Framework Code First wrapper or repository?

无人久伴 提交于 2019-12-11 23:06:01

问题


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) Injection - Hard to inject as there isn't a clear cut Interface

2) Mocking - Same as above

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:


  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.


来源:https://stackoverflow.com/questions/7651237/entity-framework-code-first-wrapper-or-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!