Generic Command Handlers

Is there a way to create a generic command handler to use a base
class? For example, to have a hierarchy of the following:

AbstractTask
+- SimpleTask
+- ComplexTask

So then to dispatch either a "SimpleTask" or "ComplexTask" and to be
handled at the "AbstractTask" level. When I implement such a command
handler I get a NoHandlerForCommandException after I dispatch a
"SimpleTask".

My method signature looks like the following:

@CommandHandler
public void process(AbstractTask task) {...}

Thanks,
Seamus

Hi Seamus,

unfortunately, the way the SimpleCommandBus is set up doesn’t allow this. The problem is that you need to register each command handler, which includes the type of command the handler can process. The SimpleCommandBus only evaluates these types using type equality.

My philosophy was that there should always be exactly on command handler for an incoming command. If there are more than one, the behavior would be undefined otherwise.

I can image a dispatching mechanism similar to the events could be useful here (first, try an exact match, then try to match on super types of the command). This will probably be an option that you need to set explicitly on the command bus.

Do you think this option will solve your problem?

Cheers,

Allard

Hi Allard,

I agree with the direction you are taking with commands in having a
single handler. Before I sent my first message I went through the
SimpleCommandBus source and found that it only looked at the current
class. I was hoping that I had overlooked something. :slight_smile:

I reviewed the option of extending SimpleCommandBus to add the class
hierarchy to the dispatch/subscribe/unsubscribe methods. However, I
only have this one requirement in which a single command handler would
be ideal. To that end I just wrapped my "Task" implementations into a
"WrappedTask" which is handled and unwrapped.

Thank you for your response and ideas.

Seamus