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 ...
随机推荐
- js原生设计模式——13桥接模式(相同业务逻辑抽象化处理的职责链模式)
桥接模式之多元化类之间的实例化调用实例 <!DOCTYPE html><html lang="en"><head> <meta ch ...
- log4net的分类型输出文件的配置
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...
- SVG的基础使用
SVG的基础使用: <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...
- Chrome 报 Resource interpreted as Script but transferred with MIME type text/plain 警告的解决办法
http://www.2cto.com/os/201312/262437.html 安装了VS2012之后,chrome在加载页面的时候会报 Resource interpreted as Scrip ...
- Bootstrap入门(二十二)组件16:列表组
Bootstrap入门(二十二)组件16:列表组 列表组是灵活又强大的组件,不仅能用于显示一组简单的元素,还能用于复杂的定制的内容. 1.默认样式列表组 2.加入徽章 3.链接 4.禁用的列表组 5. ...
- 正则匹配所有的a标签
<a\b[^>]+\bhref="([^"]*)"[^>]*>([\s\S]*?)</a>分组1和分组2即为href和value解释: ...
- Exiting the Matrix: Introducing Metasploit's Hardware Bridge
Metasploit is an amazing tool. You can use it to maneuver through vast networks, pivoting through se ...
- Oracle_关联查询
1. 等值连接(Equijoin).非等值连接(Non-Equijoin).外连接(Outer join):-->左外连接-->右外连接.自连接(Self join) 交叉连接(Cross ...
- Superwebsocket 模拟微信聊天室
在园子里潜水几年了,工作以来算是有些积累,突然想写点东西方便以后温故而知新,希望自己能够坚持下去. 关于Superwebsocket的介绍我就不多说了,请点击:http://www.cnblogs.c ...
- HUST 1541 Student’s question
1541 - Student’s question 时间限制:1秒 内存限制:128兆 696 次提交 134 次通过 题目描述 YYis a student. He is tired of calc ...