DDD-乐观并发属性(etag或时间戳)是否应该成为域的一部分? [英] DDD - Should optimistic concurrency property (etag or timestamp) ever be a part of domain?

查看:173
本文介绍了DDD-乐观并发属性(etag或时间戳)是否应该成为域的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从理论上讲,如果我们在聚合根级别上实现乐观并发(更改AR中的实体会更改AR上的版本),并且可以说我们将时间戳用于版本属性(仅出于简单性考虑)-时间轴应该是AR上的属性还是应该它一方面是读取模型的一部分,另一方面(例如更新)是应用程序服务的单独参数,例如:

Theoretically speaking, if we implement optimistic concurrency on Aggregate Root level (changing entities in AR changes version on AR) and lets say we use timestamp for version property (just for simplicity) - should timeline ever be a property on AR or should it be a part of read model on one side and on other (for example, updates) be a separate argument to application service like:

[pseudo]

public class AppService{
.
.
.
   public void UpdateSomething(UpdateModelDTO model, int timestamp)
   {
      repository.GetModel(model.Identifier);
      model.UpdateSomething(model.something);
      repository.ConcurrencySafeModelUpdate(model, timestamp);
   }
}

我看到两者的优点/缺点,但想知道这是什么本书的解决方案?

I see pros/cons for both but wondering which is by-the-book solution?

[更新]

要回答@ guillaume31的问题,我希望平时场景:

To answer question from @guillaume31, i expect usual scenario:


  1. 读取时,读取版本标识符并将其发送给客户端

  2. 在更新时,客户端发送回标识符,如果版本标识符不同,则存储库将返回某种错误。

我不知道它的标识符重要,但我想自己创建/更新版本标识符到我的数据库系统。

I don't know if its important but i want to leave responsibility for creating/updating version identifiers themselves to my database system.

推荐答案

图书解决方案。有时您的汇总中已经有一个非常适合该角色的字段(例如 LastUpdatedOn ),有时您可以使其成为非域数据。出于性能原因,最好将字段选择为获取聚集的同一查询的一部分。

There's no by-the-book solution. Sometimes you'll already have a field in your aggregate that fits the role nicely (e.g. LastUpdatedOn), sometimes you can make it non-domain data. For performance reasons, it may be a good idea to select the field as part of the same query that fetches the aggregate though.

某些ORM提供了检测并发冲突的工具。某些DBMS可以自动为您创建和更新版本列。您应该在特定堆栈中寻找有关乐观并发的准则。

Some ORMs provide facilities to detect concurrency conflicts. Some DBMS's can create and update a version column automatically for you. You should probably look for guidelines about optimistic concurrency in your specific stack.

这篇关于DDD-乐观并发属性(etag或时间戳)是否应该成为域的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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