Reputation:
To disable a profile associated to a user we usually
User.RemoveProfile(ProfileId)
method.Profile.Disable()
methodProfile.Disable()
we raise the ProfileDisabled
event.Currently events are stored inside each entity. When we persist users in the db we go through events under user and NOT under all associated profiles. As a result, we don't dispatch profile events and they get lost.
For your reference this is the code we use to dispatch event:
public abstract class Repository
{
private readonly IMediator _mediator;
protected Repository(IMediator mediator) => _mediator = mediator;
// Future: Use EventStore for audit
/* Bug: Disable profile raises an event stored at the profile level. When user gets updated only
user events are dispatched. Profile events are lost... */
protected async Task DispatchEventsAsync(Entity entity, CancellationToken token)
{
Ensure.Any.IsNotNull(entity, nameof(entity));
await Task.WhenAll(entity.Events.Select(e => _mediator.Publish(e, token)));
entity.SuppressEvents();
}
}
Where Entity
is:
public interface IEntity { }
public abstract class Entity : IEntity
{
private readonly List<DomainEvent> _events = new List<DomainEvent>();
[NotMapped]
public IReadOnlyList<DomainEvent> Events => _events.AsReadOnly();
public bool HasEvents => _events.Any();
protected void Emit(DomainEvent eventItem)
{
if (eventItem != null && !_events.Contains(eventItem))
_events.Add(eventItem);
}
public void SuppressEvents() => _events.Clear();
}
As you can see Entity does NOT know about related entities. This there is no way to access related events.
How would handle this case? Does this mean only Aggregate Root can raise events?
Thx Seb
Upvotes: 1
Views: 564
Reputation: 4754
I guess you have an aggregate with two entities: User and Profile. And User is the AR.
Events are raised when you perform an operation on the aggregate, so you should store them with the AR.
In your case the operation is "user.removeProfile(profileId)", so the AR should raise the event "ProfileRemovedFromUser" or something like that.
NOTE: Your UL is ambiguous, as you use the words remove and disable for the same thing. You should use just one word.
Upvotes: 1