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. wow.js使用方法

    近日,在做项目中,需要做到滚动条滑到某个位置时,才能显示动画,网上查询到有个wow.js可以达到要求,现在把使用方法做如下总结: wow.js演示地址 wow.js的github地址 使用方法真是超简 ...

  2. C# 获取当前月第一天和最后一天

    废话不多说,直接上代码 //先获取当前时间 DateTime now = DateTime.Now; //获取当前月的第一天 DateTime d1 = new DateTime(now.Year, ...

  3. 给自定义cell设置分隔线的不同做法

    1.给cell添加一个UIView,设置UIView的高度为1,并设置这个UIView的左.下.右约束. 2.不需要给cell添加任何控件,重写cell的- (void)setFrame:(CGRec ...

  4. tableView使用的易忘技术点(相对于自己)

    1.在tableView设置右向导航指示箭头 cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 2.tableView的系 ...

  5. GraphX的三大图算法

    1. PageRank http://blog.csdn.net/hguisu/article/details/7996185 2. Connected Components 3. Triangle ...

  6. 76.Android之EventBus源码解析

    转载:http://p.codekk.com/blogs/detail/54cfab086c4761e5001b2538 1. 功能介绍 1.1 EventBus EventBus 是一个 Andro ...

  7. Redis集合-Set

    sadd 向一个Set中添加数据 127.0.0.1:6379> sadd set01 1 1 2 2 3 3 (integer) 3127.0.0.1:6379> SMEMBERS se ...

  8. VS2010 asp.net development server 无法展示svg图片

    无解!只能使用IIS Express或者部署到服务器上的IIS能解决! 以下为解释: http://stackoverflow.com/questions/5981309/asp-net-develo ...

  9. Java命名约定

    类名 类名应该是名词, 描述对象.应该按照驼峰式写法,即只有每个单词首字母大写. 接口名称 接口名称应该是形容词,描述功能.应该以“able”.“ible”结尾,否则应该是名词.通常遵循和类名写相同的 ...

  10. Load Average

    在Linux系统下面,有很多的命令可以查看系统的负载情况:比如top,uptime,w,示例如下: [wenchao.ren@l-cmsweb1.ops.cn1 ~]$ w 18:39:10 up 7 ...