Entity Framework: storing as a list of complex type

c# domain-driven-design entity-framework entity-framework-6 nhibernate

Question

I am kinda new with entity framework, I have the following scenario which I want to persist on DB with entity framework:

public class Period
{
    public Period() { }
    public DateTime From { get; set; }
    public DateTime To { get; set; }        
}

public class Class1
{
     public Period Validity {get; set;}
}

public class Class2
{
     public List<Period> Validities {get;set;}
}

I can persist Class1 configuring Period as a complex type, but then I cannot persist Class2.
I can persist Class2 configuring Period as an Entity, but then Class1 doesn't work when trying to add Class1.

And also, Period is not an entity, I don't want to treat it as an entity, I don't want to place an ID on Period, because my model has no sense with it. It was a struct and I has to make it become a class, already.

I would like to keep my existing model as it is now. Is it there a workaround or something that can allow me to define mapping at a lower level?

Would it be possible with NHibernate 4? I am ready to switch my all persistence layer to nhibernate, if worth it! Any hints?

1
3
10/23/2015 6:43:59 AM

Popular Answer

If you try to squeeze your (non-anemic) domain model into an ORM, you will always have to make compromises. So this is usually not a good idea except for very simple applications.

Create a persistence model that consists of dead-simple, getter-setter-only classes without any logic. Make them in a way that fits EF well and use them to persist data. Now map your persistence model to your domain model an vice-versa.

This may seem like overkill, but is the only clean solution if you really want to apply DDD. Try to keep the two models close, so that the mapping is simple. Automapper is a good tool for this kind of model-to-model mapping.

1
10/23/2015 9:21:32 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow