1、逐个注册组件
即对每个接口通过代码指定其实现类,代码:

container.Register(
Component.For<IMyService>() //接口
.ImplementedBy<MyService>() //实现类
);

典型应用场景:例如定义了一个日志记录接口,放到一个独立程序集中。具体实现可能有多种方式(日志以文本文件/XML文件/数据库等不同方式存储),则可以为每个实现类建立一个独立的程序集,在各程序集中将自身注册为接口的实现。这样当我们需要日志的某个存储形式时,选择对应的dll即可

2、按规则批量注册
和1比较类似,不同的是,不用逐个指定接口和实现类,而是指定一个规则,Windsor会用规则去匹配和注册当前应用中所有程序集。代码:

container.Register(Classes.FromThisAssembly()    //当前程序集,也可以调用其它方法,如FromAssemblyInDirectory()等
.InSameNamespaceAs<RootComponent>() //与RootComponent类具有相同的命名空间,还可以用InNamespace("Com.Spbdev")直接指定命名空间
.WithService.DefaultInterfaces()
.LifestyleTransient()); //生命周期

3、按程序集安装注册
与按照规则批量注册类似,差别在于每个程序集内部自己实现一个IWindsorInstaller接口来定义注册规则。也就是将注册规则下放到程序集。
首先,需要指定对哪些程序集进行安装注册(只指定对程序集的搜索规则):
container.Install(FromAssembly.InDirectory(new AssemblyFilter("Extensions")));//Extensions目录下的所有程序集。

其次,每个程序集内通过一个或多个实现了IWindsorInstaller接口的类,来定义哪些Interface和实现类要注册到容器。
如下代码是官网上的一个范例:

public class RepositoriesInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Classes.FromThisAssembly()
.Where(Component.IsInSameNamespaceAs<RepositoriesInstaller>())
.WithService.DefaultInterfaces()
.LifestyleTransient());
}
}

意思是当前程序集中,与RepositoriesInstaller具有相同命名空间的接口、实现,都注册到IOC容器中。

4、XML配置文件注册
用构造函数方式注册:

IWindsorContainer container = new WindsorContainer("dependencies.config");

或通过Install方法

container.Install(
Configuration.FromXmlFile("dependencies.config"));

配置文件格式如下

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<installers>
<install type="WindsorInstaller.CustomerInstaller,WindsorInstaller"/>
<install type="WindsorInstaller.SecondInstaller,WindsorInstaller"/>
</installers>
</configuration>

Windsor自动获取xml文件中的installers

也可以写在应用程序配置文件中

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>
<castle>
<installers>
<install type="WindsorInstaller.CustomerInstaller,WindsorInstaller"/>
<install type="WindsorInstaller.SecondInstaller,WindsorInstaller"/> <!--查找该程序集下所有IWindsorInstaller接口的类型进行注册-->
<!--<install assembly="WindsorInstaller"></install>--> <!--查找dll文件-->
<!--<install directory="Extensions" fileMask="*.dll"></install>-->
</installers>
</castle>
</configuration>

Castle Windsor 注册组件的更多相关文章

  1. 【转】Castle Windsor之组件注册

    [转]Castle Windsor之组件注册 注册方式较多,大体有这么几种,学习得比较粗浅,先记录: 1.逐个注册组件 即对每个接口通过代码指定其实现类,代码: container.Register( ...

  2. ASP.NET MVC Castle Windsor 教程

    一.[转]ASP.NET MVC中使用Castle Windsor 二.[转]Castle Windsor之组件注册 平常用Inject比较多,今天接触到了Castle Windsor.本篇就来体验其 ...

  3. Castle Windsor常用介绍以及其在ABP项目的应用介绍

    最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castle Windsor项目常用方法介绍和关于ABP的使用总结 1.下载 ...

  4. 在ABP项目的应用Castle Windsor

    Castle Windsor常用介绍以及其在ABP项目的应用介绍 最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castl ...

  5. castle windsor学习-----Registering components one-by-one 注册类型

    1.在容器中注册一个类型 container.Register( Component.For<IMyService>() .ImplementedBy<MyServiceImpl&g ...

  6. [Castle Windsor]学习依赖注入

    初次尝试使用Castle Windsor实现依赖注入DI,或者叫做控制反转IOC. 参考: https://github.com/castleproject/Windsor/blob/master/d ...

  7. IoC - Castle Windsor 2.1

    找过一些Windsor教程的文章,博客园上TerryLee有写了不少,以及codeproject等也有一些例子,但都讲的不太明了.今天看到Alex Henderson写的一个系列,非常简单明了.下面是 ...

  8. Castle Windsor 学习-----Installer的几种安装方式

    翻译 当使用依赖注入容器时,你首先要向容器中注册你的组件,Windsor使用installers(该类型实现IWindsorInstaller接口)来封装和隔离注册的逻辑,可以使用Configurat ...

  9. 依赖注入容器之Castle Windsor

    一.Windsor的使用 Windsor的作为依赖注入的容器的一种,使用起来比较方便,我们直接在Nuget中添加Castle Windsor,将会自动引入Castle.Core 和 Castle.Wi ...

随机推荐

  1. java代码switch语句求分数等级

    总结:从键盘输入分数----- 如果在0到100内,则输出等级 小于0或者是大于100都不能输出,这里用if-else条件判断. package com.c2; import java.util.Sc ...

  2. 1115 Counting Nodes in a BST

    题意:给出一棵二叉搜索树的插入序列,要求该树最后两层的结点个数. 思路:在树结点中增加一个数据域layer,表示该结点所在的层次.另外,设置数组level[]和变量maxLevel,level[i]表 ...

  3. 微信小程序的基本认识

    小程序与公众号的区别 小程序,不支持关注,消息推送等营销手段. 小程序更倾向于产品,公众号更倾向于营销. 在系统权限方面,小程序能够获得更多. 小程序与APP的区别 小程序,面向微信用户.app面向所 ...

  4. 第七章 JVM体系结构与工作方式

    JVM能跨计算机体系结构来执行Java字节码,主要是由于JVM屏蔽了与各个计算机平台的软件和硬件之间的差异. 7.1 JVM体系结构 7.1.1 何谓JVM 模拟一个计算机来达到一个计算机所具有的计算 ...

  5. Linux平台总线驱动设备模型

    platform总线是一种虚拟的总线,相应的设备则为platform_device,而驱动则为platform_driver.Linux 2.6的设备驱动模型中,把I2C.RTC.LCD等都归纳为pl ...

  6. Git 常用命令速查手册

    来源:https://www.jianshu.com/p/5ee9897b6b65 1.初始化仓库 git init 2.将文件添加到仓库 git add 文件名 # 将工作区的某个文件添加到暂存区 ...

  7. Python图片转字符画

    PIL安装很麻烦,推荐下载exe直接安装 PIL官网:http://pythonware.com/products/pil/ 但现在里面下载链接访问不了,我把32位和64位版本上传到博客园以供下载 P ...

  8. MongoDB在Java下的增删查改

    我们总不能一直使用cmd对数据库操作,数据库总是要在程序中使用的.今天来说一下怎么通过Java调用MongoDB. 学习一下最基本也是最常用的增删查改语句,这是使用数据库的基础. 注意事项: 1.要打 ...

  9. leetcode821

    vector<int> shortestToChar(string S, char C) { vector<int> V; ; int AYC[N]; ; ; i < S ...

  10. Physics Material

    [Physics Material] 1. The Physics Material is used to adjust friction and bouncing effects of collid ...