Orchard源码分析(4.1):Orchard.Environment.CollectionOrderModule类
CollectionOrderModule类是一个Autofac模块(Module,将一系列组件和相关的功能包装在一起),而非Orchard模块。其作用是保证多个注册到容器的组件能按FIFO(First In First Out)的顺序提取。下面举例说明:
1、创建ICustomerService接口:
2、创建两个实现ICustomerService接口的类:
3、测试:
1、创建ICustomerService接口:
public interface ICustomerService { }
public class DefaultCustomerService : ICustomerService { }
public class VIPCustomerService : ICustomerService { }
[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,将能确保测试通过:
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
参考资料:
Autofac:Structuring With Modules
Autofac:Activation events
标签:
Orchard
, ASP.NET MVC
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构