What is the purpose of maintaining a Domain Event array? #116
-
In the User domain, inside the static create(create: CreateUserProps): UserEntity {
const id = v4();
const props: UserProps = { ...create, role: UserRoles.guest };
const user = new UserEntity({ id, props });
user.addEvent(
new UserCreatedDomainEvent({
aggregateId: id,
email: props.email,
...props.address.unpack(),
}),
);
return user;
} My question is what is the purpose of adding these into an array? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
When a user is created, you may want other parts of the system to know about it, that's why we are emitting Domain Events. |
Beta Was this translation helpful? Give feedback.
When a user is created, you may want other parts of the system to know about it, that's why we are emitting Domain Events.
It is an array because there may be multiple events emitted, for example if you execute multiple methods, each one may add an event. Or if one method emits multiple events, for instance when created user is an admin you may also want to emit a second event like
AdminCreatedDomainEvent
, to prevent your event listeners that are interested only in admins to subscribe for everyUserCreatedDomainEvent
you can listen to the admin events only.