Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOC with Entity Framework

I am trying to make use of Unity Framework with Entity Framework. Let me explain the scenario. Let's say I have a Database with 5 tables. I will have 5 interfaces each of them corresponding to one table in Database having each field of table as a member. Now I want my Entity Framework generated classes to implement corresponding table interface. All navigational properties should return objects as interface references. This should allow me to resolve these entities using Unity framework to allow any one to extend EF(data) entities without breaking required codes. Is this possible?

like image 777
Mazhar Qayyum Avatar asked Mar 11 '11 23:03

Mazhar Qayyum


Video Answer


1 Answers

No there is no extensibility point for resolving entities with IoC container. EF will always handle instance creation of persisted entities. Morover EF mapping must work with classes not interfaces.

You can only handle ObjectMaterialized event on ObjectContext and provide some additional initialization of created entities.

You can try to add interfaces by implementing custom T4 template and including some convention (like name of the interface is always I + name of the entity) but still without IoC.

like image 191
Ladislav Mrnka Avatar answered Oct 14 '22 07:10

Ladislav Mrnka