问题
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:
DbSet
has interface and you usually implement your own context class derived fromDbContext
so it can also implement your own interface allowing you to deal with injection without any problem.- 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 exposingIQueryable
or acceptingExpression<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. - 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