Castle.Windsor依赖注入的高级应用与生存周期
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 : 容器中只有一个实例将被创建。即一个组件只有一个实例被创建,所有请求的客户使用程序得到的都是同一个实例,同时这也是Castle IOC容器默认的一种处理方式。
5.2. Transient : 这种处理方式与我们平时使用new的效果是一样的,对于每次请求中的每次调用得到的都是一个新的实例,但是无法在请求结束时自动销毁(官方文档中说明必须手工调用容器的Release方法)。
5.3. PerThread: 每线程中只存在一个实例。即对于每一个线程来说是使用了Singleton,也就是每一个线程得到的都是同一个实例。
5.4. PerWebRequest : 每次web请求创建一个新实例。即对于每一次web请求来说是使用了Singleton,也就是每一次web请求得到的都是同一个实例,可以在请求结束时自动销毁。
5.5. Pooled :使用"池化"方式管理组件,可使用PooledWithSize方法设置池的相关属性。对象池的处理方式,对于不再需用的实例会保存到一个对象池中。
5.6. Custom:自定义的生命处理方式。
Castle.Windsor依赖注入的高级应用与生存周期的更多相关文章
- Castle.Windsor依赖注入的高级应用_Castle.Windsor.3.1.0
[转]Castle.Windsor依赖注入的高级应用_Castle.Windsor.3.1.0 1. 使用代码方式进行组件注册[依赖服务类] using System; using System.Co ...
- 小白初学Ioc、DI、Castle Windsor依赖注入,大神勿入(不适)
过了几天,我又来了.上一篇中有博友提到要分享下属于我们abp初学者的历程,今天抽出点时间写写吧.起初,我是直接去看阳光铭睿的博客,看了一遍下来,感觉好多东西没接触过,接着我又去下了github 里面下 ...
- ASP.NET Web API - 使用 Castle Windsor 依赖注入
示例代码 项目启动时,创建依赖注入容器 定义一静态容器 IWindsorContainer private static IWindsorContainer _container; 在 Applica ...
- 基于DDD的.NET开发框架 - ABP依赖注入
返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...
- (译)ABP之依赖注入
原文地址:https://aspnetboilerplate.com/Pages/Documents/Dependency-Injection 什么是依赖注入 传统方式的问题 解决方案 构造函数注入 ...
- Unity文档阅读 第二章 依赖注入
Introduction 介绍Chapter 1 outlines how you can address some of the most common requirements in enterp ...
- [Castle Windsor]学习依赖注入
初次尝试使用Castle Windsor实现依赖注入DI,或者叫做控制反转IOC. 参考: https://github.com/castleproject/Windsor/blob/master/d ...
- Castle Windsor 使MVC Controller能够使用依赖注入
以在MVC中使用Castle Windsor为例 1.第一步要想使我们的Controller能够使用依赖注入容器,先定义个WindsorControllerFactory类, using System ...
- 依赖注入容器之Castle Windsor
一.Windsor的使用 Windsor的作为依赖注入的容器的一种,使用起来比较方便,我们直接在Nuget中添加Castle Windsor,将会自动引入Castle.Core 和 Castle.Wi ...
随机推荐
- Java基础教程:多线程基础(6)——信号量(Semaphore)
Java基础教程:多线程基础(6)——信号量(Semaphore) 信号量 信号量(Semaphore)由一个值和一个指针组成,指针指向等待该信号量的进程.信号量的值表示相应资源的使用情况.信号量S≥ ...
- CSS 按钮水波纹特效
/* 按钮反馈之波纹 */ .ripple { position: relative; /* overflow:hidden */ 打开注释及效果不扩散在外 } .ripple:focus{ out ...
- Linux文件误删恢复
一.需求研究 分析对比debugfs.testdisk 6.14.extundelete,对比各自官网介绍和操作说明本次决定研究extundelete对文件和目录的恢复操作. 二.项目内容 1.工具安 ...
- mysql 事物控制语言
事务控制语言(DTL) 什么是事务 通常,在此之前,我们说,一条语句使用一个分号(;)来结束,并得到执行. 那么我们说,这个“一次性执行”的过程,可以称为“一个事务” ...
- mac tar 解压
1.下载mac上对应rar版本 http://www.rarlab.com/download.htm2.利用tar名解压下载的rarosx-5.4.0.tar.gz,版本可能会更新tar xzvf a ...
- [CF1051F]The Shortest Statement_堆优化dij_最短路树_倍增lca
The Shortest Statement 题目链接:https://codeforces.com/contest/1051/problem/F 数据范围:略. 题解: 关于这个题,有一个重要的性质 ...
- [bzoj4240]有趣的家庭菜园_树状数组
有趣的家庭菜园 题目链接:https://lydsy.com/JudgeOnline/problem.php?id=4240 数据范围:略. 题解: 第一步比较简单,只需要排序之后,每个数不是在左边就 ...
- 【转帖】如果进入CPU的世界,时间会是怎样的?
如果进入CPU的世界,时间会是怎样的? 2018-02-26 20:52:46 world6 阅读数 1295更多 分类专栏: 网络 缓存服务 架构 版权声明:本文为博主原创文章,遵循CC 4.0 ...
- IIS清理缓存
服务器突然断电经常会导致IIS中web项目运行不起来问题,各种报XXXX.dll加载失败,解决方法重新发布,如果重新发布也不行可能就是IIS缓存的问题了. 清理IIS缓存方法: 进入以下文件夹吧对应的 ...
- Resin 与 Tomcat 服务器对比
Resin 与 Tomcat对比(个人总结) 图片来源Tomcat PK Resin 上图对比发现Tomcat对于Resin来说,有诸多优点,但是Resin也有很多优点. 比方说: 速度比较 re ...