NimbleMediator 1.1.0
See the version list below for details.
dotnet add package NimbleMediator --version 1.1.0
NuGet\Install-Package NimbleMediator -Version 1.1.0
<PackageReference Include="NimbleMediator" Version="1.1.0" />
paket add NimbleMediator --version 1.1.0
#r "nuget: NimbleMediator, 1.1.0"
// Install NimbleMediator as a Cake Addin #addin nuget:?package=NimbleMediator&version=1.1.0 // Install NimbleMediator as a Cake Tool #tool nuget:?package=NimbleMediator&version=1.1.0
NimbleMediator
NimbleMediator is a significantly faster, lightweight and memory-efficient mediator pattern implementation for .NET, designed to be an alternative to popular mediator libraries.
Features
- Faster Performance: Designed to offer a speed advantage over similar packages, providing quicker request and notification processing times. (~3,5 times faster than MediatR)
- Less Memory Usage: optimized to minimize memory allocations, helping to reduce the overall memory footprint. (uses ~16x less memory than MediatR)
- Easy to Integrate: Can be easily integrated into existing .NET projects, offering a simple and similar IMediator, ISender, IPublisher interfacses.
- Individualized Notification Publishers: Allows for different publisher implementations for each notification, enabling developers to have the flexibility to choose the best publisher implementation for each notification. Also supports custom publisher implementations via INotificationPublisher interface.
Getting Started
Install
Install the NimbleMediator with dotnet add package NimbleMediator
or via NuGet package manager.
Configure
Set up NimbleMediator in your Startup.cs
or Program.cs
by utilizing the services.AddNimbleMediator()
method and configuring your handlers and publishers as necessary.
Register Handlers
Register your handlers from the assembly:
services.AddNimbleMediator(cfg => {
cfg.RegisterServicesFromAssembly(typeof(Startup).Assembly);
});
or from assemblies:
services.AddNimbleMediator(cfg => {
cfg.RegisterServicesFromAssemblies(typeof(X).Assembly, typeof(Y).Assembly, typeof(Z).Assembly);
});
Set default publisher (optional)
Set the default publisher implementation for all notifications.
It is ForeachAwaitRobustPublisher
by default if you don't set it.
cfg.SetDefaultNotificationPublisher<ForeachAwaitStopOnFirstExceptionPublisher();
or
cfg.SetDefaultNotificationPublisher<TaskWhenAllPublisher();
Set default publisher's lifetime (optional)
Set the default publisher's lifetime. It is Singleton
by default if you don't set it.
cfg.SetDefaultNotificationPublisherLifetime(ServiceLifetime.Transient);
or
cfg.SetDefaultNotificationPublisher<ForeachAwaitStopOnFirstExceptionPublisher(ServiceLifetime.Transient);
Set a different publisher for a particular notification (optional)
cfg.SetNotificationPublisher<MyNotification, TaskWhenAllPublisher>();
you can even provide lifetime here:
cfg.SetNotificationPublisher<MyNotification, TaskWhenAllPublisher>(ServiceLifetime.Singleton);
Default publishers
ForeachAwaitRobustPublisher
: Executes all handlers in a sequential manner. If any of the handlers throws an exception, it will be caught and will thrown after all handlers are executed. Ensures that all handlers are executed even if one of them throws an exception.ForeachAwaitStopOnFirstExceptionPublisher
: Executes all handlers in a sequential manner. If any of the handlers throws an exception, will be caught and be thrown immediately. Stops executing handlers if one of them throws an exception.TaskWhenAllPublisher
: Executes all handlers in a concurrent manner. If any of the handlers throws an exception, it will be caught and will thrown after all handlers are executed. Ensures that all handlers are executed even if one of them throws an exception.
Custom publishers
You can implement your own publisher by implementing INotificationPublisher
interface.
public class MyOwnCustomPublisher : INotificationPublisher
{
public async Task PublishAsync<TNotification>(TNotification notification, IEnumerable<INotificationHandler<TNotification>> handlers, CancellationToken cancellationToken)
where TNotification : INotification
{
// Implement
}
}
And set it as default publisher:
cfg.SetDefaultNotificationPublisher<MyOwnCustomPublisher>();
Or set it just for a particular notification:
cfg.SetNotificationPublisher<MyNotification, MyOwnCustomPublisher>();
Define Requests and Handlers
Define your requests and handlers as you would with any other mediator library.
Notice that ValueTask
is used instead of Task
to reduce memory allocations in case of synchronus execution based on some condition.
public class MyRequest : IRequest<string>
{
public string Name { get; set; }
}
public class MyRequestHandler : IRequestHandler<MyRequest, string>
{
public ValueTask<string> Handle(Request1 request, CancellationToken cancellationToken)
{
// Do some work.
if(someCondition)
{
return "NimbleMediator";
}
var result = await SomeAsyncTask();
return result;
}
}
Define Notifications and Handlers
Define your notifications and handlers as you would with any other mediator library.
ValueTask
's are not used for Notifications due asynchronous nature of the notifications.
public class MyNotification : INotification
{
public string Name { get; set; }
}
public class MyNotificationHandler : INotificationHandler<MyNotification>
{
public Task Handle(MyNotification notification, CancellationToken cancellationToken)
{
await SomeAsyncTask();
}
}
Performance & Benchmarks
NimbleMediator is directly depends DI container to get handlers instead of relying on runtime reflection. This approach provides a significant performance advantage over other mediator libraries.
NimbleMediator is currently 3,5 times faster and uses 16x less memory than MediatR in some cases.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. See the Contributing Guidelines for more information.
License
All contents of this package are licensed under the Apache License 2.0.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.