【问题标题】:CommonDomain/EventStore Interface FetchingCommonDomain/EventStore 接口获取
【发布时间】:2020-10-27 18:43:50
【问题描述】:

我想通过它实现的接口访问我的聚合根:

 repository.GetById[[IMyInterface]](id);    

我需要告诉 CommonDomain 或 EventStore 什么来完成此操作?我相信我的 IConstructAggregates 接收到存储事件的聚合的 Implementation 类型。我需要保留自己的 ID 地图吗?

例如,假设我有这些 agg 根:

class AggRoot1 : IInterface1, IInterface2 {}
class AggRoot2 : IInterface1, IInterface2 {}

我已经保存了一个具有“idFromAggRoot1”的 aggregate1 实例。 现在我想这样获取:

repository.GetById<IInterface1>(idFromAggRoot1);

由于 IInterface1 有两个实现者,我怎么知道以后应该创建什么?聚合根1?聚合根2?接口1? Activator 会在这里轰炸,所以我知道我需要实现 IConstructAggregates 但想知道是否还有其他描述符可以告诉我原始提交 agg 根类型是什么。

【问题讨论】:

  • 请解释一下这种结构的实际用途是什么?你在考虑“角色”吗?
  • 刚刚看到这个 - 实际用途是在我的聚合根上进行编码,而不需要客户了解它正在执行的实际实现。
  • 我承认解耦可能是件好事,但是你能举一个例子说明两个聚合根类型将实现相同的接口但不共享相同的实现吗?

标签: cqrs neventstore commondomain


【解决方案1】:

IConstructAggregates.Build() 方法可以实现以返回您需要的任何类型。

Common.AggregateFactory 中,默认实现通过Activator.CreateInstance(type) as IAggregate 创建聚合实例。

自己的实现可能如下所示:

public class MyAggregateFactory : IConstructAggregates
{
    public IAggregate Build(Type type, Guid id, IMemento snapshot)
    {
        if (type == typeof(IMyInterface))
            return new MyAggregate();
        else
            return Activator.CreateInstance(type) as IAggregate;
    }
}

编辑:聚合类型作为EventMessage.Header[EventStoreRepository.AggregateTypeHeader]存储在事件消息的标题中

【讨论】:

  • 谢谢,但仍然没有回答我的问题。我已经编辑了我的帖子以提供更多详细信息
  • 这有帮助,谢谢。看起来我需要扩展 EventStoreRepository 以获取流并读取第一个事件以获取该元数据。我在回购中提出了一个问题,以便在 IConstructAggregates 中提供这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
相关资源
最近更新 更多