A Softwaredeveloper
A Softwaredeveloper

Reputation: 11

Responsibilities of Repository in DDD (EntityFramework)

  1. Entities Foo and Bar are both aggregate roots
  2. Foo references Bar
  3. SomeService does the following a. Calls FooRepository.FindId() to get an instance of Foo b. Modifies Foo instance, and also makes some modification to the Bar instance that is referenced by the Foo instance c. Calls FooRepository.Update(Foo) to persist the changes made to the Foo instance

Questions: 1. Knowing that Foo->Bar, how would (should) FooRepository.FindId() construct the Foo instance and the Bar instance that it references? (assuming the EntityFramework is used and as I understand EntityFramework build entities and its dependencies automatically) 2. Given that the Foo instance references the Bar instance should FooRepository.Update() also persist the changes made to the Bar instance? If the answer is NO, assuming that Entity Framework is the technology used for the database access, how would SomeService tell the repository (or EntityFramework to be more accurate) to ignore the changes of Bar?

Upvotes: 1

Views: 665

Answers (2)

eulerfx
eulerfx

Reputation: 37709

  1. As pointed out by rantri, object references between different aggregates are discouraged. It is better to use an identity reference. This is because an aggregate needs to be a consistency boundary which becomes difficult to enforce if there are multiple aggregates at play.

  2. EF should be able update the referenced Bar entity. I know that in NHibernate this is called persistence by reachability and can be enabled or disabled. This however is problematic because it makes it more difficult to reason about how and when entities are persisted. The best way to tell something to ignore changes to Bar is to avoid directly referencing bar. Often times, the only reason something like Bar is referenced is for display purposes. In such cases it is better to use a dedicated read-model instead of polluting the domain objects. If a Bar reference is needed for a behavior on Foo then it would be better to have a surrounding application service provide the data that Foo needs directly to it.

Upvotes: 1

rantri
rantri

Reputation: 161

After reading Effective Aggregate Design by Vaughn Vernon it give me an insight about designing aggregates.

As a rule I will reference Bar by its identity. and if to change its boo state i will just get it from the BarRepository.

Upvotes: 2

Related Questions