Domain driven design child entities change tracking
Asked Answered
S

1

9

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?

Sometime answered 20/5, 2013 at 19:39 Comment(0)
L
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.

Lymphosarcoma answered 20/5, 2013 at 19:51 Comment(4)
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.Sometime
why not map EF directly to domain objects?Lymphosarcoma
im using model 1st in my ef im not using code 1st... i made classes that map domain object to ef entity..Sometime
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.Lymphosarcoma

© 2022 - 2024 — McMap. All rights reserved.