Autofac,MediatR&多个DLL项目 [英] Autofac, MediatR & multiple DLL projects

查看:261
本文介绍了Autofac,MediatR&多个DLL项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个(最终超过100个)小型DLL项目,这些项目均基于MediatR.这意味着正在使用的接口只是IMediatR接口( IRequest< TResult>,IRequestHandler< IRequest< TResult>,TResult> ).由于其中许多没有UI,并且通过另一个DLL的编排调用,因此我想我可以创建一个Autofac容器项目(DLL),注册所有微服务,然后在运行时在另一个应用程序中解决我需要的内容,消耗我的容器.到目前为止,一切都很好.

I have several (eventually 100+) small DLL projects all based on MediatR. This means that the interfaces in use are just the IMediatR interfaces (IRequest<TResult>, IRequestHandler<IRequest<TResult>, TResult>). Since a lot of these do not have a UI and are called via orchestration from another DLL I was thinking I could create an Autofac Container project (DLL), register all the micro-services, then resolve what I need at runtime in another app that consumes my container. So far, so good.

我遇到问题的地方是每个CQRS处理程序的注册.现在,尽管所有内容都很小,但它们是按以下方式内联定义的:

Where I am running into problems is the registration of each and every CQRS handler. Right now, while everything is small in scope, they are being defined inline like this:

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

            // register MediatR types...
            _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));
            });

            // ################################################## //
            // register EntityTree micro services...
            _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();

            // register next micro services...
            // register next micro services...
            // ad naseum...
            // ################################################## //

            // Now build it...
            Container = _builder.Build();
            Mediator = Container.Resolve<IMediator>();
        }
        #endregion
    }
}

所以,我的问题是:如何正确进行此注册?现在,有2或3个核心"(微服务),但是下个月,20,明年,200等...

So, my question is: How do I correctly do this registration? Right now, 2 or 3 "cores" (micro-services) but next month, 20, next year, 200, etc...

TIA

推荐答案

您可以执行

Instead of manually register assembly by assembly, you could do Assembly Scanning to retrieve all assemblies you need, and then, register them in simple loop (code not tested):

var assemblies = /* get assemblies based on project type (web/desktop) */;
foreach(var assembly in assemblies)
{
    container.RegisterAssemblyTypes(assembly).AsImplementedInterfaces();
}

顺便说一句,除非您有很强的论点(服务定位器反模式),否则不应将容器公开为公共属性.另外SingleInstanceFactoryMultiInstanceFactory对我来说也很可疑...

Btw, you should not expose your container as public property, unless you have very strong arguments to do so (service locator anti-pattern). Also SingleInstanceFactory and MultiInstanceFactory look pretty suspicious for me...

这篇关于Autofac,MediatR&amp;多个DLL项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆