Many commands which look similar

Hello everybody,

I have a question regarding the commands and command handlers. In my project I have a large number of classes which are look almost the same, something like in example below:

public class SomeClass {

private String id;

private String name;

private List list;


and I have only a small number of commands to process CreateObject, RenameObject, DeleteObject, AddValueToList, RemoveValueFromList, but of course I want to do this for every class type. Imagine that I have 20 different classes, then (since generic commands are not supported by command bus) I need to create 20 command handlers and 20*6 different commands. It’s not difficult, but this is something that I want to avoid.

My idea was to implement only 6 generic commands, but include the class type to each command, so for example the CreateObject command will look as follows:

public class CreateObjectCommand {


public final String rootAggregateId;

public final String name;

public final Class<?> aggregateType;


then I can have only one command handler, which must be able understand the “aggregateType” and create an object of specified type.

Is it actually a good approach? Does anyone had similar problem? What you would suggest to do in that case?

Thank you,


Hi Vitali,

you say “generic commands are not support by command bus”, but actually they are. You can use the exact same payload type for different commands. Each command also has a name, which defaults to the fully qualified classname of the payload. However, you can choose any name you like. In your @CommandHandler annotation, you have a parameter “commandName”, where you can specify your name.
Downside of this approach is that you cannot use the CommandGateway. They don’t support custom names.

You approach doesn’t seem too bad either, although I wouldn’t expose the aggregateType directly. Instead, you could use an enum with possible types, instead of using the class itself.