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

1. 使用代码方式进行组件注册【依赖服务类】

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using CastleDemo.Lib;
  7.  
  8. using Castle.Windsor;
  9. using Castle.Windsor.Configuration.Interpreters;
  10. using Castle.MicroKernel.Registration;
  11.  
  12. namespace CastleDemo.Lib.Mgr
  13. {
  14. /// <summary>
  15. /// 管理类
  16. /// </summary>
  17. public partial class Mgr
  18. {
  19. private static IWindsorContainer container = null;
  20.  
  21. /// <summary>
  22. /// 自定义容器和组件注册
  23. /// </summary>
  24. /// <returns></returns>
  25. public static IWindsorContainer GetContainer()
  26. {
  27.  
  28. if (container == null)
  29. {
  30. Type objTypeA = Type.GetType("CastleDemo.Lib.Oracle.OracleDatabase, CastleDemo.Lib.Oracle");
  31. Type objTypeB = Type.GetType("CastleDemo.Lib.Sql.SqlDatabase, CastleDemo.Lib.Sql");
  32.  
  33. //建立容器
  34. IWindsorContainer tmpContainer = new WindsorContainer();
  35.  
  36. //加入组件:旧版
  37. //tmpContainer.AddComponent("CastleDemo.Lib.Oracle.OracleDatabase", typeof(IDatabase), objTypeA);
  38. //tmpContainer.AddComponent("CastleDemo.Lib.Sql.SqlDatabase", typeof(IDatabase), objTypeB);
  39.  
  40. //加入组件:新版
  41. tmpContainer.Register(Component.For(typeof(IDatabase)).ImplementedBy(objTypeA).Named("CastleDemo.Lib.Oracle.OracleDatabase"));
  42. tmpContainer.Register(Component.For(typeof(IDatabase)).ImplementedBy(objTypeB).Named("CastleDemo.Lib.Sql.SqlDatabase"));
  43.  
  44. container = tmpContainer;
  45.  
  46. }
  47. return container;
  48. }
  49. }
  50. }

2. 使用代码方式进行组件注册【不需要依赖】【类似反射的全字符串形式】

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Castle.Windsor;
  7. using Castle.Windsor.Configuration.Interpreters;
  8. using Castle.MicroKernel.Registration;
  9.  
  10. namespace CastleDemo.Lib.Container
  11. {
  12. /// <summary>
  13. /// 管理类
  14. /// </summary>
  15. public partial class Container
  16. {
  17. private static IWindsorContainer container = null;
  18.  
  19. /// <summary>
  20. /// 自定义容器和组件注册
  21. /// </summary>
  22. /// <returns></returns>
  23. public static IWindsorContainer GetContainer()
  24. {
  25.  
  26. if (container == null)
  27. {
  28. Type objType = Type.GetType("CastleDemo.Lib.IDatabase, CastleDemo.Lib");
  29.  
  30. Type objTypeA = Type.GetType("CastleDemo.Lib.Oracle.OracleDatabase, CastleDemo.Lib.Oracle");
  31. Type objTypeB = Type.GetType("CastleDemo.Lib.Sql.SqlDatabase, CastleDemo.Lib.Sql");
  32.  
  33. //建立容器
  34. IWindsorContainer tmpContainer = new WindsorContainer();
  35.  
  36. //加入组件:旧版
  37. //tmpContainer.AddComponent("CastleDemo.Lib.Oracle.OracleDatabase", objType, objTypeA);
  38. //tmpContainer.AddComponent("CastleDemo.Lib.Sql.SqlDatabase", objType, objTypeB);
  39.  
  40. //加入组件:新版
  41. tmpContainer.Register(Component.For(objType).ImplementedBy(objTypeA).Named("CastleDemo.Lib.Oracle.OracleDatabase"));
  42. tmpContainer.Register(Component.For(objType).ImplementedBy(objTypeB).Named("CastleDemo.Lib.Sql.SqlDatabase"));
  43.  
  44. container = tmpContainer;
  45.  
  46. }
  47. return container;
  48. }
  49. }
  50. }

3. 使用配置文件进行组件注册【不需要依赖】

3.1. 定义配置文件

  1. <?xml version="1.0"?>
  2. <configuration>
  3. <configSections>
  4. <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
  5. </configSections>
  6. <castle>
  7. <components>
  8. <component name="CastleDemo.Lib.Oracle.OracleDatabase" type="CastleDemo.Lib.Oracle.OracleDatabase, CastleDemo.Lib.Oracle" service="CastleDemo.Lib.IDatabase, CastleDemo.Lib"/>
  9. <component name="CastleDemo.Lib.Sql.SqlDatabase" type="CastleDemo.Lib.Sql.SqlDatabase, CastleDemo.Lib.Sql" service="CastleDemo.Lib.IDatabase, CastleDemo.Lib"/>
  10. </components>
  11. </castle>
  12. <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

3.2. 读取config配置文件进行组件注册

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using CastleDemo.Lib;
  7.  
  8. using Castle.Windsor;
  9. using Castle.Windsor.Configuration.Interpreters;
  10. using Castle.MicroKernel.Registration;
  11.  
  12. namespace CastleDemo.Run
  13. {
  14. public partial class Helper
  15. {
  16.  
  17. /// <summary>
  18. /// 根据配置文件里的服务名生成对象
  19. /// </summary>
  20. public static void GetFrom_Config()
  21. {
  22.  
  23. IWindsorContainer container = new WindsorContainer(new XmlInterpreter());
  24.  
  25. string vServiceName = "CastleDemo.Lib.Oracle.OracleDatabase";//服务名
  26. vServiceName = "CastleDemo.Lib.Sql.SqlDatabase";
  27.  
  28. if (container != null)
  29. {
  30. IDatabase db = container.Resolve<IDatabase>(vServiceName);
  31. if (db != null)
  32. {
  33. db.Select("..........");
  34. }
  35.  
  36. }
  37.  
  38. }
  39.  
  40. }
  41. }

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.Windsor依赖注入的高级应用与生存周期

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

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

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

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

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

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

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

  5. (译)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.0依赖注入(DI)之生命周期

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

  8. PHP 依赖注入和控制反转再谈(二)

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

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

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

随机推荐

  1. hive sequencefile导入文件遇到FAILED: SemanticException Unable to load data to destination table. Error: The file that you are trying to load does not match the file format of the destination table.错误

    hive sequencefile导入文件遇到FAILED: SemanticException Unable to load data to destination table. Error: Th ...

  2. MFC让进程利用所有处理器核心

    参考资料: http://blog.csdn.net/baodi_z/article/details/1857820 http://blog.csdn.net/cbnotes/article/deta ...

  3. 普通用户无法su到root用户

    问题描述: 普通用户切换回root用户时,密码输入正确仍然报密码错误. 问题解决: 1.1 检查/etc目录下passwd的权限 [root@dev /]# ll/etc/passwd-rw-r--r ...

  4. WPF学习笔记(1)——image控件图片不显示的问题

    说明(2017-6-7 16:08:35): 1. 本来想用winform的,用winform就没这么多破事了. 2. 不过项目要用WPF,拉出一个mediaelement控件,视频正常显示. 3. ...

  5. loadrunner文本检查点

    将脚本切换回代码界面, 在光标闪烁的上行,添加如下的代码: 添加的代码根据你检查的方式不同而不同, 你可以选择其中之一即可. 代码一: web_reg_find("Text=Payment ...

  6. thinkphp验证码乱码的解决办法

    很有可能是入口文件index.php和.htaccess文件要转换成 以UTF-8无BOM格式编码

  7. Java多线程系列——深入重入锁ReentrantLock

    简述 ReentrantLock 是一个可重入的互斥(/独占)锁,又称为“独占锁”. ReentrantLock通过自定义队列同步器(AQS-AbstractQueuedSychronized,是实现 ...

  8. GODOT 3.0 开发快照版本 ALPHA1 释出

    经过将近一年的开发后,master 分支(即未来的Godot 3.0)所有功能特性接近完成,可以在Godot社区里进行更广泛的测试了.因此我们释出了第一个Alpha快照版本,供老用户体验.报告Bug. ...

  9. sam9260 adc module

    /* * driver/char/at91_adc.c * * Copyright (C) 2007 Embedall Technology Co., Ltd. * * Analog-to-digit ...

  10. 使用PD进行数据库建模时的问题

    在PowerDesigner中建立好概念模型后,将其转为物理模型的时候出现如下错误提示: 前面四个是相同的问题,原因是由于没有修改默认设置.可以在Tools--->Model Options-& ...