NCQRS:你如何从域加载?

时间:2011-11-15 21:57:43

标签: c# design-patterns cqrs ncqrs

采取标准注册流程:

  1. 用户注册

  2. 向用户发送带有链接激活帐户的电子邮件

  3. 用户激活帐户

  4. 我正在谈论的问题是:

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

    • 当用户点击激活密钥链接时,我们使用readmodel验证密钥

    • 然后我们触发传递用户名

    • 的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();
    }  
    

1 个答案:

答案 0 :(得分:2)

您可以发布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, ... })
}