DDD Mapping Entity Framework Data Model to Domain models

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

Question

I am aware that entity data models and actual domain models should be kept apart in order to prevent coupling between infrastructure issues and the domain itself, but if all domain properties lack public setters, how can we map from data model to domain model? This is especially true if the repository implementation is located in the infrastructure portion of the project, which prevents us from using internal property setters.

class DomainModel
{
    string SomeProperty {get; private set:}
}
1
3
10/25/2016 9:21:19 AM

Accepted Answer

ZZZ_tmp
2
10/25/2016 12:11:53 PM

Popular Answer

I understand that Entity data models should be separated from real domain models

True but

avoid coupling between infrastructural concerns and domain itself

True


You can directly persist your domain models using EF, but you shouldn't bind your domain models to EF.

How does this appear?

Suppose you are working on a project namedDomain which features your models, and another one titledDataStore It maintains said models in your repositories (using EF)

When using EF, you often put attributes and other nonsense on the models you wish to persist, however this contaminates the models by coupling them to EF as a storage method and introduces dependencies on EF from your pure models.Domain This is the project that we are attempting to avoid.

Look at the FluentApi EF6 for information on how to set up your domain models to function with EF6 without adding any EF-specific properties or dependencies to the model.Domain project.

First Key?

modelBuilder.Entity<OfficeAssignment>().HasKey(t => t.InstructorID);

Index?

modelBuilder 
.Entity<Department>() 
.Property(t => t.Name) 
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute()));

If you perform this for each and every item, it will rapidly become tedious, but if you follow certain patterns (or use a base type to make this even easier)

Then, you may do this via reflection.auto-magically for each and every Entity in your domain.

Good luck



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