Castle Windsor 学习-----Installer的几种安装方式

来源:互联网 发布:百度地图 api 知乎 编辑:程序博客网 时间:2024/05/19 11:17

翻译

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

 

 

  

 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 买二手房过户后发现房顶漏水怎么办 钻石绣的钻两个粘在一起怎么办 开修理部的一天不干活都着急怎么办 调好的车漆时间长了有点稠怎么办 下滤鱼缸下水管声音大怎么办 家里水管不开水龙头就总是响怎么办 生完儿子后腹股沟特黑怎么办 因盗窃罪出狱数额较大还不起怎么办 司法拍卖房原房主拒绝搬离怎么办 wps卡了没反应没有保存怎么办 微信扫描支付环境有风险怎么办 手机安装东西突然要扫描很久怎么办 我惹朋友真的很生气了怎么办 买了保险后又犯杀人罪 保险怎么办 水瓶盖了拧歪了打不开怎么办 刚买的老干妈盖子打不开怎么办? 门冬胰岛素的盖子转不动怎么办 diy做的蝴蝶结上的歪了怎么办 电脑显示电源已接通未充电怎么办 微博账号被锁无法登录怎么办 惠头条登录过两个账号被锁了怎么办 被两个月大的小狗咬破皮了怎么办 阿根廷认证出口声明成分错了怎么办 花盆土里面有白色的虫子怎么办 刚买的月季苗叶子黄了怎么办? 月季新出的芽中间黑了怎么办 龙须的茎被折断了怎么办? 扦插的月季跟部总发黑怎么办 用过皮炎平后的黑印怎么办 兰花现在不管王莽岭了员工怎么办 家里很穷妈妈还不知道省钱怎么办 执业医师电子注册信息弄错了怎么办 开过花后的百合花根怎么办 荔枝吃起来有股保鲜剂的味道怎么办 厂停产不宣布倒闭又不赔钱怎么办 二审判决生效后发现判错了怎么办 应收账款质押后附属的担保怎么办 房子借款合同和抵押合同丢了怎么办 离婚判决不服上诉期限过了怎么办 法院判完执行厅不执行怎么办 民事判决书下来又有新的证据怎么办