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.