翻译

  当使用依赖注入容器时,你首先要向容器中注册你的组件,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>

  

最新文章

  1. php编译内容
  2. WAP端 穿透问题和解决方法
  3. OC基础--Hello Shit
  4. 制作.frameWork的最全最真实的解决办法
  5. Unity4.5版本DLL库名字问题
  6. communication between threads 线程间通信 Programming Concurrent Activities 程序设计中的并发活动 Ada task 任务 Java thread 线程
  7. hdu 5340 Three Palindromes
  8. [Java] Java 获取数据库所有表基本信息和表中的所有列基本信息代码
  9. Java实现图片压缩代码,图片大小转换
  10. css 梯形标签页
  11. HTML5----input-datalist输入框自己主动提示功能
  12. 简单验证码的识别:Bitmap类的使用
  13. Django之Ajax文件上传
  14. Django 内的母版-子html规则
  15. 在 Windows 中为高级用户配置 IPv6 的指南
  16. openstack安装-计算节点-neutron服务安装
  17. 在VMware14上安装centos6.5
  18. Python 高级编程 ——观察者模式
  19. sqlserver运行脚本数据出现 内存不足的解决办法
  20. MongoDB中_id(ObjectId)生成

热门文章

  1. win8 64位+Oracle 11g 64位下使用PL/SQL Developer 的解决办法
  2. Linux安装配置VPN服务器
  3. log4j.rootLogger详细说明
  4. 汇编实现HelloWorl!
  5. SQL Server-索引故事的遥远由来,原来是这样的?
  6. 《JAVASCRIPT高级程序设计》window/location/navigator/screen/history对象
  7. SVN-TortoiseSVN安装和常用操作步骤
  8. tableView的总结
  9. didReceiveMemoryWarning-内存警告处理方法-iOS
  10. 小米wifi局域网下播放硬盘影片使用方法