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.
- 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.
You can install the LightMediator NuGet package using the following command:
dotnet add package LightMediatorYou can publish notifications to subscribers using the LightMediator instance:
private readonly IMediator _mediator;
public SampleService(IMediator mediator)
{
_deviceService = deviceService;
}
.
.
NewAppInfoNotification newAppInfo = new NewAppInfoNotification()
{
Title = "NewApp",
Description = "Des"
};
await mediator.Publish(newAppInfo);To handle notifications, create a class that implements the INotificationHandler<T> interface. Ensure that Notification implements the INotification interface:
public class NewAppInfoNotification : INotification
{
public string Title { get; set; }
public string? Description { get; set; }
}
public class NewAppInfoNotificationHandler : NotificationHandler<NewAppInfoNotification>
{
public override async Task Handle(NewAppInfoNotification notification, CancellationToken? cancellationToken)
{
// Handle the recieved notification
}
}LightMediator also supports commands and command handlers to facilitate request-response communication.
A command is a class that implements the IRequest<TResponse> interface, where TResponse is the type of the response the command expects.
public class TestCommandWithResponse : IRequest<TestCommandResponse>
{
public string Title { get; set; }
public string Description { get; set; }
}
public class TestCommandResponse
{
public string Message { get; set; }
}A command handler is a class that implements the IRequestHandler<TRequest, TResponse> interface. The handler contains the logic for processing the command.
public class TestCommandWithResponseHandler : RequestHandler<TestCommandWithResponse, TestCommandResponse>
{
private readonly ILogger<TestCommandWithResponseHandler> _logger;
public TestCommandWithResponseHandler(ILogger<TestCommandWithResponseHandler> logger)
{
_logger = logger;
}
public override async Task<TestCommandResponse> Handle(TestCommandWithResponse request, CancellationToken? cancellationToken)
{
await Task.CompletedTask;
return new TestCommandResponse
{
Message = "command executed"
};
}
}
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)
{
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);
}
}
}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()
};
});This project is licensed under the MIT License. See the LICENSE file for details.
Created with ❤️ by @omidmloo