Reputation: 21
I defined my interfaces in infrastructure layer, to use Dependency Injection, but now problem, how can i resolve dependency of DBContext using interface, without adding reference to EF dll, in infrasturcure layer and service layer.
Upvotes: 1
Views: 975
Reputation: 172835
If you need to hide EF completely from your application, you will need to use the repository pattern, hide EF behind your repositories and generate (or write) POCO entities.
If you're more pragmatic, you can use generic repositories with IQueryable support, which allows a great development and unit testing experience, but what to choose is up to you.
Upvotes: 2
Reputation: 18118
You can modify the T4 files (aka T4 templates or .tt files) to create interfaces along with context and even separate them into separate T4 files for each of the two, so you can place them in separate assemblies. You can also make the context return IQueryable instead of ObjectQuery, however...
In order to write optimized query that run on the database and not in memory, the queries must take into account the technology beneath them, you can not write generic queries, unit test them on a in memory list, then expect them to translate to SQL correct and run efficiently and without exceptions. - You will have to test your queries above a real database (with demo data).
What you should do is implement services which hide the DAL technology from the layers above it, yet inside their implementation use the full power of EF to work as efficiently as possible.
These services can be mocked, to test the layers above them and the services themselves can be tested together with their usage of EF, using a test DB (e.g. using a LOCALDB instance created and started by the test class).
A few of the many relevent links:
Generic Repository With EF 4.1 what is the point
ASP.NET MVC3 and Entity Framework Code first architecture
Is UnitOfWork and GenericRepository Pattern redundant In EF 4.1 code first?
https://softwareengineering.stackexchange.com/questions/133448/unit-integration-testing-my-dal
Upvotes: 0