CollectionOrderModule类是一个Autofac模块(Module,将一系列组件和相关的功能包装在一起),而非Orchard模块。其作用是保证多个注册到容器的组件能按FIFO(First In First Out)的顺序提取。下面举例说明:
1、创建ICustomerService接口:

    public interface ICustomerService { }
  

2、创建两个实现ICustomerService接口的类:

    public class DefaultCustomerService : ICustomerService { }
    public class VIPCustomerService : ICustomerService { }
  

3、测试:

    [TestFixture]
    public class AutofacTest
    {
        [ Test]
        public void TestCollectionModule()
        {
            ContainerBuilder builder = new ContainerBuilder();
            //builder.RegisterModule(new CollectionOrderModule());
            builder.RegisterType< DefaultCustomerService>().As<ICustomerService >();
            builder.RegisterType< VIPCustomerService>().As<ICustomerService >();
 
            IContainer container = builder.Build();
            var customeres = container.Resolve<IEnumerable< ICustomerService>>();
            //判断第一个注册的服务,取出来是不是第一个
            Assert.That(customeres.First(), Is .TypeOf<DefaultCustomerService>());
            //判断最后一个注册的服务,取出来是不是最后一个
            Assert.That(customeres.Last(), Is .TypeOf<VIPCustomerService>());
 
            //只影响集合解析,解析单个Service不受影响
            var customer = container.Resolve<ICustomerService >();
            Assert.That(customer, Is .TypeOf<VIPCustomerService>());
 
        }
    }
上述代码是不能测试通过的。
4、如果向Autofac容器注册一个CollectionOrderModule,将能确保测试通过:
        [ Test]
        public void TestCollectionModule()
        {
            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterModule( newCollectionOrderModule ());
            //...
        }
附,CollectionOrderModule的源码:
    class CollectionOrderModule : IModule {
        public void Configure( IComponentRegistry componentRegistry) {
            componentRegistry.Registered += (sender, registered) => {
                // only bother watching enumerable resolves
                var limitType = registered.ComponentRegistration.Activator.LimitType;
                if (typeof ( IEnumerable).IsAssignableFrom(limitType)) {
                    registered.ComponentRegistration.Activated += (sender2, activated) => {
                        // Autofac's IEnumerable feature returns an Array
                        if (activated.Instance is Array) {
                            // Orchard needs FIFO, not FILO, component order
                            Array .Reverse((Array )activated.Instance);
                        }
                    };
                }
            };
        }
    }
 
Orchard这么做的目的有待于进一步发掘研究。但有一定可以肯定,Orchard对某些组件是顺序敏感的。
参考资料: Autofac:Structuring With Modules Autofac:Activation events

Orchard源码分析(4.1):Orchard.Environment.CollectionOrderModule类的更多相关文章

  1. Orchard源码分析(5):Host相关(Orchard.Environment.DefaultOrchardHost类)

    概述 Host 是应用程序域级的单例,代表了Orchard应用程序.其处理应用程序生命周期中的初始化.BeginRequest事件.EndRequest事件等. 可以简单理解为HttpApplicat ...

  2. Spring源码分析——BeanFactory体系之抽象类、类分析(二)

    上一篇分析了BeanFactory体系的2个类,SimpleAliasRegistry和DefaultSingletonBeanRegistry——Spring源码分析——BeanFactory体系之 ...

  3. Orchard源码分析(1):Orchard架构

      本文主要参考官方文档"How Orchard works"以及Orchardch上的翻译.   源码分析应该做到庖丁解牛,而不是以管窥豹或瞎子摸象.所以先对Orchard架构有 ...

  4. spring源码分析系列 (5) spring BeanFactoryPostProcessor拓展类PropertyPlaceholderConfigurer、PropertySourcesPlaceholderConfigurer解析

    更多文章点击--spring源码分析系列 主要分析内容: 1.拓展类简述: 拓展类使用demo和自定义替换符号 2.继承图UML解析和源码分析 (源码基于spring 5.1.3.RELEASE分析) ...

  5. Spring源码分析——BeanFactory体系之抽象类、类分析(一)

    上一篇介绍了BeanFactory体系的所有接口——Spring源码分析——BeanFactory体系之接口详细分析,本篇就接着介绍BeanFactory体系的抽象类和接口. 一.BeanFactor ...

  6. Thinkphp源码分析系列(二)–引导类

    在上一章我们说到,ThinkPHP.php在设置完框架所需要的变量和调教好环境后,在最后调用了  Think\Think::start();  即Think命名空间中的Think类的静态方法start ...

  7. Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第1节: FastThreadLocal的使用和创建

    Netty源码分析第八章: 高性能工具类FastThreadLocal和Recycler 概述: FastThreadLocal我们在剖析堆外内存分配的时候简单介绍过, 它类似于JDK的ThreadL ...

  8. Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第2节: FastThreadLocal的set方法

    Netty源码分析第八章: 高性能工具类FastThreadLocal和Recycler 第二节: FastThreadLocal的set方法 上一小节我们学习了FastThreadLocal的创建和 ...

  9. Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第3节: recycler的使用和创建

    Netty源码分析第八章: 高性能工具类FastThreadLocal和Recycler 第三节: recycler的使用和创建   这一小节开始学习recycler相关的知识, recycler是n ...

  10. Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第4节: recycler中获取对象

    Netty源码分析第八章: 高性能工具类FastThreadLocal和Recycler 第四节: recycler中获取对象 这一小节剖析如何从对象回收站中获取对象: 我们回顾上一小节demo的ma ...

随机推荐

  1. 怎么学习AOPR使用方法

    Advanced Office Password Recovery作为一款专业级别的Office密码破解工具,对十几种office文档的密码都可以有效的破解.一些用户朋友才开始接触Advanced O ...

  2. JPA学习笔记1——JPA基础

    1.JPA简介: Java持久化规范,是从EJB2.x以前的实体Bean(Entity bean)分离出来的,EJB3以后不再有实体bean,而是将实体bean放到JPA中实现.JPA是sun提出的一 ...

  3. Jenkins配置的邮件无法发送的问题

    一.不要使用系统默认的插件,请使用[Extended E-mail Notification] 二.一下的选项要和你发邮件人填写的保持一致

  4. 洛谷P1629 邮递员送信

    题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要一定的时间.这个邮递员每 ...

  5. Cacti /graphs_new.php SQL Injection Vulnerability

    catalogue . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 other SQL injection vulnerability ...

  6. Irrelevant Elements, ACM/ICPC NEERC 2004, UVa1635

    这种题目最重要的是思路了清晰 #include <cstdio> #include <cstring> ;//sqrt(n)+1 is enough ][]; ]; int a ...

  7. 帝国cms栏目自定义字段首页调用

    例如:增加栏目自定义字段:chushi_bpic 用下面的灵动标签和调用: [e:loop={"select C.classid,C.classname,C.classimg,D.chush ...

  8. C++ 参考网站

    Reference Web cites for the C++ standard library cppreference cplusplus sgi IBM MSDN isocpp More res ...

  9. centos7 建立虚拟目录

    一.安装mysql,直接用yum安装即可,mysql在centos7.0版本中被mariadb替代了. 命令: yum install mysql-server mysql 安装好了,选择修改mysq ...

  10. UVa 437 The Tower of Babylon(经典动态规划)

    传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...