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", typeof(IDatabase), 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; } } }
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, CastleDemo.Lib.Oracle"); Type objTypeB = Type.GetType("CastleDemo.Lib.Sql.SqlDatabase, CastleDemo.Lib.Sql"); //建立容器 IWindsorContainer tmpContainer = new WindsorContainer(); //加入组件:旧版 //tmpContainer.AddComponent("CastleDemo.Lib.Oracle.OracleDatabase", objType, objTypeA); //tmpContainer.AddComponent("CastleDemo.Lib.Sql.SqlDatabase", 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; } } }
<?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>
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(".........."); } } } } }
点此下载