NCQRS:如何从域加载?

发布于 2024-12-15 18:03:08 字数 797 浏览 2 评论 0原文

采用标准注册流程:

  1. 用户注册

  2. 用户收到包含激活帐户链接的电子邮件

  3. 用户激活帐户

我所说的问题是:

  • 当我们创建初始帐户时,我们存储用户名、密码、电子邮件、激活密钥

  • 当用户单击激活密钥链接时,我们使用读取模型验证密钥

  • 然后触发传入用户名的ActivateAccountCommand

如何加载用户帐户以在域中激活它?


最初我想将新用户 Acount.Id 传递给 readmodel,但 CommandExecutorBase 中没有访问权限(据我所知) - 我们不保存此内容:

protected override void ExecuteInContext(IUnitOfWorkContext context,
       CreateAccountViaFormRegistrationCommand command)
{
    var newKey = Guid.NewGuid().ToString();
    var newAccount = new Account(
            command.UserName, command.Email, command.Password, newKey);
    SendWelcomeEmail(command.Email, command.UserName, newKey);
    context.Accept();
}  

Take the standard registration process:

  1. user signs up

  2. user is sent email with link activate account

  3. user activates account

the issue i'm talking about is:

  • when we create the initial account we store the username, password, email, activation key

  • when the user clicks the activation key link we validate the key using the readmodel

  • we then fire the ActivateAccountCommand passing in the username

how do i load the users account to activate it in the domain?


initially I wanted to pass the new users Acount.Id to the readmodel but there is no access (that i'm aware of) from within the CommandExecutorBase - we don't save this:

protected override void ExecuteInContext(IUnitOfWorkContext context,
       CreateAccountViaFormRegistrationCommand command)
{
    var newKey = Guid.NewGuid().ToString();
    var newAccount = new Account(
            command.UserName, command.Email, command.Password, newKey);
    SendWelcomeEmail(command.Email, command.UserName, newKey);
    context.Accept();
}  

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

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

发布评论

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

评论(1

亚希 2024-12-22 18:03:08

您可以发布一个 AccountActivationKeySent 事件,该事件又可以被处理以填充读取端所需的任何投影。大致如下:

// 1. Create and send the command from your client:

var command = new CreateAccountViaFormRegistrationCommand {
    AccountId = Guid.NewGuid(),
    ...
}

// 2. Create a new account in your command handler

var newAccount = new Account(command.AccountId, ...);
context.Accept();

// 3. And your account constructor (assuming it's inheriting from one
//    of EventSource's subclasses, e.g. AggregateRootMappedByConvention) 
//    might look like this:

public Account(Guid accountId, string name, ...) {
  EventSourceId = accountId;
  ApplyEvent(new AccountCreated { AccountId = Id, ... } );
  ApplyEvent(new AccountActivationSent { AccountId = Id, ... })
}

You can publish an AccountActivationKeySent event, which in turn can be handled to populate whatever projection you need on the read side. Something along the lines of:

// 1. Create and send the command from your client:

var command = new CreateAccountViaFormRegistrationCommand {
    AccountId = Guid.NewGuid(),
    ...
}

// 2. Create a new account in your command handler

var newAccount = new Account(command.AccountId, ...);
context.Accept();

// 3. And your account constructor (assuming it's inheriting from one
//    of EventSource's subclasses, e.g. AggregateRootMappedByConvention) 
//    might look like this:

public Account(Guid accountId, string name, ...) {
  EventSourceId = accountId;
  ApplyEvent(new AccountCreated { AccountId = Id, ... } );
  ApplyEvent(new AccountActivationSent { AccountId = Id, ... })
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文