IntegrationEvent Base Class
All integration events inherit from theIntegrationEvent base record, which provides common properties for event tracking and identification.
Definition
Properties
Unique identifier for each event instance. Generated automatically when the event is created.
Timestamp when the event was created. Captured at the moment of instantiation.
Fully qualified type name including assembly information. Used for event routing and deserialization.
Design Patterns
Record Type
Integration events use C# records for several benefits:- Immutability: Events shouldn’t change after creation
- Value equality: Compare events by their content, not reference
- Concise syntax: Automatic property generation
- With expressions: Easy event copying with modifications
Auto-Generated Properties
The base class uses expression-bodied members for automatic property generation:Creating Custom Events
To create a new integration event:1. Define the Event
Create a new record inheriting fromIntegrationEvent:
2. Publishing Events
Use MassTransit’sIPublishEndpoint to publish events:
3. Consuming Events
ImplementIConsumer<TEvent> to handle events:
Best Practices
Event Naming
Event Naming
Use past tense for event names since they represent something that already happened:
- ✅
OrderCreatedEvent - ✅
PaymentProcessedEvent - ❌
CreateOrderEvent - ❌
ProcessPaymentEvent
Event Granularity
Event Granularity
Keep events focused and specific:
- Do: Create separate events for different business actions
- Don’t: Create generic events that handle multiple scenarios
- Events should represent a single business fact
Event Versioning
Event Versioning
Plan for event evolution:
Event Properties
Event Properties
Include all necessary data in the event:
- Consumers should not need to call back to the publisher
- Include IDs for related entities
- Avoid large payloads (use references instead)
Context Information
MassTransit provides rich context information throughConsumeContext:
Next Steps
BasketCheckoutEvent Example
See a complete integration event implementation with publishing and consuming
