zoukankan      html  css  js  c++  java
  • Ioc容器Autofac 三种注册方式

    简单来说,所谓注册组件,就是注册类并映射为接口,然后根据接口获取对应类,Autofac将被注册的类称为组件。 虽然可像上篇提到的一次性注册程序集中所有类,但AutoFac使用最多的还是单个注册。这种注册共有三种方式,其中最简单的就是用As方法,例如,ArrayList继承了IEnumerable接口,若将其注册到Autofac中,写法如下所示:

    ContainerBuilder builder = new ContainerBuilder();   
                builder.RegisterType<ArrayList>().As<IEnumerable>();   
                // then   
                IContainer container = builder.Build();   
                ArrayList result = (ArrayList)container.Resolve<IEnumerable>();

    有时难免碰到多个类映射到同一种接口,比如ArrayList和SortedList都继承了IEnumerable接口,为了准确获取想要的类型,就有了第二种方式--Named方法,可根据字符串注册并解析相应的类。代码如下:

    builder.RegisterType<ArrayList>().Named<IEnumerable>("array");   
                builder.RegisterType<SortedList>().Named<IEnumerable>("sort");   
                // then   
                IContainer container = builder.Build();   
                ArrayList result = (ArrayList)container.ResolveNamed<IEnumerable>("array");   
                SortedList result1 = (SortedList)container.ResolveNamed<IEnumerable>("sort");

    然而相对于字符串,枚举更易读易用,但在Autofac中使用枚举会有些麻烦,因为Named只接受字符串,必须将枚举转换成字符串才能使用。比如,现在用ListType枚举来代替“array”和“sort”字符串。如下所示:

    public enum ListType{ Array, Sort }   
    builder.RegisterType<ArrayList>().Named<IEnumerable>(ListType.Array.ToString());

    这种转换略显笨拙,而且让枚举失去原有价值。于是,为不多此一举,第三种方式就产生了--Keyed方式,该方法可以接受任何类型作为参数。以上文中的ListType枚举为例,代码如下:

    builder.RegisterType<ArrayList>().Keyed<IEnumerable>(ListType.Array);   
                builder.RegisterType<SortedList>().Keyed<IEnumerable>(ListType.Sort);   
                // then   
                IContainer container = builder.Build();   
                ArrayList result = (ArrayList)container.ResolveKeyed<IEnumerable>(ListType.Array);   
                SortedList result1 = (SortedList)container.ResolveKeyed<IEnumerable>(ListType.Sort);

    无论使用As、Named还是Keyed都可达到注册目的,根据个人喜好而定。但从功能上来讲,Autofac的设计者推荐第三种。

    通过IIndex索引查找被Keyed标识过的类

    如果使用Keyed方法注册类,可通过Autofac内置索引来查找指定的值,这个索引的类型是IIndex<Tkey,TValue>,以上文的枚举为例,可通过索引完成解析,代码如下:

    IIndex<ListType, IEnumerable> allList = container.Resolve<IIndex<ListType, IEnumerable>>();   
                ArrayList result = (ArrayList)allList[ListType.Array];   
                SortedList result1 = (SortedList)allList[ListType.Sort];

    IIndex中第一个泛型参数要跟注册时一致,这里是ListType枚举。其他两种注册方法没有这样的索引查找功能,这也是为什么设计者推荐Keyed注册的原因之一。

  • 相关阅读:
    数据库——事务
    数据库——连接池
    JDBC——使用jdbc操作时,如何提取数据的性能
    2019-06-26 The Eclipse executable launcher was unable to locate its companion launcher
    2019-06-24在windows下如何查看80端口占用情况?是被哪个进程占用
    2019-06-19_解决ActiveMQ访问出现503的错误
    2019-06-17 tomcat启动;zookeeper启动批处理
    2019-06-13记忆 长命令
    2019-06-13linux的vi编辑器中如何查找关键字
    2019-06-13-vim配色步骤
  • 原文地址:https://www.cnblogs.com/acoll/p/4462386.html
Copyright © 2011-2022 走看看