Domain model mapping

328 Views Asked by At

I have domain model and data model:

  • Domain model (DoM) is designed for business layers to simplify the operations.
  • Data model (DaM) is automatically generated from the data base using entity framework.

I would like to map DoM <> DaM in both directions. I have tried Automapper, but I am having one big issue with it, say DaM data model has an entity Car, which is a direct translation of the database table

Car
{
   Type {get; set;}
   MaxSpeed {get; set;}
}

Domain model DoM has the following structure:

Car
{
   MaxSpeed {get; set;}
}

SportsCar : Car
{
}

F1 : SportsCar 
{
}

And the mapping should be:

if (DaM.Car(car).Type == Type.SportsCar)
   DaM.Car.MaxSpeed > 350 ? map DaM.car to DoM.F1 : map DaM.car to DoM.SportsCar

What is the best way/tool to archive that?

How I can do it with automapper or valueinject?

2

There are 2 best solutions below

1
On

Have you looked into Table Per Hierarchy mappings using Entity Framework? Using the Type property on Car as the discriminator, you'd be able to add SportsCar and F1 entities to the data model and Entity Framework would retrieve them direct from the database. You could then use AutoMapper if you really want to have separate SportsCar and F1 classes in your domain model.

http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph

0
On

valueinjecter by default will match all properties of same name and type from source to target regardless of types so it would be something like this:

a.InjectFrom(b);

note a and b already exist, you create them in advance

you can see valueinjecter being used with EF in mvc project here: http://prodinner.codeplex.com