The Outbox pattern ensures that domain events are reliably published even if the message broker is temporarily unavailable.
TaskHub.Shared integrates the Outbox pattern directly into the EF Core transaction flow via UnitOfWorkBase.
SaveAsync is called on the Unit of Work, it scans the EF Core Change Tracker for any entities implementing IAggregate that are in a Added or Modified state.IOutboxMessageFactory, it converts each domain event into an OutboxMessage.OutboxMessage records are added to the same database transaction as your business data.Ensure the Outbox is enabled in your appsettings.json.
"Persistence": {
"Outbox": {
"IsEnabled": true,
"TableName": "OutboxMessages"
}
}
Ensure your domain entities inherit from AggregateBase<T>.
public class Order : AggregateBase<Guid>
{
public void Complete()
{
// ... logic
AddEvent(new OrderCompletedDomainEvent(Id));
}
}
Use UnitOfWorkBase as your base class. The SaveAsync method handles everything automatically.
await _unitOfWork.SaveAsync(ct); // Business data + Outbox messages saved in one transaction
The TaskHub.Shared.Outbox.EntityFramework module provides the repository for reading and writing these messages. A separate background worker (or a recurring job) should use IOutboxReader to fetch pending messages and publish them to your message broker.