DDD - persisting aggregated children only when changing

I am trying to use DDD in the application I'm currently working on. I have the following UserAggregate structure:

UserAggregate - ProfileEntity - ImageEntity - RatingEntity 

And I have a UserRepository that requests object agents to create a UserAggregate.

Now I would like to pass UserAggregate to UserRepository to save, e.g. UserRepository->save(UserAggregate) . How do I tell UserRepository that the child objects of UserAggregate have changed and need to be saved? Is there a general outline for this? I know the UintOfWork template, but I can’t imagine how it can help with children, since I would like to hit mappers (and the database) only if the children are actually changed .

Is there a way to track the "dirty state" of an entity object, particularly in PHP? Or did I miss the concept of aggregate roots and repositories?

+6
source share
1 answer

There are two approaches to this area. You can use snapshot tracking or proxy-based change tracking . Both of them have their advantages and disadvantages. The choice also depends on the libraries you use, as they may support one of them.

Here I describe the main approaches. I don’t know which libraries you use exactly, so this will help you choose a strategy and evaluate the libraries.

Key Design Considerations

Both strategies are the responsibility of the persistence mechanism and SHOULD NOT leak into the domain and application logic. In other words, they must be transparent to users of the repository and domain objects.

Picture Comparison

With this strategy, you save a snapshot of the aggregate data when an aggregate is loaded through the repository. Later, when a potentially modified aggregate is transferred in the Update call to the repository again, you will run the aggregate to determine if the data in it has changed.

Proxy Based Change Tracking

Using this strategy, you return an object that proxies the real population instead of the aggregate itself. A proxy is created and used to wrap the aggregate when the aggregate is loaded through the repository.

The proxy server does nothing while reading in the aggregate, but sets a dirty flag whenever a mutation operation is called. When a (proxied) aggregate is transferred to storage for storage, you simply check the dirty flag.

+6
source

All Articles