PRACTICAL PROGRAMMING ADVICE

Dynamic Command Dispatching In C#

Let’s keep this one short and sweet

Nicklas Millard
The Startup
Published in
4 min readMay 25, 2020

--

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.

--

--

Nicklas Millard
The Startup

I mostly write to "future me" sharing what I learn and my opinion on software development practices. youtube.com/@nmillard | open for contracts in Jan 2026.