Castle Windsor 学习-----Installer的几种安装方式
翻译
当使用依赖注入容器时,你首先要向容器中注册你的组件,Windsor使用installers(该类型实现IWindsorInstaller接口)来封装和隔离注册的逻辑,可以使用Configuration和FromAssembly来完成工作。
Installers是实现了IWindsorInstaller接口的简单类型,只有一个Install方法,该方法接收container参数,该参数使用 fluent registration API方式来注册组件
public class RepositoriesInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(AllTypes.FromAssemblyNamed("Acme.Crm.Data")
.Where(type => type.Name.EndsWith("Repository"))
.WithService.DefaultInterfaces()
.Configure(c => c.LifeStyle.PerWebRequest));
}
}
备注:使用单独的installer来注册一些相关的服务信息(如repositories、controllers)
installer必须是公共的而且包含一个公共的默认构造函数,Windsor使用InstallerFactory扫描公开的installers
使用installers
var container = new WindsorContainer();
container.Install(
new ControllersInstaller(),
new RepositoriesInstaller(),
// and all your other installers
);
当有新的intallers,要记得在这里注册
如果installers过多,添加这些代码有点乏味。Windsor提供FromAssembly静态类和Configuration来进行外部的配置
FromAssembly静态类会扫描指定的程序集中所有实现IWindsorInstaller接口的类,如果添加新的installer,则不用添加额外代码,Windsor会自动检测到类型并实例化
container.Install(
FromAssembly.This(),
FromAssembly.Named("Acme.Crm.Bootstrap"),
FromAssembly.Containing<ServicesInstaller>(),
FromAssembly.InDirectory(new AssemblyFilter("Extensions")),
FromAssembly.Instance(this.GetPluginAssembly())
);
installer的注册没有特定的顺序,所以你不知道哪个installer先被注册,如果要进行特殊的排序,使用InstallerFactory来实现
public class WindsorBootstrap : InstallerFactory
{ public override IEnumerable<Type> Select(IEnumerable<Type> installerTypes)
{
var retval = installerTypes.OrderBy(x => this.GetPriority(x));
return retval;
} private int GetPriority(Type type)
{
var attribute = type.GetCustomAttributes(typeof(InstallerPriorityAttribute), false).FirstOrDefault() as InstallerPriorityAttribute;
return attribute != null ? attribute.Priority : InstallerPriorityAttribute.DefaultPriority;
} } [AttributeUsage(AttributeTargets.Class)]
public sealed class InstallerPriorityAttribute : Attribute
{
public const int DefaultPriority = ; public int Priority { get; private set; }
public InstallerPriorityAttribute(int priority)
{
this.Priority = priority;
}
} container.Install(FromAssembly.This(new WindsorBootstrap()));
给installer添加特性进行排序
1. FromAssembly.This() 调用该方法的程序集
2. FromAssembly.Named("Acme.Crm.Bootstrap")
Install from assembly with specified assembly name using standard .NET assembly locating mechanism.
You can also provide path to a .dll or .exe file when you have the assembly in some non-standard location.
如果是.dll,要使用绝对路径
3. FromAssembly.Containing
程序集中包含特定的类型
Configuration 类
读取xml文件的配置信息
container.Install(
Configuration.FromAppConfig(),
Configuration.FromXmlFile("settings.xml"),
Configuration.FromXml(new AssemblyResource("assembly://Acme.Crm.Data/Configuration/services.xml"))
);
settings.xml文件格式如下
<?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 学习-----Installer的几种安装方式的更多相关文章
- 【转】vue.js三种安装方式
Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手 ...
- vue.js三种安装方式
Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手 ...
- Hive的三种安装方式(内嵌模式,本地模式远程模式)
一.安装模式介绍: Hive官网上介绍了Hive的3种安装方式,分别对应不同的应用场景. 1.内嵌模式(元数据保村在内嵌的derby种,允许一个会话链接,尝试多个会话链接时会报错) ...
- Python模块常用的几种安装方式
Python模块安装方法 一.方法1: 单文件模块直接把文件拷贝到 $python_dir/Lib 二.方法2: 多文件模块,带setup.py 下载模块包,进行解压,进入模块文件夹,执行:pytho ...
- wdcp支持两种安装方式
v3.2版本已发布,支持多PHP版本共存共用,支持SSL证书,更多可看论坛 v3版讨论区 更多安装说明请看 http://www.wdlinux.cn/bbs/thread-57643-1-1.htm ...
- Python模块常用的几种安装方式 【转】
转自:http://blog.chinaunix.net/uid-23500957-id-3781907.html 一. 单文件模块直接把文件拷贝到 $python_dir/Lib 二. 多文件模块, ...
- grub安装的 三种安装方式
1. 引言 grub是什么?最常态的理解,grub是一个bootloader或者是一个bootmanager,通过grub可以引导种类丰富的系统,如linux.freebsd.windows等.但一旦 ...
- Linux 下wdcp支持两种安装方式
wdcp支持两种安装方式1 源码编译 此安装比较麻烦和耗时,一般是20分钟至一个小时不等,具体视机器配置情况而定2 RPM包安装 简单快速,下载快的话,几分钟就可以完成源码安装(ssh登录服务器,执行 ...
- [Castle Windsor]学习依赖注入
初次尝试使用Castle Windsor实现依赖注入DI,或者叫做控制反转IOC. 参考: https://github.com/castleproject/Windsor/blob/master/d ...
随机推荐
- 让php Session 存入 redis 配置方法
首先要做的就是安装redis 安装方法:http://redis.io/download Installation Download, extract and compile Redis with: ...
- catalan卡特兰数
卡塔兰数是组合数学中一个常在各种计数问题中出现的数列.以比利时的数学家欧仁·查理·卡塔兰(1814–1894)命名.历史上,清代数学家明安图(1692年-1763年)在其<割圜密率捷法>最 ...
- 单选按钮(RadioButton)与复选框(CheckBox)的功能与用法
单选按钮(RadioButton)和复选框(CheckBox).状态开关按钮(ToggleButton)与开关(Switch)是用户界面中最普通的UI组件,他们都继承了Button类,因此都可直接使用 ...
- GridView等表格模板列绑定数据的方法
//绑定GridView每一行中的CheckBoxList protected void GridView1_RowDataBound(object sender, GridViewRowEventA ...
- log4j.rootLogger详细说明
http://blog.csdn.net/anlina_1984/article/details/5313023
- puppet的配置清单书写
puppet的配置清单书写 1使用数组,合并同类的 例如你想安装很多软件,如果分开来写的话,很麻烦,不简洁,这时我们可以使用数组来完成 以前我们这样来写 class packages{ package ...
- ASP.NET Zero--14.一个例子(7)商品分类管理-分类搜索及分页
分类搜索实现 1.添加搜索框 打开Index视图,添加一个搜索框,代码如下: ... <div class="portlet light"> <div class ...
- 解析.NET对象的跨应用程序域访问--AppDomain(上篇)
在目前的项目开发中,分布式开发已经逐渐成为主流.一个项目要是没有采用分布式架构,都不好意思跟别人说这是一个完整的项目.这句话虽然有些过激,但是随着人们对效率的要求在提高,以及产品需要提升用户体验.只有 ...
- jQuery.sort对DOM元素进行排序
实例: 每个tr的第三列显示的都是数字,我们就以这数字列作为排序依据,方法就是利用jquery的sort()方法. 首先,利用jquery选择器获取每个tr元素,获取回来是一个数据: var $trs ...
- IO的构造方法
package com.file; /* File的构造方法: File(String pathname); File(File parent,String child); File(String p ...