For example, I have the following code:
public void Update(string firstName,
string? middleName,
string lastName,
string emailAddress,
string phoneNumber,
DateTime birthDate,
Gender gender,
string comment,
string? avatarUrl = null)
{
SetFullName(firstName, lastName, middleName);
SetEmail(emailAddress);
SetPhoneNumber(phoneNumber);
SetBirthDate(birthDate);
SetGender(gender);
if (avatarUrl is not null)
{
SetAvatarUrl(avatarUrl);
}
SetComment(comment);
AddPersonUpdatedDomainEvent();
}
At the end of the update, I add a domain event for the update, but anyone can call methods like SetFullName
, SetEmail
, SetPhoneNumber
separately, so I would need to handle domain events there as well. However, this creates a problem because calling one complete update results in 8 domain events being added, which doesn't seem right. Ultimately, I only need to handle one "update" event.
Is it acceptable to remove/update previously added update events if they already exist?
There are multiple ways to achieve this:
My preference is to have more granular events for more control and I will probably not fire the general "AddPersonUpdatedDomainEvent" event, but as I say that may vary from application to application.
In summary I think there is strict rules how to do this, but it may depend of application needs.