Skip to main content

Domaintrix.Mediator 🧩

Domaintrix.Mediator is a cross-layer infrastructure service that acts as a central orchestration point for commands, queries, notifications, validations, permissions, authentication, and events. It fully abstracts tools like MediatR, authentication systems, and event buses, allowing the application to operate in a decoupled and standardized way.


Why Use Domaintrix.Mediator? 🤔

1. Infrastructure Abstraction 🔌

The mediator allows you to switch underlying technologies (e.g., Auth0 to Microsoft Entra, or RabbitMQ to Kafka) without affecting business logic. It does this by exposing only abstract contracts for external integrations.

2. Command and Query Orchestration 🧠

Commands and queries follow a unified structure, with automatic validation and structured response handling. This eliminates repetitive logic in handlers and improves consistency across use cases.

3. Centralized Notification Handling 📢

All layers can register notifications (system or user-facing). These are centralized into a shared scope by the mediator and can be surfaced via HTTP or logs.

4. Automatic HTTP Response Translation 🌐

An additional component, ApiMediator, translates the mediator's state into a standardized HTTP response, with proper status codes and accumulated messages. This eliminates the need for controller-specific response logic.

5. Context-Based Authorization 🔐

The mediator extracts the authenticated user from HTTP headers and exposes values like PersonId, authentication state, and permissions. Authorization can be performed at any layer (Presentation, Application, Domain) without tight coupling to identity infrastructure.

6. Resilient Event Publishing 📬

Domain and integration events are published via the mediator. Internally, it uses a pluggable IEventBus interface. Errors during event publication are handled gracefully, converting exceptions into system notifications.


Usage Examples (.NET) 💻

🚀 Executing a command

public record CreateOrderCommand(Guid CustomerId) : ICommand;

await mediator.Exec(new CreateOrderCommand(customerId));

🔍 Executing a query with response

public record GetOrderQuery(Guid Id) : IQuery<OrderDto>;

var result = await mediator.Get(new GetOrderQuery(orderId));

✅ Validation using FluentValidation

public class CreateOrderValidator : AbstractValidator<CreateOrderCommand>
{
public CreateOrderValidator()
{
RuleFor(x => x.CustomerId).NotEmpty();
}
}

⚠️ Notifications

mediator.AddNotification("Invalid user name.", DomainNotificationType.UnprocessableEntity);

if (mediator.Blocked)
return;

🌍 Using ApiMediator

var response = await apiMediator.Exec(command); // returns IActionResult with a standardized response

📡 Publishing events

await mediator.Publish(new OrderCreatedEvent(orderId));

Technical Characteristics ⚙️

  • Abstract class implemented by consumers.
  • Registered as a scoped service in the DI container.
  • Uses IMediator from MediatR internally.
  • Integrates with ScopedNotifications for message flow control.
  • Publishes events via IDomainEventBus abstraction.
  • Usable in any layer: presentation, application, domain, or infrastructure.

Benefits 🎯

  • Infrastructure can be swapped without impacting business logic.
  • Centralized and automatic validations.
  • Authorization and identity accessible anywhere.
  • Reduces code duplication.
  • Standardizes developer experience.
  • Facilitates unit testing and cross-context coordination.

Domaintrix.Mediator is essential for applications that demand clean architecture, scalability, integration flexibility, and consistent behavior across layers. Its adoption reinforces maintainability and architectural clarity. ✅