事件溯源聚合根用户
Event Sourcing Aggregate Root User
我是事件溯源方面的新手,我开始了一个新项目来了解它,但我有几个关于架构的问题。
我的第一个问题是关于聚合根的。我有一个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
中?
如果订单 AggregateRoot
类似于 OrderAggregateRoot
怎么办?我应该将 AggregateKey
存储在我的 UserAggregateRoot
中以指向此 OrderAggregateRoot
吗?
第三个问题是 Aggregate
root 是否应该在内部有业务逻辑,或者业务逻辑应该在传递给此 Aggregate
的实体内部和内部值对象中?
- 我觉得哪个都好
- 没有更多的领域知识很难回答。最有可能的是
OrderAggregateRoot
和包含 UserAggregateRoot
的 AggregateKey
的 属性。 OrderAggregateRoot
甚至可能是一个完全不同的限界上下文。如果您想了解有关定义聚合根的更多详细信息,我建议您寻找定义事务边界。
- 取决于您所说的业务逻辑及其特定范围的含义,但实体和值对象可以有自己的行为。
我是事件溯源方面的新手,我开始了一个新项目来了解它,但我有几个关于架构的问题。
我的第一个问题是关于聚合根的。我有一个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
中?
如果订单 AggregateRoot
类似于 OrderAggregateRoot
怎么办?我应该将 AggregateKey
存储在我的 UserAggregateRoot
中以指向此 OrderAggregateRoot
吗?
第三个问题是 Aggregate
root 是否应该在内部有业务逻辑,或者业务逻辑应该在传递给此 Aggregate
的实体内部和内部值对象中?
- 我觉得哪个都好
- 没有更多的领域知识很难回答。最有可能的是
OrderAggregateRoot
和包含UserAggregateRoot
的AggregateKey
的 属性。OrderAggregateRoot
甚至可能是一个完全不同的限界上下文。如果您想了解有关定义聚合根的更多详细信息,我建议您寻找定义事务边界。 - 取决于您所说的业务逻辑及其特定范围的含义,但实体和值对象可以有自己的行为。