SMediator.Core
1.0.0
dotnet add package SMediator.Core --version 1.0.0
NuGet\Install-Package SMediator.Core -Version 1.0.0
<PackageReference Include="SMediator.Core" Version="1.0.0" />
<PackageVersion Include="SMediator.Core" Version="1.0.0" />
<PackageReference Include="SMediator.Core" />
paket add SMediator.Core --version 1.0.0
#r "nuget: SMediator.Core, 1.0.0"
#:package SMediator.Core@1.0.0
#addin nuget:?package=SMediator.Core&version=1.0.0
#tool nuget:?package=SMediator.Core&version=1.0.0
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
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.
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 } }
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; } }
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
- Fork the repo
- Create your feature branch (
git checkout -b feature/foo
) - Commit your changes (
git commit -am 'Add foo'
) - Push to the branch (
git push origin feature/foo
) - Open a Pull Request
Please ensure all new code is covered by unit tests.
License
This project is licensed under the MIT License.
Product | Versions 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.4)
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 |