I am currently reading about DDD and have a issue how to implement a certain validation.
Scenario:
I have an entity Group
that contains a list of Member
s which in turn consists of a User
and a MemberState
(Member
, Admin
). The entity has a method MakeAdmin(User user)
that turns a member into an admin.
The rule is: Only if the current user is admin of the group, it is allowed to turn the member to an admin.
I have three possible implementations in mind, with some caveats each.
Variant 1
Group
enitity gets a dependency IUserContext
injected via constructor to get the current user. With that the entity can check if the current user is admin and is allowed to switch the member state.
Caveat: I don't like that the domain entity has such a dependency injected and I don't think that is correct in a DDD view.
Variant 2
Method MakeAdmin()
gets an additional parameter which turns it into MakeAdmin(User user, User currentUser)
. That removes the requirement for the IUserContext
dependency in the entity.
Caveat: I am not sure if it is correct at all that the entity validates this rule, because it is not really an invariant of the entity.
Variant 3
Validate this rule in an application service, and only call MakeAdmin(User user)
if validation passed.
Caveat: I consider the rule domain specific, so I think it is not correct to put it in application layer.
So what would be the best option, or is there some totally different variant?