8

I'm having some difficulty figuring out how the Aggregate Root will track changes on child entities.

Let say I have an aggregate:

  • Order (root)
  • OrderLineItem

With the Order class being the aggregate root. How will I track the changes made on each of the OrderLineItem through the Order class?

When I make a repository (implementing) e.g. an OrderRepository (because only the aggregate root can have the repository right?), how will my OrderRepository track the changes of each OrderLineItem?

Example:

  • Newly added but not committed to DB
  • Edited but committed to DB
  • Edited but not committed to DB

How do you guys deal with this?

Matt
  • 3,617
  • 2
  • 27
  • 39
Patrick87
  • 237
  • 1
  • 2
  • 10

1 Answers1

7

with the Order class being the aggregate root now how will I track the changes made on each of the OrderLineItem through the Order class?

All changes to the Order aggregate, including OrderLineItem, should go through the aggregate root. This way, the aggregate can maintain its integrity. As far as tracking changes, that depends on your persistence implementation. If using an ORM such as EF or NHibernate, then the ORM will take care of tracking changes. If using event sourcing, then changes are tracked explicitly as a sequence of events, usually maintained by the aggregate in OOP implementations. If using SQL directly, you can also avoid tracking changes and update the entire aggregate upon each commit.

and when I make a repository(implementing) say OrderRepository because only the aggregate root can have the repository right?

Yes, repository per aggregate.

eulerfx
  • 36,769
  • 7
  • 61
  • 83
  • hi.. eulerfx.. thanks.. though my domain model is different from my EF entities.. im doing some mapping in the Data Access Layer.. mapping classes in my domain to my EF entity..example: SalesTransaction (class in domain model), SpecialTransaction(class in domain model) but in DAL its just a Transaction (EF Entity) .. thats how i did it so i cant track my domain entities since its persistence ignorance (dont know who's implementing the repository interfaces).. so how do i track the changes?? since the upper layer uses domain entities. – Patrick87 May 20 '13 at 20:50
  • why not map EF directly to domain objects? – eulerfx May 20 '13 at 20:57
  • im using model 1st in my ef im not using code 1st... i made classes that map domain object to ef entity.. – Patrick87 May 20 '13 at 21:04
  • if you're dealing with two class hierarchies, another way to track changes is to do a comparison between the domain objects and the corresponding persistence objects. EF can do a similar thing behind the scenes I believe. – eulerfx May 21 '13 at 15:31