Creating instances of derived model with Entity Framework

Aug 11, 2011 at 9:29 AM

Hello all,

hopefully you can give me a hint on something I read about in the documentation. On page 159 (last paragraph / section: "Factoring Application Code with ASP.NET MVC") it is suggested to derive the domain model from the data model if they are very similar. We are currently looking into such a solution together with Entity Framework. The problem is that I have no idea how the derived domain model classes would be instantiated with EF. It only knows about the base (data) model. If I could only configure sort of factories to use for specific types like for example: I have a class DomainCar derived from DataCar. When I read all my DataCars via EF I would need to get Instances of DomainCar which wouldn't be a problem, but I can't configure the object creation. So does anyone know how to achieve that or is deriving a domain model from the data model impossible when using EF?

Just besides: our scenario is actually about deriving a UI-Model from the Domain/DataModel which consists of POCOs anyways. We do need to have BindingLists instead of the actual model's IEnumerables and some additional calculated Properties on the derived model classes.

Any help would be highly appreciated.

 

Best regards

Gope

Developer
Aug 12, 2011 at 5:10 PM

Hi Gope,

As there is no guidance in Silk regarding how to use EntityFramework to instantiate the domain models (which inherit from data models), in my opinion you could define your own factory scheme to retrieve instances of them. For example, you could use the Unity container and use the IUnityContainer.RegisterType method to specify that, when you request the container to resolve a certain interface, (through the IUnityContainer.Resolve method), an instance of your domain model is returned; or you could use any other approach for retrieving instances of the domain models that you consider appropriate for your scenario.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Aug 14, 2011 at 9:57 AM

Hello Guido,

thank you for your reply. The main goal would be to not having to create instances of my Domain Model from datamodel instances, but only have one type (the derived domain type). Therefore Entity Framework would need to have a hook or some mechanism that would take sort of factories or a DI Container to request instances from for a certain type. Unfortunately it doesn't. The scenario you are describing still has an integral problem: you have not only two models, but also two layers of instances, meaning you will have to maintain relations between objects in both layers. Wether the domain model is instanciated via new() or a DI-Container doesn't make a difference. And that is putting so much complexity on top of our current solution. Still, thanks for your ideas on this.

Best Regards

Gope

Developer
Aug 15, 2011 at 5:25 PM

Gope,

In case you define your domain model as a class that inherits from the EntityFramework data model (which is the scenario you need to fulfill in order to avoid maintaining two separate instances), you can then use the factory/ DI container of your choice directly to request instances of that domain model, which should account for your requirement. In the scenario I'm describing, you would only be using instances of your domain model. Have you experienced difficulties when trying to implement it?

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Aug 16, 2011 at 2:49 PM

Hello Guido and thanks for speding a bit of your time,

there's one thing I don't understand about your idea. We are retrieving "data / objects" through EntityFramework. It instanciates only datamodel-classes. If I could hook Unity into EF your suggestion would work, but as far as I know that is not possible. You wrote above "you can then use the factory/ DI container of your choice directly to request instances of that domain model", but the problem is am not requesting it, but rather need to tell EF to get the objects from the DB. This whole idea would work brilliantly if EF was offering support for injecting object construction, be it an DI-Container, a simple factory or at least register a delegate for it. Am I missing something here? It appears you have an Idea how to do this...

Just to be clear: I believe Unity cannot be used in this scenario, because EF is instanciating the objects (datamodel). All Unity could possibly do is to build domain objects from given datamodel objects (created by EF) but these would be new instances (new references, etc.) and that's not helping with the problem I guess. Or if I did not understand your idea please describe how to hook Unity into EF.

Best regards

Gope

Developer
Aug 17, 2011 at 4:46 PM

Gope,

In my previous answers I was referring to the class that inherits from object context, not to the actual instances of your entities retrieved by Entity Framework. So in your context, my previous suggestions won't be useful.

Now that I understand your scenario correctly, I believe the following article might be useful for your case:

Customizing Objects

From the article: "The Entity Framework enables you to use custom data classes with your data model without making any modifications to the data classes themselves, as long as the names of the entity types, complex types, and properties in the custom data classes match the names of the entity types, complex types, and properties in the conceptual model. This enables you to use "plain-old" CLR objects (POCO), such as domain objects, with your data model."

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi