Can a command contain a DomainService field or a Supplier<T> field

can a command contain a DomainService field or a Supplier field?

Like this:

class AddMemberCommand(
        @TargetAggregateIdentifier
        val groupId: String,
        val userId: String,
        val userFullName: String,
        val memberCount: Supplier<Int>,
        val memberService: MemberService
)
I need member count from Supplier<Int> or MemberService to evaluat whether additions are allowed.
Is this appropriate?

Technically this could work though I would advise against this approach:

- it seems MemberService is an implementation detail that is used by
the command handler to evaluate some business rules. Why would you
want this detail to be part of the contract to your domain?
- secondly, in case you need to serialize your commands in the future
(e.g. distributing your model across the network) this will lead to
further problems as you cannot simply serialize MemberService

That said, simply try to inject MemberService into your Command-Handler, e.g.

@CommandHandler
void handle(AddMemberCommand cmd, MemberService service) { ... }

Hi Michael
Thanks for your response.I am poor in English and use the translator. Hope there will be no impoliteness in the following. I sincerely ask for help.

Why would you want this detail to be part of the contract to your domain?
I have two Aggregate (Group and Member), the AddMemberCommand is handle by Group. Group need to limit Member to no more than 2000, Group implement this business rule need to get the number of Member by MemberService.
MemberService get the number of Member by HTTP on Query endpoint

That said, simply try to inject MemberService into your Command-Handler, e.g.
@CommandHandler
void handle(AddMemberCommand cmd, MemberService service) { … }
I don’t understand how the MemberService inject into, is use MetaData?

在 2019年4月27日星期六 UTC+8上午3:52:17,Michael写道:

Hi Jerry,

the English came across fine. I hope this message also gets properly translated back.

There are a lot of questions I have around the design. Why is there a “Group” aggregate and a “Member” aggregate? I understand they are conceptually different entities and in terms of information, capture different aspects. However, in command processing, the model should be based on expected behavior. In your case, you expect that adding a member to a group with 2000 members will fail. Ideally, that means that the Group aggregate has something inside that knows when there are 2000 members.

One way to achieve this is by modelling something that represents Member inside the Group. Whether that is really a solution for you, I wouldn’t be able to say. It depends on many factors.

Another way, and that might be the simplest solution from here, is by executing a query or invoking a service that will give you the number of members inside the group.
If you use Spring, then you can simply add your MemberService as a parameter, just like Michael suggested. Axon will take the Spring Bean when invoking your method.
If you don’t use Spring, you can register an instance in the Axon Configuration API as follows:
Configurer configurer = // define your configuration
configurer.registerComponent(MemberService.class, c-> new MemberService()); // instead of creating “new”, you will probably want to inject your existing service instance.

Any components registered in the Configuration, are available for use as parameters in @CommandHandler methods.

I hope this helps (and not too much is lost in translation :wink: ).
Kind regards,

HI Allard
Thanks for your response, it’s helpful to me.

I modelling the Member as a AggregateMember List in the Group before, and i like this solution, but then i think group maybe have 2000 members, is it too big? I worry that this will become a performance bottleneck when group loading and then I modelling the Member as a Aggregate.
I am a DDD newbie, Member should be a Aggreagte or a AggregateMember of Group? this makes me hesitate

在 2019年5月1日星期三 UTC+8下午6:37:21,Allard Buijze写道: