Windsor 实现IOC容器配制, 暴露接口,通过配制文件指定接口实现,从而降低系统内部依赖 C# Code:
IWindsorContainer container =
new WindsorContainer(
new XmlInterpreter( new ConfigResource( " castle ")));
Interface1 myInstance= container.Resolve<Interface1>( " Interface1 ");
// or
myInstance= container.Resolve<Interface1>();
new WindsorContainer(
new XmlInterpreter( new ConfigResource( " castle ")));
Interface1 myInstance= container.Resolve<Interface1>( " Interface1 ");
// or
myInstance= container.Resolve<Interface1>();
App.Config or Web.config 配制
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
< configSections >
< section name ="castle" type ="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</ configSections >
< castle >
< include uri ="file://castleconfiguration.xml" />
</ castle >
</ configuration >
< configuration >
< configSections >
< section name ="castle" type ="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</ configSections >
< castle >
< include uri ="file://castleconfiguration.xml" />
</ castle >
</ configuration >
castleconfiguration.xml 如果是window应用程序,需要把此配制文件copy到bin目录。Windsor Schema 是用来定义配制文件结构的
注意:
- 可以配制生命周期:lifestyle=singleton|thread|transient|pooled|custom|
- 配制构造函数:<parameters>....<参数名>值</参数名>....</parameters>
<?
xml version="1.0" encoding="utf-8"
?>
< configuration xmlns ="MyWindsorSchema" >
< components >
< component id ="httpservicewatcher" type ="GettingStartedPart1.HttpServiceWatcher, GettingStartedPart1" >
< parameters >
< notifiers >
< array >
< item >${email.notifier} </ item >
< item >${alarm.notifier} </ item >
</ array >
</ notifiers >
< Url >different url </ Url >
</ parameters >
</ component >
< component id ="email.notifier" service ="GettingStartedPart1.IFailureNotifier, GettingStartedPart1" type ="GettingStartedPart1.EmailFailureNotifier, GettingStartedPart1" />
< component id ="alarm.notifier" service ="GettingStartedPart1.IFailureNotifier, GettingStartedPart1" type ="GettingStartedPart1.AlarmFailureNotifier, GettingStartedPart1" />
< component id ="form.component" type ="GettingStartedPart1.Form1, GettingStartedPart1" />
< component id ="Interface1" service ="GettingStartedPart1.Interface1, GettingStartedPart1" type ="GettingStartedPart1.Class1, GettingStartedPart1" lifestyle ="transient" />
< component id ="Interface2" service ="GettingStartedPart1.Interface1, GettingStartedPart1" type ="GettingStartedPart1.Class2, GettingStartedPart1" lifestyle ="transient" />
</ components >
</ configuration >
< configuration xmlns ="MyWindsorSchema" >
< components >
< component id ="httpservicewatcher" type ="GettingStartedPart1.HttpServiceWatcher, GettingStartedPart1" >
< parameters >
< notifiers >
< array >
< item >${email.notifier} </ item >
< item >${alarm.notifier} </ item >
</ array >
</ notifiers >
< Url >different url </ Url >
</ parameters >
</ component >
< component id ="email.notifier" service ="GettingStartedPart1.IFailureNotifier, GettingStartedPart1" type ="GettingStartedPart1.EmailFailureNotifier, GettingStartedPart1" />
< component id ="alarm.notifier" service ="GettingStartedPart1.IFailureNotifier, GettingStartedPart1" type ="GettingStartedPart1.AlarmFailureNotifier, GettingStartedPart1" />
< component id ="form.component" type ="GettingStartedPart1.Form1, GettingStartedPart1" />
< component id ="Interface1" service ="GettingStartedPart1.Interface1, GettingStartedPart1" type ="GettingStartedPart1.Class1, GettingStartedPart1" lifestyle ="transient" />
< component id ="Interface2" service ="GettingStartedPart1.Interface1, GettingStartedPart1" type ="GettingStartedPart1.Class2, GettingStartedPart1" lifestyle ="transient" />
</ components >
</ configuration >
Service look up notation
The following notation is used to describe a service reference.
${componentid}
Configuration Property evaluation