This requires me to have the idProperty set in my command. Otherwise, I will get a framework error.
I do not think this is ideal for at least 2 reasons:
My command may be malformed, but I want to be able to handle the error inside my command handling method to output a functional exception.
It may be (especially with more than 1 entity to be injected) that a an absent entity is not an invalid state. An event may have nullable relations in a DCB context, it requires only 1 non-nullable tag (if even that).
I encounter both of these reasons in our use cases.
So what I would like to do is the following:
data class Command(val property: String?, ...)
@CommandHandler
fun handle(
command: Command,
@InjectEntity(idProperty = "property") state: State?, // nullable
eventAppender: EventAppender,
) {
if(state==null) {
// handle nullability
}
}
Where the state resolves to `null` (or empty, if you want to go the optional route, which may make more sense in Java) in case the property in the command is null.
Is there in the current implementation a workaround for this? I have tried to build something, but I’m running into issues every time.
The @InjectEntity should be nullable already! By supporting that, a stateful command handler can represent the old “create-if-missing” logic:
Is the given entity null? Then we create.
Is the given entity not null? Then we update.
However, the thing that’s currently not supported, is “not” fail on the idProperty not existing in the handled command.
So, to your point:
My command may be malformed, but I want to be able to handle the error inside my command handling method to output a functional exception.
I am not sure how Axon would be able to differentiate for the user between intended null, because not created, and faulty null, because the command was malformed. Differently put, I’d be hesitant to have the idProperty support this out of the box.
What we could do, is introduce a EntityIdResolver, which is configurable for the @InjectEntity as well, that supports the malformed format.
—
However, lot’s of talk. Let me hear what you think, Maarten-Jan!
I’m not entirely sure I follow. With ‘we’ you mean the framework? I cannot end up with an injected entity being null when inside my command handler, or can I? If so, How?
This would be great! My thinking was to add an extra flag to the @InjectEntity, something like ‘throw exception if id cannot be resolved’,with the default being true.
If the @InjectEntity cannot be null, then we have a bug in Axon Framework, for sure
Sweet, glad to hear! Perhaps, if you’re up for it, you can write an issue for this with us.
Actually, if the nullability of @InjectEntity doesn’t work, that’s two issues: one bug and one feature request