I have a class which has a number of overloaded methods:
public class CustomerCommandHandlers
{
public void Handler(ChangeNameCommand command)
{
...
}
public void Handler(ChangeAddressCommand command)
{
...
}
}
I have included the following method:
public void Handle<TCommand>(TCommand command) where TCommand : ICommand
{
Handler((dynamic)command);
}
which allows me to call the overloaded methods from another class that registers the command and the command handler.
However, as I create other commandHandlers such as ProductCommandHandlers, InventoryCommandHandlers etc, I do not want to include the dynamic method in each class.
Is there a way I can create a base class for every command handler which contains this method and then I can call this method from the base class?
Thanks
If you're already using dynamics, you might as well try this as a base class:
public class Basehandler
{
public void Handle<TCommand>(T command) where TCommand : ICommand {
((dynamic)this).Handler(command);
}
// As fallback if there is no implementation for the command type
public void Handler(ICommand val) {
// You could implement default or error handling here.
Console.WriteLine(val == null ? "null" : val.GetType().ToString());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.