I have several (ultimately 100+) small MediatR-based DLL projects. This means that the interfaces used are only IMediatR interfaces ( IRequest <TResult>, IRequestHandler <IRequest <TResult>, TResult> ). Since many of them do not have a user interface and are called through orchestration from another DLL, I thought I could create an Autofac Container project (DLL), register all microservices and then decide what I need at runtime in another application that consumes my container . So far so good.
Where I ran into problems is registering each CQRS handler. Right now, although not so important, they are defined as follows:
namespace My.Core.Container
{
public class CoreDependencies
{
#region Properties
public IMediator Mediator { get; private set; }
public IContainer Container { get; private set; }
private static ContainerBuilder _builder;
#endregion
#region Constructor
public CoreDependencies()
{
_builder = new ContainerBuilder();
_builder.RegisterSource(new ContravariantRegistrationSource());
_builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly).AsImplementedInterfaces();
_builder.Register<SingleInstanceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => c.Resolve(t);
});
_builder.Register<MultiInstanceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => (IEnumerable<object>)c.Resolve(typeof(IEnumerable<>).MakeGenericType(t));
});
_builder.RegisterAssemblyTypes(typeof(My.Core.EntityTree.GetChildren).GetTypeInfo().Assembly).AsImplementedInterfaces();
_builder.RegisterAssemblyTypes(typeof(My.Core.EntityTree.DeleteEntity).GetTypeInfo().Assembly).AsImplementedInterfaces();
_builder.RegisterAssemblyTypes(typeof(My.Core.EntityTree.AddEntity).GetTypeInfo().Assembly).AsImplementedInterfaces();
Container = _builder.Build();
Mediator = Container.Resolve<IMediator>();
}
#endregion
}
}
, : ? , 2 3 "" (-), , 20, , 200 ..