LightMediator 0.4.1
LightMediator
LightMediator is a lightweight library designed to simplify decoupled communication in distributed Windows services. It works with multiple notification types and supports services across different namespaces.
Features
- Lightweight and efficient.
- Supports publish-subscribe, request-response, and one-way notifications.
- Simplifies working with decoupled services in distributed systems.
- Easy to integrate with existing applications.
- Supports commands and command handlers for request-response communication.
Installation
You can install the LightMediator NuGet package using the following command:
dotnet add package LightMediator
Usage
Publish Notifications
You can publish notifications to subscribers using the LightMediator instance:
var mediator = new LightMediator();
mediator.Publish(new Notification("ServiceStarted"));
Handle Notifications
To handle notifications, create a class that implements the INotificationHandler<T> interface. Ensure that Notification implements the INotification interface:
public class Notification : INotification
{
public string Message { get; }
public Notification(string message)
{
Message = message;
}
}
public class NotificationHandler : INotificationHandler<Notification>
{
public Task Handle(Notification notification, CancellationToken cancellationToken)
{
Console.WriteLine($"Received: {notification.Message}");
return Task.CompletedTask;
}
}
Use Commands and Command Handlers
LightMediator also supports commands and command handlers to facilitate request-response communication.
Define a Command
A command is a class that implements the IRequest<TResponse> interface, where TResponse is the type of the response the command expects.
public class TestCommand : IRequest<TestCommandResponse>
{
public string Message { get; set; }
}
Define a Command Handler
A command handler is a class that implements the IRequestHandler<TRequest, TResponse> interface. The handler contains the logic for processing the command.
public class TestCommandHandler : RequestHandler<TestCommand, TestCommandResponse>
{
private readonly ILogger<TestCommandResponseHandler> _logger;
public TestCommandHandler(ILogger<TestCommandResponseHandler> logger)
{
_logger = logger;
}
public override async Task<TestCommandResponse> Handle(TestCommand request, CancellationToken cancellationToken)
{
// Handle the command and return a response
return new TestCommandResponse
{
ResponseMessage = $"Received command with message: {request.Message}"
};
}
}
Define a Command Response
The response is a class that contains the result of processing the command.
public class TestCommandResponse
{
public string ResponseMessage { get; set; }
}
Sending a Command
You can send a command and get a response using the Send method from LightMediator.
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly IMediator _mediator;
public Worker(ILogger<Worker> logger,IMediator mediator)
{
_mediator = mediator;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await _mediator.Send(new TestCommand()
{
Title = "Test",
Description = "Test",
});
var res = await _mediator.Send<bool>(new TestCommandResponse()
{
Title = "Test",
Description = "Test",
});
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}
await Task.Delay(1000, stoppingToken);
}
}
}
Registering Commands , Notifications and Handlers
You can register handlers with the dependency injection container in your application startup:
// Add Mediator to the Dependency Injection container
builder.Services.AddLightMediator(options =>
{
// Configure options for the mediator
options.IgnoreNamespaceInAssemblies = true;
options.IgnoreNotificationDifferences = true;
options.RegisterNotificationsByAssembly = true;
options.RegisterRequestsByAssembly = true;
//Specify the assemblies to scan for notification and request
options.Assemblies = new[]
{
Assembly.GetExecutingAssembly(),
ServiceAExtensions.GetServiceAssembly(),
ServiceBExtensions.GetServiceAssembly(),
ServiceCExtensions.GetServiceAssembly()
};
});
License
This project is licensed under the MIT License. See the LICENSE file for details.
No packages depend on LightMediator.
.NET 9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.1)
- Newtonsoft.Json (>= 13.0.3)
| Version | Downloads | Last updated |
|---|---|---|
| 1.0.0 | 3 | 12/13/2025 |
| 0.5.2 | 1 | 12/13/2025 |
| 0.5.1.4 | 1 | 12/13/2025 |
| 0.5.1.3 | 1 | 12/13/2025 |
| 0.5.1.2 | 1 | 12/13/2025 |
| 0.5.1.1 | 1 | 12/13/2025 |
| 0.5.1 | 1 | 12/13/2025 |
| 0.5.0 | 1 | 12/13/2025 |
| 0.4.8 | 1 | 12/13/2025 |
| 0.4.7 | 1 | 12/13/2025 |
| 0.4.6 | 1 | 12/13/2025 |
| 0.4.5 | 1 | 12/13/2025 |
| 0.4.4 | 1 | 12/13/2025 |
| 0.4.3 | 1 | 12/13/2025 |
| 0.4.2 | 1 | 12/13/2025 |
| 0.4.1 | 1 | 12/13/2025 |
| 0.4.0 | 1 | 12/13/2025 |
| 0.3.0 | 1 | 12/13/2025 |
| 0.2.0 | 1 | 12/13/2025 |
| 0.1.0 | 1 | 12/13/2025 |