I will present code examples to explain. I won't use MediatR, but IGet, with which you can easily create MediatR-like functionality.
In your startup class, instead of adding MediatR, use
serviceCollection.AddIGet();
mediatR.Send
Before sending a request you'll need to create a handler. The constructor may contain all kinds of dependencies, as long as they are part of your service collection.
public class WeatherForecastRequestHandler
{
private IConnectionFactory _connectionFactory;
private ILogger<MyHandler> _logger;
public WeatherForecastRequestHandler(
IConnectionFactory connectionFactory,
ILogger<MyHandler> logger)
{
_connectionFactory = connectionFactory;
_logger = logger;
}
public WeatherForecast Handle(WeatherForecastRequest request)
{
// connect and get weather forecast
return new WeatherForecast
{
// set properties.
}
}
}
Get the weather forecast:
var forecast = i.Get<WeatherForecastRequestHandler>().Handle(request);
mediatR.Publish
This example deviates more from how MediatR works internally, because MediatR collects all INotificationHandlers via assembly scanning. With IGet you can also activate types from a Type[]
but for now I will explicitly instantiate the handlers and invoke their methods. Let's do that in a class that we call NotificationPublisher
(without logging exceptions in this example):
public class NotificationPublisher
{
private IGet i;
public NotificationPublisher(IGet iget)
{
i = iget;
}
public async Task PublishAsync(Notification notification)
{
try
{
await i.Get<FirstHandler>().HandleAsync(notification);
}
catch { }
try
{
await i.Get<SecondHandler>().HandleAsync(notification);
}
catch { }
try
{
i.Get<ThirdHandler>().Handle(notification);
}
catch { }
// etc.
}
}
Now define the 3 handlers in a similar way as in the first example and then we can publish a notification to all of them with one line of code:
await i.Get<NotificationPublisher>().PublishAsync(notification);
The IGet readme actually also contains an example that shows how to make a generic notificaiton publisher for any type of notification.