castle 组件化开发应用配制示例

本文介绍如何使用Windsor实现IOC容器配置,包括通过配置文件指定接口实现的方法来减少系统内部依赖。文中提供了C#代码示例及详细的配置文件结构说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例

Windsor 实现IOC容器配制, 暴露接口,通过配制文件指定接口实现,从而降低系统内部依赖 C# Code:

IWindsorContainer container = 
                 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 >

castleconfiguration.xml  如果是window应用程序,需要把此配制文件copy到bin目录。Windsor Schema 是用来定义配制文件结构的

注意:

  1. 可以配制生命周期:lifestyle=singleton|thread|transient|pooled|custom|
  2. 配制构造函数:<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 >

 

Service look up notation

The following notation is used to describe a service reference.

${componentid}

Configuration Property evaluation

This notation is used with a properties node and allows the developer to expand the value on the properties node to as a node value or attribute value. #{propertyname}

 

 

 

 

转载于:https://www.cnblogs.com/sgciviolence/archive/2008/06/17/1223691.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值