DKNet

Messaging & CQRS

The Messaging & CQRS components provide a lightweight, efficient alternative to MediatR using SlimMessageBus, implementing Command Query Responsibility Segregation (CQRS) patterns and event-driven architecture for Domain-Driven Design applications.

Components

Architecture Role in DDD & Onion Architecture

The Messaging components implement CQRS patterns that span multiple layers of the Onion Architecture, enabling clean separation between read and write operations:

┌─────────────────────────────────────────────────────────────────┐
│                    🌐 Presentation Layer                        │
│                   (Controllers, API Endpoints)                  │
│                                                                 │
│  Sends: Commands and Queries via IMessageBus                   │
│  Receives: Results and DTOs from handlers                      │
└─────────────────────────┬───────────────────────────────────────┘
                          │
┌─────────────────────────┴───────────────────────────────────────┐
│                   🎯 Application Layer                          │
│              (Command/Query Handlers, Use Cases)               │
│                                                                 │
│  📨 Command Handlers - Write operations (IHandler<TRequest>)   │
│  📊 Query Handlers - Read operations (IQueryHandler<TQuery>)   │
│  🔄 Event Handlers - Cross-cutting concerns                    │
│  ⚡ Auto-save behavior - EF Core integration                   │
└─────────────────────────┬───────────────────────────────────────┘
                          │
┌─────────────────────────┴───────────────────────────────────────┐
│                    💼 Domain Layer                             │
│           (Entities, Aggregates, Domain Services)              │
│                                                                 │
│  🎭 Domain Events - Aggregate communication                    │
│  📝 Business Logic - Pure domain operations                    │
│  🏷️ No messaging dependencies - Clean separation               │
└─────────────────────────┬───────────────────────────────────────┘
                          │
┌─────────────────────────┴───────────────────────────────────────┐
│                 🗄️ Infrastructure Layer                        │
│                  (Message Bus, Event Dispatching)              │
│                                                                 │
│  🚌 SlimMessageBus - Message routing and delivery              │
│  📡 Event Publishers - Domain event dispatching                │
│  🗄️ DbContext Integration - Automatic change persistence       │
└─────────────────────────────────────────────────────────────────┘

Key CQRS Patterns Implemented

1. Command Query Separation

2. Event-Driven Architecture

3. Pipeline Behaviors

4. Messaging Patterns

DDD Implementation Benefits

1. Aggregate Coordination

2. Domain Event Integration

3. Clean Architecture

4. Scalability Patterns

Onion Architecture Benefits

1. Dependency Inversion

2. Testability

3. Separation of Concerns

4. Technology Independence

Integration Patterns

1. Command Processing

// Command definition
public record CreateOrderCommand(Guid CustomerId, List<OrderItem> Items) 
    : Fluents.Requests.IWitResponse<Result<Guid>>;

// Command handler
public class CreateOrderHandler : Fluents.Requests.IHandler<CreateOrderCommand, Result<Guid>>
{
    public async Task<IResult<Result<Guid>>> Handle(CreateOrderCommand command)
    {
        // Business logic here
        // Auto-save handled by EfAutoSavePostProcessor
    }
}

2. Query Processing

// Query definition
public record GetCustomerOrdersQuery(Guid CustomerId, int Page, int PageSize) 
    : Fluents.Queries.IWitPageResponse<OrderSummaryDto>;

// Query handler
public class GetCustomerOrdersHandler : Fluents.Queries.IPageHandler<GetCustomerOrdersQuery, OrderSummaryDto>
{
    public async Task<IPagedList<OrderSummaryDto>> Handle(GetCustomerOrdersQuery query)
    {
        // Optimized read logic here
    }
}

3. Event Handling

// Domain event
public record OrderCreatedEvent(Guid OrderId, Guid CustomerId, decimal TotalAmount);

// Event handler
public class OrderCreatedEventHandler : IEventHandler<OrderCreatedEvent>
{
    public async Task Handle(OrderCreatedEvent evt)
    {
        // Cross-cutting concerns (notifications, logging, etc.)
    }
}

Performance Features

1. SlimMessageBus Advantages

2. EF Core Integration

3. Caching Support

Security & Compliance

1. Authorization Integration

2. Data Protection