SMediator.Core 1.0.0

dotnet add package SMediator.Core --version 1.0.0
                    
NuGet\Install-Package SMediator.Core -Version 1.0.0
                    
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="SMediator.Core" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SMediator.Core" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SMediator.Core" />
                    
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 SMediator.Core --version 1.0.0
                    
#r "nuget: SMediator.Core, 1.0.0"
                    
#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 SMediator.Core@1.0.0
                    
#: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=SMediator.Core&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SMediator.Core&version=1.0.0
                    
Install as a Cake Tool

SMediator

A lightweight, zero-dependency Mediator implementation for .NET 8+ that follows the Mediator pattern. SMediator makes it easy to send requests (commands/queries) and publish notifications in your application via a simple API and dependency injection.


Features

  • Zero dependencies beyond .NET 8+ and Microsoft.Extensions.DependencyInjection.
  • Requests & Responses (commands & queries) with IRequest<TResponse>.
  • Notifications (fire-and-forget) with INotification.
  • Automatic handler discovery via assembly scanning.
  • Simple DI integration with IServiceCollection.AddSMediator() extension.
  • Extensible: add pipeline behaviors, scoped/singleton lifetimes, caching, etc.

Installation

Install via NuGet:

dotnet add package SMediator.Core

Your project will reference SMediator and bring in the required abstractions.


Quick Start

  1. Register SMediator in Program.cs:

    using SMediator.Core;
    using Microsoft.Extensions.DependencyInjection;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add SMediator and scan this assembly for handlers
    builder.Services.AddSMediator(typeof(Program).Assembly);
    
    // ... add controllers, Swagger, etc.
    
  2. Define a request and handler:

    // IRequest<T>
    public record GetProductsQuery() : IRequest<List<ProductDto>>;
    
    public class GetProductsQueryHandler : IRequestHandler<GetProductsQuery, List<ProductDto>>
    {
        public Task<List<ProductDto>> Handle(GetProductsQuery request, CancellationToken ct)
        {
            // return list of products
        }
    }
    
  3. Define a notification and handler (optional):

    public record OrderCreatedNotification(int OrderId, int CustomerId) : INotification;
    
    public class OrderCreatedNotificationHandler : INotificationHandler<OrderCreatedNotification>
    {
        public Task Handle(OrderCreatedNotification notification, CancellationToken ct)
        {
            Console.WriteLine($"Order {notification.OrderId} created.");
            return Task.CompletedTask;
        }
    }
    
  4. Send requests / publish notifications anywhere DI is available:

    public class MyService
    {
        private readonly IMediator _mediator;
        public MyService(IMediator mediator) => _mediator = mediator;
    
        public async Task PlaceOrderAsync()
        {
            int newOrderId = await _mediator.Send(new CreateOrderCommand(customerId, items));
            await _mediator.Publish(new OrderCreatedNotification(newOrderId, customerId));
        }
    }
    

Examples

A concrete ASP.NET Core sample lives in the Examples folder:

  • ProductsController (GET /api/products)
  • OrdersController (POST /api/orders)
  • requests.http for VS Code REST Client

Refer to that folder for a full end-to-end demo.


Extension Points

  • Pipeline behaviors: intercept requests (logging, validation, caching).
  • Lifetime management: register handlers as scoped/singleton.
  • Custom conventions: filter which assemblies/types to scan.

Custom AddSMediator overload

public static IServiceCollection AddSMediator(
    this IServiceCollection services,
    params Assembly[] assembliesToScan);

Pass specific assemblies for performance.


Contributing

  1. Fork the repo
  2. Create your feature branch (git checkout -b feature/foo)
  3. Commit your changes (git commit -am 'Add foo')
  4. Push to the branch (git push origin feature/foo)
  5. Open a Pull Request

Please ensure all new code is covered by unit tests.


License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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
1.0.0 182 4/27/2025