-
Notifications
You must be signed in to change notification settings - Fork 10
Home
A. Shafie edited this page Apr 19, 2025
·
7 revisions
LiteBus is a lightweight, in-process mediator implementing Command Query Separation (CQS) patterns in .NET applications. It provides a structured approach to handling commands, queries, and events.
Key characteristics:
- Developed in .NET 8
- No external dependencies
- Reduced reflection usage
- Polymorphic message dispatch with covariance/contravariance support
- Modular architecture with separated abstractions and implementations
Commands request system state changes:
- Single handler per command type
- Can be result-producing (
ICommand<TResult>
) or void (ICommand
) - Processed exactly once
- Named with imperative verbs (e.g.,
CreateProductCommand
)
Queries retrieve data without changing state:
- Single handler per query type
- Always return results (
IQuery<TResult>
) or result streams (IStreamQuery<TResult>
) - Side-effect free
- Named with noun phrases (e.g.,
GetProductQuery
)
Events notify about system occurrences:
- Multiple handlers allowed (or none)
- No required return value
- Asynchronous processing
- Named with past tense verbs (e.g.,
ProductCreatedEvent
) - Can use POCO objects without framework interfaces
LiteBus implements the Mediator pattern to decouple components by channeling communication through mediators:
- Message senders don't need direct handler references
- Handlers focus on specific tasks
- Pipeline processing with pre/post/error handling capabilities
-
ICommand
/ICommand<TResult>
: State-changing operations -
IQuery<TResult>
: Data retrieval operations -
IStreamQuery<TResult>
: Stream-based data retrieval -
IEvent
: Notifications
- Pre-handlers for validation, logging, enrichment
- Post-handlers for notifications, cleanup
- Error handlers for exception management
- Covariant type handling for inheritance hierarchies
- Handler ordering control
- Tag-based handler filtering
- Contextual data sharing between handlers
- Execution flow control (abort capabilities)
- Cancellation support
- Microsoft Dependency Injection support
- Assembly scanning for automatic registration
- Command Module: State-changing operations
- Query Module: Data retrieval operations
- Event Module: Notification broadcasting
- Messaging Module: Core foundation for all types
- Message sent to mediator
- Pre-handlers execute
- Main handler processes message
- Post-handlers execute
- Error handlers catch exceptions
- Result returned (if applicable)
- Core Concepts
- Event Contracts
- Event Handlers
- Event Mediator/Publisher
- Advanced Features
- Best Practices
- Execution Context
- Handler Tags
- Handler Ordering
- Testing with LiteBus
- Performance Considerations
- Best Practices