国际奥委会 - 多种实现单一接口支持 [英] IoC - Multiple implementations support for a single interface

查看:127
本文介绍了国际奥委会 - 多种实现单一接口支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么净IoC容器中,不要轻易支持多个实现单个接口!也许是我错了,但据我所看到的,像Ninject框架部分支持使用注释(A HREF此功能<=htt​​p://stackoverflow.com/questions/11684022/annotation-based-di-with-producers/ 11684470#11684470>如何?)。我不认为像温莎或简单的注射器其他框架有一个简单的机制来支持此方案。



是否有这是为什么没有很多框架支持的任何原因?据我所知,中使用接口的最重要原因之一就是实现松耦合。如果旨在提高松耦合框架,不流利支持多个实现了单一界面,我不明白为什么!



P.S。当然,我明白,会有在运行时的分辨率问题,并且该容器会混淆选择哪个实现,但是这是一些东西,有丰富的设计要考虑的,是吧?


< DIV CLASS =h2_lin>解决方案

团结具有相同的功能



注册名为依赖

  VAR集装箱=​​新UnityContainer(); 
container.RegisterType< IConnector,连接器及GT;(TestConnector);



按名称解析

  container.Resolve< IConnector>(TestConnector); 



同样的方法

  [依赖(TestConnector)] 
公共IConnector连接器{搞定;组; }



温莎有相同的

 类节目
{
静态无效的主要(字串[] args)
{
变种集装箱=新WindsorContainer( )
.Register(Component.For< IConnector>()ImplementedBy< ConnectorA>()命名(ConnectorA))
.Register。(Component.For< IConnector>()ImplementedBy< ConnectorB&G​​T ;()命名(ConnectorB))。

VAR connectorA = container.Resolve< IConnector>(ConnectorA);
Console.WriteLine(连接器类型:{0},connectorA.GetType());
变种connectorB = container.Resolve&所述; IConnector>(ConnectorB);
Console.WriteLine(连接器类型:{0},connectorB.GetType());
Console.ReadKey();
}
}

公共接口IConnector
{
}

公共类ConnectorA:IConnector
{

}

公共类ConnectorB:IConnector
{

}


I am wondering why .Net IoC containers do not easily support multiple implementations for a single interface! May be I am wrong, but as far I have seen, frameworks like Ninject partially supports this feature using annotations (how?). I do not think other frameworks like Windsor or simple injector have an easy mechanism to support this scenario.

Is there any reason why this is not supported by many frameworks? AFAIK, one of the most important reasons to use interfaces is to achieve loose coupling. If the frameworks designed to improve loose coupling, do not fluently support multiple implementations for a single interface, I do not understand why!

P.S. Of course I understand that there will be a resolution issue during run time, and the container would be confused which implementation to choose, but that is something which has to be considered in the design, right?

解决方案

Unity has the same functionality

Register named dependency

    var container = new UnityContainer();
    container.RegisterType<IConnector, Connector>("TestConnector");

Resolve by name

    container.Resolve<IConnector>("TestConnector");

the same approach

    [Dependency("TestConnector")]
    public IConnector Connector { get; set; }

Windsor has the same

class Program
{
    static void Main(string[] args)
    {
        var container = new WindsorContainer()
            .Register(Component.For<IConnector>().ImplementedBy<ConnectorA>().Named("ConnectorA"))
            .Register(Component.For<IConnector>().ImplementedBy<ConnectorB>().Named("ConnectorB"));

        var connectorA = container.Resolve<IConnector>("ConnectorA");
        Console.WriteLine("Connector type: {0}", connectorA.GetType());
        var connectorB = container.Resolve<IConnector>("ConnectorB");
        Console.WriteLine("Connector type: {0}", connectorB.GetType());
        Console.ReadKey();
    }
}

public interface IConnector
{
}

public class ConnectorA : IConnector
{

}

public class ConnectorB : IConnector
{

}

这篇关于国际奥委会 - 多种实现单一接口支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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