Event Handling
Events enable loosely coupled communication between different parts of your application and with external systems. This architecture supports both domain events (internal) and integration events (external).Event Types
Domain Events
Domain events represent something that happened within your domain that other parts of the application care about. They:- Are published and consumed within the same application
- Represent domain-specific occurrences
- Are handled synchronously
- Inherit from
DomainEvent
Integration Events
Integration events communicate with external systems or microservices. They:- Are published to message brokers (RabbitMQ, etc.)
- Cross application boundaries
- Are handled asynchronously
- Inherit from
IntegrationEvent
Domain Events
Create a Domain Event
Create a domain event class that inherits from Example from codebase:
DomainEvent:Application/DomainEvents/DummyEntityCreated.cs:10-16Publish Domain Event from Use Case
Publish the domain event after a successful operation:Key method:
entity.To<YourEntityCreated>() maps the entity to the eventReference: Application/UseCases/DummyEntity/Commands/CreateDummyEntity/CreateDummyEntityHandler.cs:34Integration Events
Create Integration Event
Create an integration event class that inherits from Example from codebase:
IntegrationEvent:Application/Integrations/Events/DummyEntityCreatedIntegrationEvent.cs:5-20Create Integration Event Publisher
Create a publisher handler that converts domain events to integration events:Reference:
Application/Integrations/Handlers/Publishers/DummyEntityCreatedIntegrationEventHandlerPub.cs:5-21Create Integration Event Subscriber
Create a subscriber handler to consume integration events from external systems:Reference:
Application/Integrations/Handlers/Subscribers/DummyEntityCreatedIntegrationEventHandlerSub.cs:5-12RabbitMQ Configuration
Configure Connection String
Add RabbitMQ connection string toappsettings.json:
Template-API/appsettings.json:16-18
Register Event Bus
The event bus is registered in the infrastructure services:Event Flow Example
Here’s a complete example of how events flow through the system:1. User Creates Entity
2. Command Handler Creates Entity and Publishes Domain Event
3. Domain Event Handler Processes Event
4. Integration Event Published to RabbitMQ
5. External Services Consume Integration Event
Other microservices subscribed to the event receive and process it:Best Practices
When to Use Domain Events
When to Use Domain Events
Use domain events when:
- Multiple parts of your application need to react to domain changes
- You want to decouple handlers from use cases
- Side effects should happen after the main operation succeeds
- You need to maintain consistency within a single transaction
- Send email after user registration
- Update statistics after entity creation
- Invalidate cache after data modification
When to Use Integration Events
When to Use Integration Events
Use integration events when:
- Communicating with external systems or microservices
- Changes need to be propagated across service boundaries
- You need asynchronous, distributed processing
- Implementing event sourcing or CQRS read models
- Notify other microservices of data changes
- Sync data to reporting databases
- Trigger workflows in external systems
- Update search indexes
Event Naming Conventions
Event Naming Conventions
Follow these naming patterns:Domain Events:
- Past tense:
YourEntityCreated,OrderPlaced,PaymentProcessed - Specific to domain:
ProductAddedToCart,ShipmentDispatched
- Include context:
YourEntityCreatedIntegrationEvent - Use namespaced event types:
"Catalog.Product.Created"
Error Handling
Error Handling
Domain Events:
- Failures roll back the entire transaction
- Use try-catch to handle specific scenarios
- Log errors for debugging
- Implement retry logic for transient failures
- Use dead letter queues for failed messages
- Monitor and alert on processing failures
- Consider idempotency for duplicate messages
Testing Events
Unit Testing Event Publishing
Integration Testing with RabbitMQ
Next Steps
Docker Deployment
Deploy your application with Docker and RabbitMQ
Implementing Use Cases
Learn more about commands and queries