views:

48

answers:

2

While I have been dealing with domain-driven design (DDD) for quite some time now, I'm relatively new to Entity Framework (EF), and one question that came to my mind when using the Entity Framework Designer in Visual Studio was how Aggregates should be represented/modeled in EF.

Following DDD best practices, Entities should only reference other Entities (or Value Objects) within the same Aggregate, and references to other Entities are restricted to Root Entities of Aggregates (Aggregate Roots). However, I don't see any of these concepts present in EF (i.e., all Entities are treated alike, and consequently no restrictions are applied on references between Entities).

Thus, I'm asking: did I miss something in EF, or is it completely agnostic about Aggregates, Aggregate Roots and references between Entities? If the latter is the case, how do you model Aggregates when using Entity Framework?

+1  A: 

I think DDD is other level of abstraction so my answer is no EF by default does not follow these practicies. It is up to you to model your entities and repositories to follow DDD. You will use repositories to build your aggregate root with loaded related entities related only to current aggregate root and you will use domain services to work with different repositories.

Ladislav Mrnka
That's what I was suspecting, thanks for the confirmation!
angelwithagun
A: 

Hi,

I just wanted to correct a small (but pretty important detail):

You state that "Entities should only reference other Entities (or Value Objects) within the same Aggregate".

Of course, there could be some arguments for this, but it's stricter than what at least one resource on DDD recomends: "Objects inside an Aggregate should be allowed to hold references to roots of other Aggregates." ("Domain-Driven Design Quickly" by Avram & Marinescu).

Best regards, Simon

SimonT