1. 使用代码方式进行组件注册【依赖服务类】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using CastleDemo.Lib; using Castle.Windsor; using Castle.Windsor.Configuration.Interpreters; using Castle.MicroKernel.Registration; namespace CastleDemo.Lib.Mgr { /// <summary> /// 管理类 /// </summary> public partial class Mgr { private static IWindsorContainer container = null; /// <summary> /// 自定义容器和组件注册 /// </summary> /// <returns></returns> public static IWindsorContainer GetContainer() { if (container == null) { Type objTypeA = Type.GetType("CastleDemo.Lib.Oracle.OracleDatabase,CastleDemo.Lib.Oracle"); Type objTypeB = Type.GetType("CastleDemo.Lib.sql.sqlDatabase,CastleDemo.Lib.sql"); //建立容器 IWindsorContainer tmpContainer = new WindsorContainer(); //加入组件:旧版 //tmpContainer.AddComponent("CastleDemo.Lib.Oracle.OracleDatabase",typeof(IDatabase),objTypeA); //tmpContainer.AddComponent("CastleDemo.Lib.sql.sqlDatabase",objTypeB); //加入组件:新版 tmpContainer.Register(Component.For(typeof(IDatabase)).ImplementedBy(objTypeA).Named("CastleDemo.Lib.Oracle.OracleDatabase")); tmpContainer.Register(Component.For(typeof(IDatabase)).ImplementedBy(objTypeB).Named("CastleDemo.Lib.sql.sqlDatabase")); container = tmpContainer; } return container; } } }
2. 使用代码方式进行组件注册【不需要依赖】【类似反射的全字符串形式】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Castle.Windsor; using Castle.Windsor.Configuration.Interpreters; using Castle.MicroKernel.Registration; namespace CastleDemo.Lib.Container { /// <summary> /// 管理类 /// </summary> public partial class Container { private static IWindsorContainer container = null; /// <summary> /// 自定义容器和组件注册 /// </summary> /// <returns></returns> public static IWindsorContainer GetContainer() { if (container == null) { Type objType = Type.GetType("CastleDemo.Lib.IDatabase,CastleDemo.Lib"); Type objTypeA = Type.GetType("CastleDemo.Lib.Oracle.OracleDatabase,objType,objTypeB); //加入组件:新版 tmpContainer.Register(Component.For(objType).ImplementedBy(objTypeA).Named("CastleDemo.Lib.Oracle.OracleDatabase")); tmpContainer.Register(Component.For(objType).ImplementedBy(objTypeB).Named("CastleDemo.Lib.sql.sqlDatabase")); container = tmpContainer; } return container; } } }
3. 使用配置文件进行组件注册【不需要依赖】
3.1. 定义配置文件
<?xml version="1.0"?> <configuration> <configSections> <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,Castle.Windsor" /> </configSections> <castle> <components> <component name="CastleDemo.Lib.Oracle.OracleDatabase" type="CastleDemo.Lib.Oracle.OracleDatabase,CastleDemo.Lib.Oracle" service="CastleDemo.Lib.IDatabase,CastleDemo.Lib"/> <component name="CastleDemo.Lib.sql.sqlDatabase" type="CastleDemo.Lib.sql.sqlDatabase,CastleDemo.Lib.sql" service="CastleDemo.Lib.IDatabase,CastleDemo.Lib"/> </components> </castle> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
3.2. 读取config配置文件进行组件注册
using System; using System.Collections.Generic; using System.Linq; using System.Text; using CastleDemo.Lib; using Castle.Windsor; using Castle.Windsor.Configuration.Interpreters; using Castle.MicroKernel.Registration; namespace CastleDemo.Run { public partial class Helper { /// <summary> /// 根据配置文件里的服务名生成对象 /// </summary> public static void GetFrom_Config() { IWindsorContainer container = new WindsorContainer(new XmlInterpreter()); string vServiceName = "CastleDemo.Lib.Oracle.OracleDatabase";//服务名 vServiceName = "CastleDemo.Lib.sql.sqlDatabase"; if (container != null) { IDatabase db = container.Resolve<IDatabase>(vServiceName); if (db != null) { db.Select(".........."); } } } } }