MiniMediator.DependencyInjection 1.2.0

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

// Install MiniMediator.DependencyInjection as a Cake Tool
#tool nuget:?package=MiniMediator.DependencyInjection&version=1.2.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

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
6.1.3 497 12/28/2020
5.0.0 497 8/19/2020
4.1.0 494 8/4/2020
4.0.0 532 8/4/2020
3.2.2 504 7/20/2020
3.2.1 493 7/20/2020
3.2.0 523 7/19/2020
3.1.2 463 7/16/2020
3.0.1 510 6/28/2020
2.2.3 609 6/27/2020
2.2.2 444 6/27/2020
2.2.1 592 6/27/2020
2.2.0 537 6/26/2020
2.1.0 488 6/26/2020
2.0.0 498 6/24/2020
1.5.2 514 6/23/2020
1.5.1 539 6/20/2020
1.5.0 493 6/20/2020
1.4.0 512 6/20/2020
1.3.0 511 6/15/2020
1.2.0 510 6/5/2020