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

时间:2023-02-21 10:44:57

[转]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方法设置池的相关属性

Castle.Windsor依赖注入的高级应用_Castle.Windsor.3.1.0的更多相关文章

  1. Castle&period;Windsor依赖注入的高级应用与生存周期

    1. 使用代码方式进行组件注册[依赖服务类] using System; using System.Collections.Generic; using System.Linq; using Syst ...

  2. 小白初学Ioc、DI、Castle Windsor依赖注入,大神勿入(不适)

    过了几天,我又来了.上一篇中有博友提到要分享下属于我们abp初学者的历程,今天抽出点时间写写吧.起初,我是直接去看阳光铭睿的博客,看了一遍下来,感觉好多东西没接触过,接着我又去下了github 里面下 ...

  3. ASP&period;NET Web API - 使用 Castle Windsor 依赖注入

    示例代码 项目启动时,创建依赖注入容器 定义一静态容器 IWindsorContainer private static IWindsorContainer _container; 在 Applica ...

  4. 基于DDD的&period;NET开发框架 - ABP依赖注入

    返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...

  5. &lpar;译)ABP之依赖注入

    原文地址:https://aspnetboilerplate.com/Pages/Documents/Dependency-Injection 什么是依赖注入 传统方式的问题 解决方案 构造函数注入 ...

  6. Unity文档阅读 第二章 依赖注入

    Introduction 介绍Chapter 1 outlines how you can address some of the most common requirements in enterp ...

  7. 07、NetCore2&period;0依赖注入(DI)之生命周期

    07.NetCore2.0依赖注入(DI)之生命周期 NetCore2.0依赖注入框架(DI)是如何管理注入对象的生命周期的?生命周期有哪几类,又是在哪些场景下应用的呢? -------------- ...

  8. PHP 依赖注入和控制反转再谈&lpar;二&rpar;

    今天有个朋友看到yii2中介绍的依赖注入一头雾水,之前我写过类似的文章发给他看了,可能还没深入理解吧,这里我再通俗点描述下依赖注入的原理吧,尽可能滴说通俗易懂一点吧:先还是扯下概念性滴问题(概念问题我 ...

  9. Spring4笔记4--基于XML的DI(依赖注入)

    基于XML的DI(依赖注入): Bean 实例在调用无参构造器创建了空值对象后,就要对 Bean 对象的属性进行初始化.初始化是由容器自动完成的,称为注入.根据注入方式的不同,常用的有两类:设值注入. ...

随机推荐

  1. iOS---设置输入框的光标位置

    //这里设置光标位置,让光标位置后移10 textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)]; tex ...

  2. APP上架证书无效:解决

    转发:http://www.cnblogs.com/pruple/p/5523767.html 转发:http://blog.csdn.net/sunnyboy9/article/details/50 ...

  3. 【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法

    转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWi ...

  4. Java GC 面试问题

    转自:http://icyfenix.iteye.com/blog/715301 这个帖子的背景是今晚看到je上这张贴:http://www.iteye.com/topic/715256,心血来潮写下 ...

  5. dedecms织梦自定义表单发送到邮箱-用163邮箱发送邮件

    https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=monline_3_dg&wd=dedecms 邮箱&oq=d ...

  6. gulp和webpack之间的区别

    webpack 是一个打包工具 webpack 是用来把你的源文件打包成一个文件的,你做了一系列配置以后,可以用一句 webpack 实现打包的功能. webpack的作用是从若干个文件开始顺藤摸瓜, ...

  7. eclipse集成maven插件

    一.准备工作 1. 安装jdk并配置:https://www.cnblogs.com/diandiangui/p/10002100.html 2. 已安装好 maven并配置:https://www. ...

  8. Centos 7环境下安装配置Hadoop 3&period;0 Beta1简记

    前言 由于以前已经写过一篇Centos 7环境下安装配置2.8的随笔,因此这篇写得精简些,只挑选一些重要环节记录一下. 安装环境为:两台主机均为Centos 7.*操作系统,两台机器配置分别为: 主机 ...

  9. FPGA跨时钟域握手信号的结构

    FPGA跨时钟数据传输,是我们经常遇到的问题的,下面给出一种跨时钟握手操作的电路结构.先上图 先对与其他人的结构,这个结构最大的特点是使用 req 从低到高或者高到低的变化 来表示DIN数据有效并开始 ...

  10. 2018年UI设计趋势概览

    ​互联网产品的用户界面设计趋势是根据用户的不同需求而不断变化的.在仔细分析了过去几年用户界面设计的趋势和创新之后,我们可以发现其背后的一些规律,2018年UI界面设计的趋势如下. 渐变色 在过去的几年 ...