DDD - persisting aggregate children only if changed

后端 未结 1 1817
半阙折子戏
半阙折子戏 2020-12-20 17:38

I\'m trying use DDD in an application I\'m currently working on. I have a following UserAggregate structure:

UserAggregate
- ProfileEntity
- ImageEntity
- Ra         


        
1条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 18:09

    There are basically two approaches to that problem. You can either use snapshot comparison or proxy-based change tracking. Both of them have advantages and disadvantages. Selection also depends on the libraries you use, as they may have support for one of them.

    Here, I describe the basic approaches. I don't know what libraries you're using exactly, so this will help you select a strategy and evaluate libraries.

    Basic Design Considerations

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

    Snapshot Comparison

    With this strategy, you keep a snapshot of the aggregate data when the aggregate is loaded through the repository. Later, when the potentially modified aggregate is passed in an Update call to the repository again, you walk the aggregate to determine whether data in it changed or not.

    Proxy-based Change Tracking

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

    The proxy does nothing on read operations on the aggregate, but sets a dirty flag whenever a mutating operation is invoked. When the (proxied) aggregate is passed to the repository for persisting, you simply check the dirty flag.

    0 讨论(0)
提交回复
热议问题