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. datatables ajax异步分页

    $('#sample_1').dataTable({ "sAjaxSource": "../table/data", // "bProcessing& ...

  2. xunsearch全文检索初体验

    目录 测试添加数据 测试搜索 简单搜索 稍微复杂的搜索 搜索建议 测试添加数据 ./Indexer.php --source=csv --clean demo 清空现有索引数据 ... 初始化数据源 ...

  3. 温故而知新(java实现)单例模式的七种写法

    第一种(懒汉,线程不安全): Java代码 public class Singleton { private static Singleton instance; private Singleton ...

  4. mysql流程控制

    一 流程控制 delimiter // CREATE PROCEDURE proc_if () BEGIN declare i int default 0; if i = 1 THEN SELECT ...

  5. ffmpeg码率控制

    一.VBR与CBR的含义和区别 VBR是动态码率.CBR是静态码率. VBR(Variable Bitrate)动态比特率.也就是没有固定的比特率,压缩软件在压缩时根据音频数据即时确定使用什么比特率, ...

  6. .NET 调用c++库注意事项

    很久没有更新了,主要还是因为自己懒吧,希望从今天开始坚持至少一周写一篇文章. 调用函数库是正常的,调用完成后,在使用EF进行数据更新时,将发生如下异常信息,而且几乎必现. 行库遇到了错误.此错误的地址 ...

  7. [iOS]通过xib定义Cell然后关联UICollectionView

    先新建一个View的xib,然后删掉自动生成的View,拖进一个UICollectionCell,再新建一个对应的UIView继承UICollectionCell类. OK,接下来该连outlet的就 ...

  8. 页面布局 frameset元素

    frameset.html: <!DOCTYPE html><html lang="en"><head> <meta charset=&q ...

  9. 【bzoj1444】[Jsoi2009]有趣的游戏

    1444: [Jsoi2009]有趣的游戏 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1007  Solved: 334[Submit][Statu ...

  10. php-fpm, nginx ,fastcgi ,php-cgi 关系粗解

    首先,CGI 是干什么的?  CGI 是为了保证web server传递过来的数据是标准格式.CGI  是个协议和 进程没什么关系. CGI 是http服务器于你的本机或者其他电脑上的程序交谈的一种工 ...