.netdesign-patternsdomain-driven-designcqrs

CQRS - Should CommandHandlers Invoke other CommandHandlers


Just trying to get some opinions on whether or not CommandHandlers can/should communicate with other CommandHandlers.

Here is a simple case I just ran into. I have a ChangePasswordCommandHandler who's command looks like the following:

public class ChangePasswordCommand : Command
{
    public string Email { get; }
    public string OldPassword { get; set; }
    public string NewPassword { get; set; }
}

So, inside the handler I need to validate the users olds password, so as I see it I have three options:

  1. dispatch out a call to my ValidateCredentialsCommandHandler.
  2. factor out some of the validation logic into a service that both handlers can use.
  3. have the calling process do this check first, but now business logic is leaking outside of my domain layer.

I'm running into a few technical issues with dispatching to other handlers mostly b/c i'm using a transaction per-web request, so i have two transactions trying to contend.

Thoughts?


Solution

  • Command handler handles command. If ChangePasswordCommandHandler dispatches validation to ValidateCredentialsCommandHandler, what command does ValidateCredentialsCommandHandler handles then?

    In short - no, i don't think that makes sense.

    2nd option sounds best from ones You mentioned.