MiniMediator 1.1.0
See the version list below for details.
dotnet add package MiniMediator --version 1.1.0
NuGet\Install-Package MiniMediator -Version 1.1.0
<PackageReference Include="MiniMediator" Version="1.1.0" />
paket add MiniMediator --version 1.1.0
#r "nuget: MiniMediator, 1.1.0"
// Install MiniMediator as a Cake Addin #addin nuget:?package=MiniMediator&version=1.1.0 // Install MiniMediator as a Cake Tool #tool nuget:?package=MiniMediator&version=1.1.0
MiniMediator
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 | 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
- System.Reactive (>= 4.4.1)
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 | 589 | 12/28/2020 |
5.0.0 | 652 | 8/19/2020 |
4.1.0 | 640 | 8/4/2020 |
4.0.0 | 611 | 8/4/2020 |
3.2.2 | 437 | 7/20/2020 |
3.2.1 | 620 | 7/20/2020 |
3.2.0 | 825 | 7/19/2020 |
3.1.2 | 614 | 7/16/2020 |
3.1.1 | 666 | 6/29/2020 |
3.0.1 | 682 | 6/28/2020 |
3.0.0 | 466 | 6/28/2020 |
2.2.3 | 587 | 6/27/2020 |
2.2.2 | 455 | 6/27/2020 |
2.2.1 | 515 | 6/27/2020 |
2.2.0 | 1,154 | 6/26/2020 |
2.1.0 | 419 | 6/26/2020 |
2.0.0 | 827 | 6/24/2020 |
1.5.2 | 489 | 6/23/2020 |
1.5.1 | 695 | 6/20/2020 |
1.5.0 | 655 | 6/20/2020 |
1.4.0 | 845 | 6/20/2020 |
1.3.0 | 481 | 6/15/2020 |
1.2.0 | 853 | 6/5/2020 |
1.1.0 | 484 | 6/5/2020 |
1.0.0 | 457 | 5/30/2020 |