[转]Castle.Windsor依赖注入的高级应用_Castle.Windsor.3.1.0

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", 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;
}
}
}

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, 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;
}
}
}

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("..........");
} } } }
}

4.

5. Castle容器的组件生存周期,主要有如下几种

5.1. Singleton : 容器中只有一个实例将被创建

5.2. Transient : 每次请求创建一个新实例

5.3. PerThread: 每线程中只存在一个实例

5.4. PerWebRequest : 每次web请求创建一个新实例

5.5. Pooled :使用"池化"方式管理组件,可使用PooledWithSize方法设置池的相关属性

最新文章

  1. 源代码管理工具之SVN
  2. oracle procedure
  3. eCos系统CPU负载测量
  4. opencv 常用函数介绍
  5. rsyslog 传输mysql 日志
  6. 循环之while
  7. PHP - 设置地址栏小图标
  8. android平台TextView使用ImageSpan画廊GIF图像
  9. Scroll View 深入
  10. shiro权限控制(一):shiro介绍以及整合SSM框架
  11. python学习之爬虫(一) ——————爬取网易云歌词
  12. 使用Node.js搭建一个本地服务器
  13. linux中mv命令使用详解
  14. Java线程-异常处理
  15. Ubuntu14.04安装 HP DeskJet GT 5820 打印机的方法
  16. vue stylus 格式化问题
  17. python经典案例
  18. SQLServer数据库自增长标识列的更新修改操作
  19. Kali Linux 与 BackTrack Linux
  20. Angular CLI 使用教程指南参考

热门文章

  1. vue项目的搭建
  2. electron 创建右键菜单
  3. 项目bug
  4. 在CentOS6.5上安装/启动PostgreSQL
  5. 获取控制台窗口句柄GetConsoleWindow
  6. Docker、Dockerfile、Docker镜像、容器这些都是什么鸟?
  7. [转]JS组件系列——表格组件神器:bootstrap table
  8. idea中git git pull push需要反复输入密码
  9. 适配器模式,装饰模式,代理模式异同&lt;转&gt;
  10. 远程log调试工具console.re