0

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?

Ingó Vals
  • 4,788
  • 14
  • 65
  • 113

1 Answers1

2
  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.
Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Good points, during development time I like only to point at straight dependencies so that's why the EF.dll thing bothers me. – Ingó Vals Oct 05 '11 at 09:42
  • What about exception handling and custom exception. Not sure about handling DB exception in each and every call. – Ingó Vals Oct 07 '11 at 10:14