
PRACTICAL PROGRAMMING ADVICE
Dynamic Command Dispatching In C#
Let’s keep this one short and sweet
So you’ve heard of the Open/Closed principal of SOLID — and Command Query Separation. But you’re not completely onboard with how to actually do this in practice.
Also, you’re aware that it’s considered a violation of OCP if you have to modify an existing class to accommodate new behaviour. The desired action would be to just create a new class that implements the new behaviour or functionality you’d like to add to the application.
In this article we’ll achieve the following
👉🏼 Dispatch commands that will be handled by some other “handler” class 👉🏼 Add new commands without modifying existing classes 👉🏼 Dynamically invoke handlers when a command is dispatched
🔔 Want more articles like this? Sign up here.
It’s all about interfaces
First, we’ll create a contract (interface) that handlers must adhere to.

ICommandHandler` simply requires the implementing class to expose a method called “Handle” which takes an argument of whatever type “TCommand” is. The only constraint is TCommand must be a class.

Here’s an example of a class that implements the ICommandHandler interface. We’re just writing out some text.
If you’re curious about the CreateAuthorCommand`, it looks like this

With the foundations in place, we’ll move on to what it’s all about: dynamically dispatching commands and allowing us to extend the application as much as we want.
Creating the dispatcher
We’ll have a so-called dispatcher — name it whatever you want — that will have the responsibility of receiving a command and find appropriate handler(s) for that specific command.

If you’re not used to reflection, this might look like greek to you. However, this is a must if you want to know how to add functionality without modifying existing classes.
Anyway… Simply put it’s a three step process
- We derive a type based on the ICommand interface and the generic method argument
- Find any concrete classes that implements the interface ICommandHandler
- Invoke “Handle” on the concrete handler class
How it works
On line 7 we get a type called BookStore.Application.ICommandHandler`1[TCommand]`. BookStore.Application is just the name of the assembly (.dll) that the ICommand interface lives in.
If you’re new to generics, then this reads as “ICommandHandler of TCommand”.
You see, this is not specific enough for us to use. We need the TCommand to not be generic, but instead be of a concrete type.
If we for instance call the Dispatcher using the CreateAuthorCommand`, we’ll need to derive a type that replaces the TCommand` with a concrete type. We do this on line 8 by calling .MakeGenericType(command.GetType())`. This effectively replaces ICommand with the command type passed in.
Now, on line 11 we search for any concrete classes that implements the type ICommandHandler<CreateAuthorCommand>`.
On line 18–20 we loop thru the list of found classes that implements the target interface, and try to instantiate the class. Activator.CreateInstance()` just calls the parameterless constructor of the class.
On line 22 we try to call the Handle class that we know the object will have, because it implements the interface ICommandHandler.

