博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Orchard源码分析(4.1):Orchard.Environment.CollectionOrderModule类
阅读量:4357 次
发布时间:2019-06-07

本文共 2058 字,大约阅读时间需要 6 分钟。

CollectionOrderModule类是一个(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对某些组件是顺序敏感的。
参考资料:

转载于:https://www.cnblogs.com/lhxsoft/p/5322485.html

你可能感兴趣的文章
Android关于buildToolVersion与CompileSdkVersion的区别
查看>>
袋鼠云日志,日志分析没那么容易
查看>>
缓存穿透 缓存雪崩 缓存并发
查看>>
了解你的Linux系统:必须掌握的20个命令
查看>>
js setInterval 启用&停止
查看>>
knockoutJS学习笔记04:监控属性
查看>>
Linux下启动/关闭Oracle
查看>>
session和cookie的区别
查看>>
oracle 数据库、实例、服务名、SID
查看>>
web.xml文件的作用
查看>>
linux下oracle调试小知识
查看>>
alert弹出窗口,点击确认后关闭页面
查看>>
oracle问题之数据库恢复(三)
查看>>
单点登陆(SSO)
查看>>
HR,也确实“尽职尽责”
查看>>
MaxComputer 使用客户端配置
查看>>
20190823 顺其自然
查看>>
阅读《余生有你,人间值得》有感
查看>>
每日英语
查看>>
SpringCloud+feign 基于Springboot2.0 负载均衡
查看>>