0

I have a Provision, this Provision has state with relative constraints. For example: I can accept a Provision only if its actual state is authorized.

Since this is clearly a business rule I code it inside my domain model:

public void Accept()
{
    if (this.State == ProvisionsState.Authorized)
        this.State = ProvisionsState.Accepted;
    else
        throw new InvalidOperationForProvision("The provision have to be authorized.");
}

So far so good, but... I have a command (a sort of DTO with only the ProvisionId) and a relative handler. When a client wants to accept a provision will put AcceptCommand DTO on a bus. Right now the AcceptCommandHandler take this command from the bus and handle it.

public void Handle(AcceptCommand command)
{
    var provision = Repository.GetById(command.ProvisionId);
    provision.Accept(); 
    ...
}

If the InvalidOperationForProvision isn't raised everything will be ok and ProvisionAcceptedEvent will be send. So far so good (as far as I know). But the question is: what happens if exception is raised?

Bearing in mind that some bus will retry the command many times, even if the command will surely fail (eg: a disabled provision will never be authorized, so I will never accept it, but the accept command will still be there).

BAD_SEED
  • 4,840
  • 11
  • 53
  • 110

1 Answers1

0

Considering that you intend to process the command asynchronously, InvalidOperationForProvision exception must be handled in the command-handler itself. Some kind of compensating action might be required.

Having said that, its probably a good idea to do some validation, if possible, on the command itself before putting it on the Bus and reject it if it is not good enough. For example, if you happen to have access to the ProvisionsState before issuing the command, do your check and accept or reject the it accordingly. Hence, you would be able to give immediate feedback to the user.

http://codingcraft.wordpress.com/2014/07/05/application-service-design-with-commands/

http://www.udidahan.com/2009/12/09/clarified-cqrs/

Anshuman
  • 39
  • 2