NCQRS: how do you load from the domain?

251 views Asked by At

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

There are 1 answers

17
Dennis Traub On BEST ANSWER

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, ... })
}