vendredi 12 avril 2019

Handling batches of Commands

I have an architecture like the one described in this blog post. In summary, I have command objects, e.g. :

public class MoveCustomerCommand
{
    public int CustomerId { get; set; }
    public Address NewAddress { get; set; }
}

And command handlers for each command which derive from the interface ICommandHandler<TCommand>:

public interface ICommandHandler<TCommand>
{
    void Handle(TCommand command);
}

public class MoveCustomerCommandHandler : ICommandHandler<MoveCustomerCommand>
{
    public void Handle(MoveCustomerCommand command)
    {
        // Logic here
    }
}

Now I'm looking for a clean solution to the following use case: Some clients produce a batch of heterogenous commands that need to be processed. In other words, I want to implement the following:

void HandleBatch(List<ICommand> batch) {
}

I have some ideas but I'm not convinced that any of them is good enough.

Option 1 Put a humongous switch-case in the HandleBatch function.

Option 2 Use reflections to find the appropriate command handler for each command.

Option 3 Annotate the command handlers and select the appropriate annotated class for each command.

Option 4 Something else?

Another inconvenience is that the HandleBatch will have to have handy an instance of virtually every possible dependency since most of the logic of the application is in these commands. But I guess I can't go around this.

Aucun commentaire:

Enregistrer un commentaire