We're having a bounded context dealing with payments.
I'm trying to model case, where Merchant can create another merchant and inherit some of its properties in this case: users that are needed for authentication to another bounded context (payment processor) that acts as open host service with anti-corruption layer on our side.
class Merchant {
public Merchant registerSubMerchant(name, ..);
...
}
The problem is, that User is also an aggregate as Merchant is. The process will create an instance of merchant, stores it, but I also need to load all users from parent merchant, and assign them to newly created merchant and also generate new user for authentication against our system:
users.filter(user -> user.realm() == PAYMENT_PROCESSOR)
.forEach(user -> {
user.assignTo(merchantId);
userRepository.store(user);
});
User anotherUser = new UserForOurSystem(passwordGenerator).assignTo(merchantId);
userRepository.store(anotherUser);
Whole process should be (I suppose) in one transaction to be valid. Now here's the problem: I'm breaking the rule (and I guess also many other) that only 1 aggregate instance can be modified (or created?).
I know that I'm doing something wrong, but I'm not sure if users should be wrapped inside the Merchant aggregate or should know about some users and their passwords etc.. How can I model such situation?