BatchRegistration Facility是一个辅助Castle IOC容器配置的扩张单元。我们使用他可以很方便在容器中加载Component和Facility,你可以把单独的Component或者Facility编译成dll,然后通过配置文件用BatchRegistration Facility“一次性”或者“筛选”后加入到容器中而无需硬编码AddComponent和AddFacility,灵活性是很高的~
1)加载程序集中所有定义了Castle.Model.CastleComponentAttribute 的组件
2)除Component1不会加载,其他定义了CastleComponentAttribute都会被加载
4)加载Faclity
其实这个Facility做的就是根据配置扫描程序集然后动态加载到容器里面,虽然很简单却很灵活。比如你想更改一个component的行为,你只需要替换掉这个component的dll就行了,其他部分无需重新编译。
【使用方法】
1)建立这个Facility的配置
2)声明组件的class并把他编辑成配置文件里面MyAssembly.dll.
3)使用Facility,这样Component1和Component2就自动被加载了。

1)加载程序集中所有定义了Castle.Model.CastleComponentAttribute 的组件
<facility id="batchregistration">
<assemblyBatch name="MyAssembly" useAttributes="true" />
</facility>
<assemblyBatch name="MyAssembly" useAttributes="true" />
</facility>
2)除Component1不会加载,其他定义了CastleComponentAttribute都会被加载
<facility id="batchregistration">
<assemblyBatch name="MyAssembly" useAttributes="true">
<exclude type="MyAssembly.Component1" />
</assemblyBatch>
</facility>
<assemblyBatch name="MyAssembly" useAttributes="true">
<exclude type="MyAssembly.Component1" />
</assemblyBatch>
</facility>
3)只加载Component2
<facility id="batchregistration">
<assemblyBatch name="MyAssembly" useAttributes="false">
<include key="other" component="MyAssembly.Component2" />
</assemblyBatch>
</facility>
<assemblyBatch name="MyAssembly" useAttributes="false">
<include key="other" component="MyAssembly.Component2" />
</assemblyBatch>
</facility>
4)加载Faclity
<facility id="batchregistration">
<addFacility id="facility1" type="MyAssembly.Facility1, MyAssembly" />
<addFacility id="facility2" type="MyAssembly.Facility2, MyAssembly" />
</facility>
<addFacility id="facility1" type="MyAssembly.Facility1, MyAssembly" />
<addFacility id="facility2" type="MyAssembly.Facility2, MyAssembly" />
</facility>
其实这个Facility做的就是根据配置扫描程序集然后动态加载到容器里面,虽然很简单却很灵活。比如你想更改一个component的行为,你只需要替换掉这个component的dll就行了,其他部分无需重新编译。

【使用方法】
1)建立这个Facility的配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<facilities>
<facility id="batchregistration">
<assemblyBatch name="MyAssembly" useAttributes="true" />
</facility>
</facilities>
</configuration>
<configuration>
<facilities>
<facility id="batchregistration">
<assemblyBatch name="MyAssembly" useAttributes="true" />
</facility>
</facilities>
</configuration>
2)声明组件的class并把他编辑成配置文件里面MyAssembly.dll.
[CastleComponent("comp1")]
public class Component1
{
public Component1()
{
}
}
[CastleComponent("comp2")]
public class Component2
{
public Component2()
{
}
}
public class Component1
{
public Component1()
{
}
}
[CastleComponent("comp2")]
public class Component2
{
public Component2()
{
}
}
3)使用Facility,这样Component1和Component2就自动被加载了。
IWindsorContainer container = new WindsorContainer("../../CastleConfig.xml");
container.AddFacility( "batchregistration", new BatchRegistrationFacility() );
container.AddFacility( "batchregistration", new BatchRegistrationFacility() );
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?