MiniMediator 1.3.0

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

// Install MiniMediator as a Cake Tool
#tool nuget:?package=MiniMediator&version=1.3.0                

MiniMediator

Build Status Nuget MiniMediator Nuget MiniMediator.DependencyInjection

A simple mediator that requires no setup, just publish messages and subscribe to messages. Helpful with event driven applications.

Usage

Simple

With an example consumer and message. A single message can be consumed by many subscribed consumers.

public class Consumer
{
    public Consumer(Mediator mediator)
    {
        mediator.Subscribe<Message>(Receive);
    }

    public virtual void Receive(Message message)
    {

    }
}

public class Message
{
    public string Content { get; set; }
}

Sending a message from the mediator will invoke Receive on the Consumer.

// Arrange
var mediator = new Mediator();
var consumer = Substitute.ForPartsOf<Consumer>(mediator);
var message = new Message
{
    Content = "This is a new message"
};

// Act
mediator.Publish(message);

// Assert
consumer.Received(1).Receive(Arg.Is<Message>(x => x == message));

Creating a new sub class for the message and consumer demonstrates that Consumers of message types which are super classes of published messaged will get invoked.

public class DifferentConsumer
{
    public DifferentConsumer(Mediator mediator)
    {
        mediator.Subscribe<DifferentMessage>(DifferentReceive);
    }

    public virtual void DifferentReceive(DifferentMessage message)
    {

    }
}

public class DifferentMessage : Message
{
    public int Sequence { get; set; }
}

Here, both consumers get invoked when the sub class DifferentMessage is published.

// Arrange
var mediator = new Mediator();
var consumer = Substitute.ForPartsOf<Consumer>(mediator);
var differentConsumer = Substitute.ForPartsOf<DifferentConsumer>(mediator);
var message = new DifferentMessage
{
    Content = "This is a new message",
    Sequence = 5
};

// Act
mediator.Publish(message);

// Assert
consumer.Received(1).Receive(Arg.Is<Message>(x => x == message));
differentConsumer.Received(1)
    .DifferentReceive(Arg.Is<DifferentMessage>(x => x == message));

When the message type is not assignable to the subscribed type the subscription is not invoked.

// Arrange
var mediator = new Mediator();
var consumer = Substitute.ForPartsOf<Consumer>(mediator);
var differentConsumer = Substitute.ForPartsOf<DifferentConsumer>(mediator);
var message = new Message
{
    Content = "This is a new message"
};

// Act
mediator.Publish(message);

// Assert
consumer.Received(1).Receive(Arg.Is<Message>(x => x == message));
differentConsumer.DidNotReceive().DifferentReceive(Arg.Any<DifferentMessage>());

Or if the message is of a subtype but the generic type is the supertype, then the subtype subscriptions are not invoked. This is to allow greater control over how the message is handled. If the producer is expected type T it should be handled as T.

// Arrange
var mediator = new Mediator();
var consumer = Substitute.ForPartsOf<Consumer>(mediator);
var differentConsumer = Substitute.ForPartsOf<DifferentConsumer>(mediator);
var message = new DifferentMessage
{
    Content = "This is a new message",
    Sequence = 5
};

// Act
mediator.Publish<Message>(message);

// Assert
consumer.Received(1).Receive(Arg.Is<Message>(x => x == message));
differentConsumer.DidNotReceive().DifferentReceive(Arg.Any<DifferentMessage>());

Dependency Injection

Implement handlers using the IMessageHandler<TMessage> interface. Then use the IContainerExtensions.

// register with service container
serivces.AddTransient<MyHandler>()

// and/or specify assemblies to load
services.AddMediator(Assembly.GetExecutingAssembly());
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 (1)

Showing the top 1 NuGet packages that depend on MiniMediator:

Package Downloads
MiniMediator.DependencyInjection

Simple mediator to handle publishing and subscribing to messages

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.1.3 587 12/28/2020
5.0.0 650 8/19/2020
4.1.0 638 8/4/2020
4.0.0 609 8/4/2020
3.2.2 435 7/20/2020
3.2.1 618 7/20/2020
3.2.0 823 7/19/2020
3.1.2 612 7/16/2020
3.1.1 664 6/29/2020
3.0.1 680 6/28/2020
3.0.0 464 6/28/2020
2.2.3 585 6/27/2020
2.2.2 453 6/27/2020
2.2.1 513 6/27/2020
2.2.0 1,152 6/26/2020
2.1.0 417 6/26/2020
2.0.0 825 6/24/2020
1.5.2 487 6/23/2020
1.5.1 693 6/20/2020
1.5.0 653 6/20/2020
1.4.0 843 6/20/2020
1.3.0 479 6/15/2020
1.2.0 851 6/5/2020
1.1.0 482 6/5/2020
1.0.0 455 5/30/2020