Skip to content

Getting started

Mediant targets .NET 8, 9 and 10.

1. Install

Terminal window
dotnet add package Mediant

Optional packages, added as you need them:

PackageWhat it adds
Mediant.BehaviorsThe 11 built-in pipeline behaviors
Mediant.FluentValidationFluentValidation integration (auto-discovery, multi-validator)
Mediant.AspNetCore[HttpEndpoint] mapping, Result-to-HTTP, OpenAPI
Mediant.SourceGeneratorCompile-time, AOT-safe registration & dispatch
Mediant.EntityFrameworkCoreDurable EF Core stores for the outbox and audit
Mediant.AnalyzersRoslyn analyzers that catch attribute misuse at compile time
Mediant.ContractsShared contracts for multi-project solutions

2. Register

builder.Services.AddMediant(cfg =>
cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));

3. Define a command

Commands change state; queries read it. Mediant makes that explicit.

public record CreateOrderCommand(string UserId, List<OrderItem> Items)
: ICommand<Result<Guid>>;

4. Handle it

Handlers return ValueTask<T> and carry failures in a Result<T> — no exceptions for control flow.

public class CreateOrderHandler : ICommandHandler<CreateOrderCommand, Result<Guid>>
{
public ValueTask<Result<Guid>> Handle(CreateOrderCommand request, CancellationToken ct)
{
var orderId = Guid.NewGuid();
// ... create the order
return new ValueTask<Result<Guid>>(Result<Guid>.Success(orderId));
}
}

5. Send it

var result = await mediator.Send(new CreateOrderCommand("user-1", items));
result.Match(
id => Console.WriteLine($"Order created: {id}"),
error => Console.WriteLine($"Failed: {error.Description}"));

Configuration

builder.Services.AddMediant(cfg =>
{
cfg.RegisterServicesFromAssembly(typeof(Program).Assembly);
cfg.NotificationPublishStrategy = NotificationPublishStrategy.Parallel;
cfg.EnablePolymorphicNotifications = true;
cfg.ValidateOnStartup = true; // fail fast on a missing handler
cfg.AddOpenBehavior(typeof(MyLoggingBehavior<,>)); // wraps every request
});
builder.Services.AddMediantValidation(typeof(Program).Assembly);
builder.Services.AddMediantAllBehaviors();

Namespaces

using Mediant.Abstractions; // ICommand, IQuery, handlers, behaviors
using Mediant.Results; // Result<T>, Error, ErrorType

Next steps