Entity Framework 4 DB-First Injection Dependency Injection?

I prefer to create my own database, configure indexes, unique restrictions, etc. Creating a domain model from a database is cinch with the Entity Framework edmx designer.

Now I'm interested in setting up some repositories using Injection Dependency. I looked through some articles and posts on StackOverflow and seemed to focus on a code approach. You can create a common repository for CRUD processing and use Dependency Injection to select implementation details.

I would like to do the same, but it seems that the domain model generated by the edmx process inherits specific classes instead of implementing interfaces (ObjectContext / ObjectSet, unlike IObjectContext / IObjectSet).

Does anyone have any resources that they can point me to, how can I use Injection Dependency when using the db-first / code generation method?

+4
source share
2 answers

I may not understand your question, but the fact that EDMX generates code that inherits ObjectContext does not stop you from using dependency injection. You seem to be worried that you cannot inject your ObjectSet into your repository, but that’s not quite the way it is intended to be used.

With a common repository template like the one found here , the IRepository interface is what you enter into your ViewModels / Controllers / Whatever.

So, you do not enter IObjectContext or IObjectSet in your repository; instead, you inject your IRepsoitory into your classes that need it, and you provide an implementation of the IRepository interface that your ObjectSet uses. You can then trick your IRepository interface into testing or switch to a completely different specific repository implementation without affecting any other code.

We are currently doing the same with the EF4 DB-first and the repository template that I linked above, and it works very well.

+10
source

I myself was looking for an answer to this question, and I got this solution: a DBContext generator tutorial for creating a POCO model after creating an Entity model with the database first.

After that, the implementation is quite simple, as it is very similar to CodeFirst Repository & DI repository and DI (IoC) patterns

+1
source

Source: https://habr.com/ru/post/1385568/


All Articles