NimbleMediator 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package NimbleMediator --version 1.0.0                
NuGet\Install-Package NimbleMediator -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="NimbleMediator" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NimbleMediator --version 1.0.0                
#r "nuget: NimbleMediator, 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.
// Install NimbleMediator as a Cake Addin
#addin nuget:?package=NimbleMediator&version=1.0.0

// Install NimbleMediator as a Cake Tool
#tool nuget:?package=NimbleMediator&version=1.0.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. (~2,5 times faster than MediatR)
  • Less Memory Usage: optimized to minimize memory allocations, helping to reduce the overall memory footprint. (uses ~8x 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 Publish Strategies: Allows for different publish strategies for each notification, enabling developers to choose between sequential or concurrent execution based on the specific requirements of their notification handlers.

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.RegisterHandlersFromAssembly(typeof(Startup).Assembly);
    });

Set default publishing strategy

Set the default publishing strategy for all notifications. It is PublishStrategy.Foreach by default if you don't set it.


    cfg.SetDefaultNotificationPublisherType(PublishStrategy.TaskWhenAll);

Set an individual publishing strategy for a notification

Even the default strategy is different, you can set an individual strategy for a notification.


    cfg.SetNotificationPublisherType<MyRequest1>(PublishStrategy.Foreach);

    cfg.SetNotificationPublisherType<MyRequest2>(PublishStrategy.TaskWhenAll);

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<Request1, string>
    {
        public ValueTask<string> Handle(Request1 request, CancellationToken cancellationToken)
        {
            // Do some work.

            if(someCondition)
            {
                return request.Name;
            }

            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 backed by Dictionaries to hold type informations etc instead of relying on runtime reflection. This approach provides a significant performance advantage over other mediator libraries.

NimbleMediator is currently approximately 2,5 times faster than MediatR and uses almost ~8x less memory.

Benchmark

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 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. 
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.2.0 833 10/3/2023
1.1.0 150 9/17/2023
1.0.2 145 9/13/2023
1.0.1 149 9/12/2023
1.0.0 155 9/12/2023