Sunday, March 1, 2020

MediatR dependency resolved by Autofac and structureMap


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>();


     



Kafka setup in window

Here's a step-by-step guide on how to do it : 1. Prerequisites : Before you begin, make sure you have the following prerequisites inst...