与轴突框架一对一关系 [英] One to One relationship with axon framework

查看:118
本文介绍了与轴突框架一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Axon Framework的新手。我正在处理的资产管理模块中有一项要求。
在此模块中,将构建不同类型的资产,这些资产需要以类似于SQL中一对一关系的方式进行配对。我发现很难为此格式设计汇总。

I'm new to Axon Framework. I've a requirement within an asset management module which I am working on. In this module different types of asset are build, which need to be paired in a similar fashion as one to one relationships in SQL. I am finding it difficult to design an Aggregate for this format.

业务逻辑验证如下:
输入两个 assetId 。这些标识符类似于集合标识符。
然后,加载绑定到这些 assetId 的资产实例,并检查状态是否为未配对或配对。如果两个资产都未配对,则将它们配对(将状态更新为paired,并将UUID添加到associatedAssets)。否则引发异常。

The business logic validation is as follows: Two assetIds are inputs. These identifiers resemble aggregate identifiers. Then, load the asset instances tied to these assetIds and check if the status is unpaired or paired. If both the assets are unpaired then pair them (update the status to paired and add UUID to associatedAssets). Else raise an exception.

我想出了以下聚合类:

@Aggregate
@Data
public class AssetAggregate {
    @AggregateIdentifier
    private UUID assetId;
    private String assetType;
    private HashMap<String,String> attributes;
    private String status;
    private String modifier;
    private UUID associatedAsset;
}

我的配对命令消息是:

@Data
public class PairAssetCommand {
    private UUID assetAId;
    private UUID assetBId;
}


推荐答案

在您给出的示例中, PairAssetsCommand 不能由单个 AssetAggregate 处理,因为它跨越了两个不同聚合实例的一致性边界。即,两个不同的 AssetAggregate s。

In the sample you have given, the PairAssetsCommand can not be handled by a single AssetAggregate as it spans the consistency boundary of two distinct aggregate instances. Namely, two different AssetAggregates.

请注意,聚合定义了命令模型内的一致性边界。因此,它所接受的任何命令及其所有导致的事件(以及状态更改之后)都将被视为原子操作。通过这种方式在多个实体之间建立关联可能意味着两件事:

Note that the Aggregate defines the consistency boundary within your command model. Thus any command taken in by it and all it's resulting events (and following state changes) will be regarded as an atomic operation. Making associations between several entities through this can mean two things:


  1. 您创建了一个跨 all 的更大的Aggregate类 AssetAggregate s。

  2. 您有一个外部命令处理程序(即, @CommandHandler 处理 PairAssetsCommand 的聚合)。

  1. You create a bigger Aggregate class which spans all AssetAggregates.
  2. You have a External Command Handler (i.e. a @CommandHandler outside of an Aggregate) which handles the PairAssetsCommand.

我建议不要使用选项一,因为它将扩大系统中整个资产集合的一致性边界。最终,这将成为维护保持一致性边界集合的要求的主要瓶颈。

I'd advise against option one, as it will enlarge the consistency boundary towards the entire set of assets in your system. This will eventually become a major bottleneck the maintain an Aggregate's requirement of "keeping the consistency boundary".

因此,这留下了选项2。让我们重新定义一下您定义的业务逻辑:

That thus leaves option 2. Let's rephrase the business logic you have defined:


如果两个资产都未配对,则将它们配对(将状态更新为配对,并将UUID添加到associatedAssets),否则引发异常

if both the assets are unpaired then pair them(update the status to paired and add UUID to associatedAssets) else raise an exception

这意味着您无法在单个实例上进行验证,但需要在多个实例上进行验证。同样,您可以采取两种方法来解决此问题:

This means you cannot validate on a single instance, but need to do this on several. Again, you can take two routes to solving this:


  1. AssociateWithAssetCommand 发送给如果 AssetAggregate 之一已经关联,则同时发送 AssetAggregate 并调度补偿命令。

  2. 使用基于集合的验证在处理 PairAssetsCommand 的外部命令处理程序中,以验证您的业务逻辑。

  1. Dispatch a AssociateWithAssetCommand to both the AssetAggregates and dispatch a compensating command if one of the AssetAggregates is already associated.
  2. Use set based validation in the external command handler handling the PairAssetsCommand to validate your business logic.

我想说,最好的是两个。解决方案二要求您使用小型查询模型,其中包含一组资产及其关联状态。补充说,此查询模型需要在与关联命令发生时相同的事务中更新。因此,稍微复杂一些。

Which of the two is best is left to preference I'd say. Solution two requires you to have a small query model containing a set of assets and their association status'. Added, this query model needs to be updated in the same transaction as when the association commands occur. Thus, somewhat more complicated.

因此,一种解决方案将是您遇到的最简单的方法。

Hence solution one would be the simplest way to go in your scenario.

这篇关于与轴突框架一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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