3

I understand that Entity data models should be separated from real domain models, to avoid coupling between infrastructural concerns and domain itself, but i was wondering if all domain properties do not have public setters, how can we map from data model to domain model, especially if repository implementation resides in infrastructural part of project, so we can't use internal property setters.

class DomainModel
{
    string SomeProperty {get; private set:}
}
12
  • 2
    Pass parameters with constructor. But EF already IS repository pattern and unit of work, why do you need yet another abstraction based on that? Also, if you use code first, you don't need data model; just map database to domain model. Commented Oct 25, 2016 at 12:14
  • @L-Four what if 2 different domain models share same infrastructural table, or you require complex property within complex property? Commented Oct 25, 2016 at 12:28
  • Then use techniques like "Entity Splitting". Not sure what you mean with complex property within complex property. Commented Oct 25, 2016 at 12:55
  • @L-Four Employee can have location, which can be complex type with address name, street number, zip code and geo_information, and geo_information can be another complex type, which means you need to have nested complex properties. I think entity framework does not support that, which means your domain model would be limited by infrastructural concerns. Commented Oct 25, 2016 at 13:04
  • 1
    Of course it supports that, through navigation properties. Commented Oct 25, 2016 at 13:20

2 Answers 2

2

In a schema where you have an intermediate "Data Model", Entity Framework no longer has control over how your domain entities are instantiated. Your Repository has. So they don't necessarily need public setters, you can also rehydrate them using the constructor.

One such technique is explained here : https://vaughnvernon.co/?p=879

Note that a simpler alternative can be to avoid the additional data model and use private setters (see https://lostechies.com/jimmybogard/2014/04/29/domain-modeling-with-entity-framework-scorecard/), if you consider the little impact EF will have on your entities a reasonable tradeoff.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Not true

avoid coupling between infrastructural concerns and domain itself

True


You CAN use EF to directly persist your domain models, however you should not couple your domain models to EF.

What does this look like?

Let's say you have a project called Domain which has your models in it, and another called DataStore which has your repositories to persist said models (using EF)

Now usually with EF you would use attributes and all sorts of nonsense on the models you want to persist, but this pollutes & couples those models to EF as a storage mechanism & adds dependancies to EF from your pure Domain project - this is what we are trying to avoid.

EF6 to the rescue, have a look at the EF6 FluentApi, you are able to configure your Domain models to work with EF without adding any EF specific attributes or dependancies to the Domain project.

Primary Key?

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

Index?

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

Now this will quickly become a pain if you do this for each and every object, BUT if you stick to some conventions (or use a base type to make this even easier)

Then you can use reflection to do this auto-magically for all Entities in your domain.

Good luck

1 Comment

I'm a bit late, but how do you solve foreign keys in fluent api, when you do not want navigational properties pollute your domain model?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.