Within my application service, I have the following code for publishing domain events:
var document = await dbContext.Documents.GetAggregateAsync(message.DocumentId);
publisher.SubscribeTo<DocumentOwnerChanged>()
.UsingDelegate(
async a => await messageGateway.DocumentOwnerChanged(1, 1, 1));
document.ChangeOwner(message.OwnerId);
await dbContext.SaveChangesAsync();
await publisher.Publish(document.ReleaseEvents());
I'm trying to decide if I like having this knowledge of publishing events within the app service or if I should externalize this somewhere up higher in the root.
thoughts?
You would typically register handlers in the Composition Root, unless you had to dynamically register and un-register handlers based on other messages.
There is some discussion around this here
You would publish domain events typically in your domain layer:
public void SomeDomainBehaviour()
{
// do something domain-y
DomainEvents.Publish(new DomainEvent());
}
Jimmy Bogard discusses other ways of publishing Domain Events here