DDD-仅在更改后持久保留子级 [英] DDD - persisting aggregate children only if changed

查看:73
本文介绍了DDD-仅在更改后持久保留子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在当前正在使用的应用程序中使用DDD。
我具有以下UserAggregate结构:

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

UserAggregate
- ProfileEntity
- ImageEntity
- RatingEntity

我有一个UserRepository,它正在查询实体映射器以构建UserAggregate。

And i have a UserRepository which is querying entities mappers to build a UserAggregate.

现在我想将UserAggregate传递给UserRepository以保持持久性,例如 UserRepository-> save(UserAggregate) 。如何告诉UserRepository UserAggregate子实体已更改,需要保存?有什么通用的模式吗?我知道UintOfWork模式,但无法真正想象它对孩子有什么帮助,因为我只想在实际更改子实体的情况下才能访问映射器(和数据库)

Now I'd like to pass the UserAggregate to the UserRepository for persistance, like UserRepository->save(UserAggregate). How do I tell the UserRepository that UserAggregate children entities have changed and needs to be saved? Is there any common pattern for that? I'm aware of UintOfWork pattern but can't really imagine how it may help with children, as I'd like to hit the mappers (and the database) only if the children entities are actually changed.

有没有办法跟踪实体对象的脏状态,特别是在PHP中?还是我错过了聚合根和存储库的概念?

Is there any way to track a "dirty state" of the entity object, specifically in PHP? Or Am I missed the concept of aggregate roots and repositories?

推荐答案

基本上,有两种方法可以解决该问题。您可以使用快照比较基于代理的更改跟踪。两者都有优点和缺点。选择还取决于您使用的库,因为它们可能支持其中一种。

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.

这两种策略都是持久性机制的责任,绝不能泄漏到域和应用程序逻辑中。换句话说,它们必须对存储库用户和域对象透明。

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.

使用此策略,当通过存储库加载聚合时,可以保留聚合数据的快照。稍后,当潜在修改过的聚合再次通过 Update 调用传递到存储库时,您将遍历该聚合以确定其中的数据是否已更改。

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.

使用此策略,您将返回一个对象,该对象代理实际的聚合而不是聚合本身。通过存储库加载聚合时,将创建代理并将其用于包装聚合。

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.

这篇关于DDD-仅在更改后持久保留子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆