简体   繁体   English

在 Unity Container Config 中一次注册多种类型

[英]Register many types at once in Unity Container Config

We are using some services in unity that need to be added manualy everytime.我们正在统一使用一些服务,每次都需要手动添加。 The only problem is that this is easy to forget.唯一的问题是这很容易忘记。 Here's how we construct the container:以下是我们构建容器的方式:

 container.RegisterType<IWorkflowEntityService<emplabsq>, EmplabsqService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<requhrap>, RequhrapService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<emplabsk>, EmplabskService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<lettrept>, LettreptService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<lettdocs>, LettdocsService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<emplrqti>, EmplrqtiService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<emplplrq>, EmplplrqService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<empltrrq>, EmpltrrqService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<emplfurq>, EmplfurqService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<emploprq>, EmploprqService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<emplqtrq>, EmplqtrqService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<emplclrq>, EmplclrqService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<addrstrq>, AddrstrqService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<addrphrq>, AddrphrqService>(new PerRequestLifetimeManager())
        .RegisterType<IWorkflowEntityService<addrmlrq>, AddrmlrqService>(new PerRequestLifetimeManager())

Is there any way we can simplify this and auto generate this code?有什么方法可以简化并自动生成此代码?

Thanks all.谢谢大家。

I managed to do it this way.我设法做到了这一点。 Not sure if it's the right way btw.不知道这是否是正确的方式顺便说一句。

 var type = typeof(IWorkflowable);
        var workflowables = AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes()).Where(p => type.IsAssignableFrom(p) && p.Name != type.Name);
        foreach (var @class in workflowables)
        {
            var serviceClassName = @class.Name.First().ToString().ToUpper() + @class.Name.Substring(1);
            Type serviceClass = AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes()).Where(x => x.Name.StartsWith(serviceClassName) && x.Name.EndsWith("Service")).FirstOrDefault();

            IWorkflowable instance = (IWorkflowable)Activator.CreateInstance(@class);

            var dataType = new Type[] { @class };
            var genericBase = typeof(IWorkflowEntityService<>);
            var combinedType = genericBase.MakeGenericType(dataType);

            container.RegisterType(combinedType, serviceClass);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM