简体   繁体   English

将接口映射到具体类

[英]Mapping Interfaces to concrete classes

I have written the following piece of code and I'm looking to refine it. 我已经编写了以下代码,并且正在寻求完善。 using Unity DI, I need to register concrete classes along with the interfaces they implement. 使用Unity DI,我需要注册具体的类及其实现的接口。 The advantage of the method is to remove the need to keep registering new classes when added. 该方法的优点是消除了添加后继续注册新类的需求。

I have reduced the code to 我将代码简化为

IUnityContainer container = new UnityContainer();
            // from
            // Factories
            // container.RegisterType();

            // Services
            // container.RegisterType();
            // container.RegisterType();
            //......
            // to

    var assemblies = AppDomain.CurrentDomain.GetAssemblies()
        .Where(x => x.GetName().Name.Contains("Romabravo.ApplicationManager"));

    foreach (var assembly in assemblies)
    {
        var types = assembly.GetExportedTypes();
        foreach (Type t in types)
        {
            var interfaces = t.GetInterfaces().Where(x => x.FullName != null &&
            x.FullName.Contains("Romabravo.ApplicationManager")).ToList();

                foreach (var item in interfaces)
                {
                    var interfaceImplementers = AppDomain.CurrentDomain.GetAssemblies()
                    .SelectMany(s => s.GetTypes())
                    .Where(p => item.GetTypeInfo().IsAssignableFrom(p) && 
                    item.FullName.Contains("Romabravo.ApplicationManager"))
                    .Select(x => x).Where(x => !x.IsInterface && !x.IsAbstract).ToList();

                    if (interfaceImplementers != null)
                    {
                        foreach (var implementer in interfaceImplementers)
                        {
                            container.RegisterType(item, implementer);
                        }
                    }
                }
            }
        }

    return container;

Just looking for a way to optimise it further. 只是在寻找进一步优化它的方法。

If you are using Unity 3 then you can use registration by convention which could simplify the code a bit: 如果您使用的是Unity 3,则可以按约定使用注册,这可以简化代码:

container.RegisterTypes(
    AllClasses.FromLoadedAssemblies()
      .Where(a => a.FullName.Contains("Romabravo.ApplicationManager")),
    WithMappings.FromMatchingInterface);

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

相关问题 统一通用接口和具体类 - unity generic interfaces and concrete classes 类设计,接口或具体类 - Class design, Interfaces or Concrete classes 公开接口而不是具体的类 - Exposing interfaces instead of concrete classes 当具体类包含其他接口时,如何反序列化接口集合 - How to deserialize collection of interfaces when concrete classes contains other interfaces AutoMapper映射共享接口的两个具体类 - AutoMapper mapping two concrete classes that share an interface 为什么我不能将具体类添加到具体类实现的接口列表中? - Why can't I add concrete classes to a list of interfaces which the concrete classes implement? 为什么要为我的具体DataProvider类创建接口 - Why should I create interfaces for my concrete DataProvider classes 更喜欢在接口上嵌入依赖关系,还是将其交给具体的类? - Preferable to embeb dependencies on interfaces or let that job to the concrete classes? NHibernate-实现多个接口的映射类 - NHibernate - mapping classes that implement multiple interfaces 使用依赖注入是否需要使用接口而不是具体类? - Does the use of Dependency Injection necessitate the use of Interfaces to be injected rather than concrete classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM