I'm playing around with the new ASP.NET Core and are currently creating a API that I want to call from a JavaScript frontend.
I want to use the mediator pattern to reduce the coupling, and I have found the Library MediatR from Jimmy Bogard.
My problem consist in wiring it up using the build in DI, I have tried looking at the examples, but can't see to crack how it binds into the ConfigureServices method in the startup class.
Do anybody have any insight?
UPDATE: I got it working, from my ConfigureService method:
services.AddScoped<SingleInstanceFactory>(p => t => p.GetRequiredService(t));
services.Scan(scan => scan
.FromAssembliesOf(typeof(IMediator), typeof(MyHandler.Handler))
.AddClasses()
.AsImplementedInterfaces());
I got it working, my code:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<SingleInstanceFactory>(p => t => p.GetRequiredService(t));
services.AddScoped<MultiInstanceFactory>(p => t => p.GetRequiredServices(t));
services.Scan(scan => scan
.FromAssembliesOf(typeof(IMediator), typeof(MyHandlerOne.Handler))
.FromAssembliesOf(typeof(IMediator), typeof(MyHandlerTwo.Handler))
.AddClasses()
.AsImplementedInterfaces());
}
and I have a class that implements the GetRequiredService that MultiInstanceFactory need:
public static class GetServices
{
public static IEnumerable<object> GetRequiredServices(this IServiceProvider provider, Type serviceType)
{
return (IEnumerable<object>)provider.GetRequiredService(typeof(IEnumerable<>).MakeGenericType(serviceType));
}
}