CleanArch.Mediator.AspNetCore 0.3.1

dotnet add package CleanArch.Mediator.AspNetCore --version 0.3.1
                    
NuGet\Install-Package CleanArch.Mediator.AspNetCore -Version 0.3.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CleanArch.Mediator.AspNetCore" Version="0.3.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CleanArch.Mediator.AspNetCore" Version="0.3.1" />
                    
Directory.Packages.props
<PackageReference Include="CleanArch.Mediator.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CleanArch.Mediator.AspNetCore --version 0.3.1
                    
#r "nuget: CleanArch.Mediator.AspNetCore, 0.3.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CleanArch.Mediator.AspNetCore@0.3.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CleanArch.Mediator.AspNetCore&version=0.3.1
                    
Install as a Cake Addin
#tool nuget:?package=CleanArch.Mediator.AspNetCore&version=0.3.1
                    
Install as a Cake Tool

CleanArchitecture Mediator AspNetCore

NuGet

CleanArch.Mediator.AspNetCore provides seamless integration between ASP.NET Core, Mediator request/response architecture, and Clean Architecture principles.

It is designed to eliminate boilerplate, enable automatic endpoint discovery, support pipeline behaviors, and provide clean separation between Application and Infrastructure layers.

This package is ideal for Vertical Slice Architecture, Clean Architecture, and modular monolith designs.


✨ Features

  • πŸš€ Automatic command/query endpoint mapping using attributes
  • 🧬 Pipeline behaviors (logging, validation, performance tracking, etc.)
  • βœ… Clean separation of concerns β€” no ASP.NET Core dependencies in the Application layer
  • πŸ“„ Convention-based endpoint discovery (Minimal APIs or Controllers)
  • πŸ’― Zero boilerplate wiring
  • 🚨 Custom exception β†’ HTTP result mapping
  • πŸ• Perfect for vertical slices

πŸ“¦ Installation

dotnet add package CleanArch.Mediator.AspNetCore

πŸš€ Quick Start

Register Mediator and your Application Assemblies

builder.AddMediator([ typeof(Create).Assembly ]);

That’s it β€” all commands/queries with HttpEndpointAttribute get mapped automatically.

🧩 Defining a Command With an Endpoint

Command

public sealed record CreateUserCommand(string Name, string Email) : ICommand<UserDto>;

Handler

public sealed class CreateUserHandler : ICommandHandler<CreateUserCommand, UserDto>
{
    public Task<UserDto> Handle(CreateUserCommand request, CancellationToken cancellationToken)
    {
        return Task.FromResult(new UserDto(request.Name, request.Email));
    }
}

πŸ”Ž Creating a Query With Route Parameters

[HttpEndpoint(Method = HttpMethod.Get, Route = "users/{id}")]
public sealed record GetUserByIdQuery(Guid Id)
    : IRequest<UserDto>;

Handler:

public sealed class GetUserByIdHandler: IRequestHandler<GetUserByIdQuery, UserDto>
{
    public Task<UserDto> Handle(GetUserByIdQuery request, CancellationToken ct)
    {
        // Fetch user from DB
        return Task.FromResult(new UserDto("John", "john@example.com"));
    }
}

πŸ”§ Pipeline Behavior Example

public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
    public async Task<TResponse> Handle(
        TRequest request,
        RequestHandlerDelegate<TResponse> next,
        CancellationToken cancellationToken)
    {
        Console.WriteLine($"Executing {typeof(TRequest).Name}...");
        
        var response = await next();

        Console.WriteLine($"Finished {typeof(TRequest).Name}");

        return response;
    }
}

Register using AddMediator:

builder.Services.AddMediator(opts =>
{
    opts.AddBehavior(typeof(LoggingBehavior<,>));
});

🧨 Exception β†’ HTTP Result Mapping

public class UserNotFoundException : Exception;

public class UserNotFoundExceptionMapper 
    : IExceptionMapper<UserNotFoundException>
{
    public IResult Map(UserNotFoundException exception)
    {
        return Results.NotFound(new { message = "User not found" });
    }
}

Automatically discovered when AddMediatorAspNetCore() is used.


πŸ“ Suggested Folder Structure

src/
 β”œβ”€β”€ Users/
 β”‚    β”œβ”€β”€ CreateUser/
 β”‚    β”‚     β”œβ”€β”€ CreateUserCommand.cs
 β”‚    β”‚     β”œβ”€β”€ CreateUserHandler.cs
 β”‚    β”œβ”€β”€ GetUserById/
 β”‚    β”‚     β”œβ”€β”€ GetUserByIdQuery.cs
 β”‚    β”‚     β”œβ”€β”€ GetUserByIdHandler.cs

Keeps each vertical slice isolated and maintainable.


🀝 Contributing

Pull requests and discussions are welcome!
Please ensure new features include tests and follow Clean Architecture best practices.


πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.3.1 461 12/11/2025