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写基础的九九乘法表

    package com.aaa; public class Xox { public static void main(String[] args) { for (int i = 1; i <= ...

  2. 转载:Oracle RAC日常基本维护命令

    本文转载自: https://blog.csdn.net/tianlesoftware/article/details/5358573 Oracle RAC日常基本维护命令 好文转载, Oracle  ...

  3. 第13章 Spring框架的设计理念与设计模式分析

    13.1 Spring的骨骼架构 最核心的组件是:Core, Context, Bean. 13.1.1 Spring的设计理念 Spring最核心的组件是Bean组件.Spring解决的最关键的问题 ...

  4. Zabbix 客户端自定义端口监控

    http://www.linuxidc.com/Linux/2013-05/83780.htm 

  5. 生成signature

    签名规则说明 1. 将参数先按键值排序(只做一级排序),进行key和value的拼接 2. 拼接完后,在最后面再拼接上分配的 appSecret 然后用sha1对拼接串加密 签名示例 假如传入参数: ...

  6. leetcode690

    class Solution { public: int getImportance(vector<Employee*> employees, int id) { ; map<int ...

  7. MYCAT实战之分片迁移

    实践扩容 1.要求: travelrecord 表定义为10个分片,尝试将10个分片中的 2 个分片转移到第二台MySQL上, 并完成记录要求,最快的数据迁移做法,中断业务时间最短 2.针对分片以及迁 ...

  8. Python基础学习五 内置函数

    1.函数补充: 1)函数返回值return可以有多个 2)补充示例: nums = [0,1,2,3,4,5,6,7,8] #如何将list里面的元素变为字符串类型 new_nums = [str(x ...

  9. centos 中没有 ifcfg-eth0 配置文件的解决办法

    用 CentOS-6.5-i386-LiveDVD.iso 镜像安装好CentOS 6.5系统后(已经把系统写入硬盘),发现ip在每次重启后都会还原,用ifconfig查看是有eth0网卡的(也有可能 ...

  10. 【原】Coursera—Andrew Ng斯坦福机器学习(0)——课程地址和软件下载

    斯坦福大学机器学习 课程信息 机器学习是一门研究在非特定编程条件下让计算机采取行动的学科.最近二十年,机器学习为我们带来了自动驾驶汽车.实用的语音识别.高效的网络搜索,让我们对人类基因的解读能力大大提 ...