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.
source share