MediatR is a messaging handler which has responsibility for dealing with command and queues.It simplify the controller with single dependency(Most of the case).
Controller use mediatr to send for the data they need to fetch or the action they need to perform.
MediatR with AutoFac :-
1. First need the mediatR using package manager console by command "Install-Package MediatR "
2. Install autofac by "Install-Package Autofac".
// Intializes IOC container...
var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterWebApiFilterProvider(config);
builder.RegisterType<ApiWorkingContext>().As<IApiWorkingContext>();
builder.Register(c => new
HttpContextWrapper(HttpContext.Current)).As(typeof(HttpContextBase));
builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly)
.AsImplementedInterfaces();
// Register all the Command classes (they implement IRequestHandler) in assembly holding the Commands
builder.RegisterAssemblyTypes(typeof(CreateOrderCommand).GetTypeInfo().Assembly)
.AsClosedTypesOf(typeof(IRequestHandler<,>));
builder.Register<ServiceFactory>(context =>
{
var componentContext = context.Resolve<IComponentContext>();
return t => { object o; return componentContext.TryResolve(t, out o) ? o : null; };
});
MediatR with StructureMap :-
1. First need the mediatR using package manager console by command "Install-Package MediatR "
2. Install autofac by "Install-Package structuremap".
Scan.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<,>));
Scan.ConnectImplementationsToTypesClosing(typeof(INotificationHandler<>));
Scan.AssemblyContainingType<IMediator>();
Scan.WithDefaultConventions();
Scan.For<IMediator>().Use<Mediator>();