事件溯源聚合根用户

发布于 2025-01-11 02:49:31 字数 2440 浏览 1 评论 0原文

我是事件采购领域的新手,我启动了一个新项目来了解它,但我有几个关于架构的问题。

我的第一个问题是关于聚合根的。我有一个UserAggregateRoot。该聚合是否应包含用户实体通常可识别的属性?

我的意思是我的Aggregate看起来像这样

public class UserAggregateRoot : AggregateRoot
{
    public Login Login { get; private set; }
    public Password Password { get; private set; }
    public Name Name { get; private set; }
    public DateTime BirthDate { get; private set; }
    public UserId UserId { get; private set; }
    public Status Status { get; private set; }

    private void Apply(UserCreatedEvent e)
    {
        Version = e.Version;
        Login = e.Login;
        Password = e.Password;
        Name = e.Name;
        BirthDate = e.BirthDate;
        Key = e.Key;
    }

    private void Apply(UserActivatedEvent e)
    {
        Version = e.Version++;
        Status = e.Status;
        Key = e.Key;
    }

    private void Apply(UserDeletedEvent e)
    {
        Version = e.Version++;
        Key = e.Key;
    }

    private void Apply(UserLogedEvent e)
    {
        Version = e.Version++;
        Key = e.Key;
    }

    private void Apply(UserUpdatedEvent e)
    {
        Version = e.Version++;
        Login = e.Login;
        Password = e.Password;
        Name = e.Name;    
        Key = e.Key;
     }
} 

或者也许UserAggregateRoot应该存储用户实体看起来像

public class UserAggregateRoot : AggregateRoot
{
    public User User { get; private set; }

    private void Apply(UserCreatedEvent e)
    {
        User = e.User;
        Key = e.User.Key;
    }

    private void Apply(UserActivatedEvent e)
    {
        Version = e.Version++;
        User = e.User;
        Key = e.User.Key;
    }

    private void Apply(UserDeletedEvent e)
    {
        Version = e.Version++;
        Key = e.User.Key;
        User = e.User;
    }

    private void Apply(UserLogedEvent e)
    {
        Version = e.Version++;
        Key = e.User.Key;
    }

    private void Apply(UserUpdatedEvent e)
    {
        Version = e.Version++;
        User = e.User;
        Key = e.User.Key;
    }
}

第二个问题是如果UserAggregateRoot存储一些订单,我应该存储UserAggregateRoot 中的订单实体列表?

如果订单是类似于 OrderAggregateRootAggregateRoot 该怎么办?我应该将 AggregateKey 存储在我的 UserAggregateRoot 中以指向此 OrderAggregateRoot 吗?

第三个问题是聚合根是否应该在内部包含业务逻辑,或者业务逻辑是否应该在传递给此聚合的实体内部以及内部值对象?

I'm new in event sourcing, and I started a new project to learn about it but I have a couple of questions about architecture.

My first question is about aggregate root. I have a UserAggregateRoot. Did this aggregate should contain properties that are typically recognizable to user entities?

I mean my Aggregate looks like that

public class UserAggregateRoot : AggregateRoot
{
    public Login Login { get; private set; }
    public Password Password { get; private set; }
    public Name Name { get; private set; }
    public DateTime BirthDate { get; private set; }
    public UserId UserId { get; private set; }
    public Status Status { get; private set; }

    private void Apply(UserCreatedEvent e)
    {
        Version = e.Version;
        Login = e.Login;
        Password = e.Password;
        Name = e.Name;
        BirthDate = e.BirthDate;
        Key = e.Key;
    }

    private void Apply(UserActivatedEvent e)
    {
        Version = e.Version++;
        Status = e.Status;
        Key = e.Key;
    }

    private void Apply(UserDeletedEvent e)
    {
        Version = e.Version++;
        Key = e.Key;
    }

    private void Apply(UserLogedEvent e)
    {
        Version = e.Version++;
        Key = e.Key;
    }

    private void Apply(UserUpdatedEvent e)
    {
        Version = e.Version++;
        Login = e.Login;
        Password = e.Password;
        Name = e.Name;    
        Key = e.Key;
     }
} 

Or maybe UserAggregateRoot should store user entity looks likes that

public class UserAggregateRoot : AggregateRoot
{
    public User User { get; private set; }

    private void Apply(UserCreatedEvent e)
    {
        User = e.User;
        Key = e.User.Key;
    }

    private void Apply(UserActivatedEvent e)
    {
        Version = e.Version++;
        User = e.User;
        Key = e.User.Key;
    }

    private void Apply(UserDeletedEvent e)
    {
        Version = e.Version++;
        Key = e.User.Key;
        User = e.User;
    }

    private void Apply(UserLogedEvent e)
    {
        Version = e.Version++;
        Key = e.User.Key;
    }

    private void Apply(UserUpdatedEvent e)
    {
        Version = e.Version++;
        User = e.User;
        Key = e.User.Key;
    }
}

Second question is if UserAggregateRoot stores some orders, should I store the order entity list inside this UserAggregateRoot?

What if orders are AggregateRoot something like OrderAggregateRoot? Should I store AggregateKey inside my UserAggregateRoot to point this OrderAggregateRoot?

Third question is should Aggregate root have business logic inside or should business logic be inside entity passed to this Aggregate and inside value objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你曾走过我的故事 2025-01-18 02:49:31
  1. 无论哪种方式都可以,我认为
  2. 如果没有更多的领域知识就很难回答。它很可能是一个 OrderAggregateRoot,其属性包含 UserAggregateRootAggregateKeyOrderAggregateRoot 甚至可能是完全不同的有界上下文。如果您想了解有关定义聚合根的更多详细信息,我建议您寻找定义事务边界。
  3. 取决于您所说的业务逻辑及其特定范围的含义,但实体和值对象可以有自己的行为。
  1. Either way is fine, as I see it
  2. Hard to answer without more domain knowledge. Most likely it'd be an OrderAggregateRoot with a property that contains the AggregateKey of the UserAggregateRoot. OrderAggregateRoot might even be an entirely different bounded context. If you want more details on defining aggregate roots I suggest looking for defining transactional boundaries.
  3. Depends on what you mean by business logic and its specific scope, but Entities and Value Objects can have behaviors of their own.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文