Becoming a software developer – episode XIII

Becoming a software developer – episode XIII

Welcome to the thirteenth episode of my course “Becoming a software developer” in which we will make use of the Command Handler pattern in order to extend our business logic and clean up the controllers.

All of the materials including videos and sample projects can be downloaded from here.
The source code repository is being hosted on GitHub.



  • Command Handler
  • Command Dispatcher


Command Handler

We can encapsulate our business logic even more by using the ICommand interface which is a common way to start using the CQS (Command & Query separation) pattern in our application.
The interface per se is merely a “marker” and can be defined as follows:

Having ICommand in place, we can define as many commands as we want to:

Eventually, we can add define the generic ICommandHandler interface:

And implement the handler for our commands, like this:

Here comes the question – why would you even bother to do that? Usually, the business logic consists more than a single operation to be invoked in order to complete its flow.
Command handlers are a great way to achieve such goal, as we’re able to inject as many services as we need to. Otherwise, we would have to create a separate interfaces dedicated for a single business logic unit, which would do exactly the same (or in the worst case scenario, write such code within our controllers that should be as transparent as possible).

Command Dispatcher

Now, how can we enforce our software to automatically wire up commands to the particular command handlers? And even more importantly, how can we order our controllers, to resolve the proper command handler? At first, let’s define the ICommandDispatcher interface along with its implementation:

Notice that we’re making use of IComponentContext which is a part of the AutofacAutofac IoC container.
Next step, is to register all of the commands and the appropriate command handlers:

And finally, we can inject the dispatcher into our controller and redefine our HTTP requets to look as simple as that:


In the next episode, you’ll find out how to configure the application by passing the configuration classes that can be defined and mapped from the appsettings.json.
Moreover, you shall see my efforts in trying to find out why something didn’t work as expected in the first place ;).

1 Comment Becoming a software developer – episode XIII

  1. Pingback: Dew Drop - April 20, 2017 (#2462) - Morning Dew

Leave A Comment

Your email address will not be published. Required fields are marked *