注册多个组件

1.one-by-one注册组件可能是一项非常重复的工作,可以通过Classes或Types注册一组组件(你可以指定一些特定的特征)

三个步骤

注册多个类型通常采取以下结构

container.Register(Classes.FromThisAssembly()
.InSameNamespaceAs<RootComponent>()
.WithService.DefaultInterfaces()
.LifestyleTransient());

可以看成三个不同的步骤

1)选择程序集

  第一步指定Windsor要扫描的程序集,可以使用如下方法(或者使用它的姊妹方法)

Classes.FromThisAssembly()...

2)选择要扫描的程序集后,你可以筛选一些类型来注册,选择下面三种方式

  a. 通过基类型或者实现的接口

    

Classes.FromThisAssembly().BasedOn<IMessage>()

  b. 通过命名空间筛选类型

Classes.FromAssemblyInDirectory(new AssemblyFilter("bin")).InNamespace("Acme.Crm.Extensions")

  c. 通过任何一个条件筛选

Classes.FromAssemblyContaining<MyController>().Where( t=> Attribute.IsDefined(t, typeof(CacheAttribute)))

  d. 获取所有的类型

Classes.FromAssemblyNamed("Acme.Crm.Services").Pick()

附加过滤和配置

通过上面筛选到了类型源,你也可以给它们添加额外的配置或过滤条件,下面我们讨论所有的api使用

BaseOn、Where、Pick 如果使用多次需谨慎

container.Register(
Classes.FromThisAssembly()
.BasedOn<IMessage>()
.BasedOn(typeof(IMessageHandler<>)).WithService.Base()
.Where(Component.IsInNamespace("Acme.Crm.MessageDTOs"))
);

注册所有子类

container.Register(
Classes.FromThisAssembly()
.BasedOn<SmartDispatcherController>()
.Configure(c => c.Lifestyle.Transient)
);

记住  Of 、Pick和其他过滤方法只是缩小注册类型的范围

他们没有指定服务的提供者,也就是说上面的注册只是注册继承了SmartDispatcherController服务的自己类型,SmartDispatcherController并没有被注册

所以调用如下方法将出现异常

var controller = container.Resolve<SmartDispatcherController>();

选择组件的服务

默认的 组件的服务就是类型本身,一些场景下这是不够的,Windsor让你指定明确的服务类型

Base()

container.Register(
Classes.FromThisAssembly()
.BasedOn(typeof(ICommand<>)).WithService.Base(),
Classes.FromThisAssembly()
.BasedOn(typeof(IValidator<>)).WithService.Base()
);

这里我们注册了所有实现ICommand<>和IValidator的类型。

DefaultInterfaces()

container.Register(
Classes.FromThisAssembly()
.InNamespace("Acme.Crm.Services")
.WithService.DefaultInterfaces()
);

FromInterface()

container.Register(
Classes.FromThisAssembly()
.BasedOn<IService>().WithService.FromInterface()
);

AllInterfaces()

When a component implements multiple interfaces and you want to use it as a service for all of them, use WithService.AllInterfaces() method.

Self()

To register the component implementation type explicitly as a service use WithService.Self()

Select()

If none of the above options suits you you can provide your own selection logic as a delegate and pass it to WithService.Select() method.

Registering non-public types

By default only types visible from outside of the assembly will be registered. If you want to include non-public types, you have to start with specifying assembly first, and then call IncludeNonPublicTypes

container.Register(
Classes.FromThisAssembly()
.IncludeNonPublicTypes()
.BasedOn<NonPublicComponent>()
);

  

Configuring registration

container.Register(
Classes.FromAssembly(Assembly.GetExecutingAssembly())
.BasedOn<ICommon>()
.Configure(component => component.LifestyleTransient())
);
container.Register(
Classes.FromAssembly(Assembly.GetExecutingAssembly())
.BasedOn<ICommon>()
.LifestyleTransient()
);
container.Register(
Classes.FromAssembly(Assembly.GetExecutingAssembly())
.BasedOn<ICommon>()
.LifestyleTransient()
.Configure(component => component.Named(component.Implementation.FullName + "XYZ"))
);
container.Register(
Classes.FromThisAssembly()
.BasedOn<ICommon>()
.LifestyleTransient()
.Configure(
component => component.Named(component.Implementation.FullName + "XYZ")
)
.ConfigureFor<CommonImpl1>(
component => component.DependsOn(Property.ForKey("key1").Eq())
)
.ConfigureFor<CommonImpl2>(
component => component.DependsOn(Property.ForKey("key2").Eq())
)
);
container.Register(
Classes.FromThisAssembly()
.BasedOn<ICommon>()
.LifestyleTransient()
.Configure(
component => component.Named(component.Implementation.FullName + "XYZ")
)
.ConfigureIf(
x => x.Implementation.Name.StarsWith("Common"),
component => component.DependsOn(Property.ForKey("key1").Eq())
)
);

castle windsor学习-----Registering components by conventions的更多相关文章

  1. castle windsor学习-----Registering components one-by-one 注册类型

    1.在容器中注册一个类型 container.Register( Component.For<IMyService>() .ImplementedBy<MyServiceImpl&g ...

  2. castle windsor学习-----How components are created

  3. [Castle Windsor]学习依赖注入

    初次尝试使用Castle Windsor实现依赖注入DI,或者叫做控制反转IOC. 参考: https://github.com/castleproject/Windsor/blob/master/d ...

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

    翻译 当使用依赖注入容器时,你首先要向容器中注册你的组件,Windsor使用installers(该类型实现IWindsorInstaller接口)来封装和隔离注册的逻辑,可以使用Configurat ...

  5. castle windsor学习----- Services and Components 两者的定义

  6. castle windsor学习-----XML Inline Parameters 内联参数

    当使用XML配置的时候,可能要给组件指定各种各样的依赖 1.简单的参数 参数名称不区分大小写 <component id="ping" type="Acme.Crm ...

  7. castle windsor学习----- Referencing types in XML 在xm文件中引用类型

    当从xml引用installer的语法如下 <install type="Acme.Crm.Infrastructure.ServicesInstaller, Acme.Crm.Inf ...

  8. castle windsor学习-----Inline dependencies 依赖

    应用程序中的很多组件都会依赖其他的服务组件,很多依赖一些不合法的组件或者容器中没有的组件,例如int类型.string类型.TimeSpan类型 Windsor支持以上的场景,注册API有Depend ...

  9. castle windsor学习-------Container Events 容器的事件

    所有的事件是实现IKernelEvents 接口,已容器的Kernel属性暴露出来 1. AddedAsChildKernel 当前的容器添加子容器或其他容器时触发 2. RemovedAsChild ...

随机推荐

  1. CSS 温故而知新 断句失败

    设置了一定的宽度和高度.但无论是下面哪句都无效. word-break: break-word; word-wrap: break-word; 原因竟然是因为 /* white-space: nowr ...

  2. 常用IIS mime类型

    MIME类型 .woff font/x-woff.json application/json.svgz image/svg+xml .ttf application/octet-stream.eot ...

  3. Unity3d 发动机原理详细介绍

    Unity3d 发动机原理详细介绍 www.MyException.Cn   发布于:2013-10-08 16:32:36   浏览:46次 0     Unity3d 引擎原理详细介绍 体系结构 ...

  4. Modern.IE,创建现代网站的给力开发工具!

    Modern.IE是微软推出的用来帮助开发者创建现代网站的基本开发工具.作为Web攻城师,最头疼的问题莫过于浏览器兼容性测试,各种类型浏览器,各种版本的浏览器,还有各种头疼的前缀等等.Modern.I ...

  5. PL/0编译程序

    Pl/0语言文法的BNF表示: 〈程序〉→〈分程序>. 〈分程序〉→ [<常量说明部分>][<变量说明部分>][<过程说明部分>]〈语句〉 <常量说明部 ...

  6. java并发阻塞队列

    Java 并发编程利用 Condition 来实现阻塞队列 You are here:  开发&语言 - Java 文章 发布于 2017年06月26日  阅读 944 并发编程   什么是阻 ...

  7. Linux mail发送邮件

    发送前一天的监控日志#!/bin/bash source /etc/profile time=`date -d '-1 day' "+%Y%m%d"` #判断服务是否已经启动 se ...

  8. 谷歌浏览器console.log()失效,打印不出来内容

    这个问题困扰好几天了,网上说的都说的是下图: 勾选这三个就好了,但是我的本来就是勾选上的,还是不行. 后来发现这个: 把这个去掉就可以了,如下图: 原来是因为之前调试js的时候,使用了这个过滤,导致对 ...

  9. iOS 平台如何使用 TestFlight 进行 Beta 测试

    使用 TestFlight,你可以向测试人员发布你 App 的 prerelease 版本来收集反馈信息,为将来发布 App 的正式版做准备.现在 TestFlight 是一个可选功能,你也可以不使用 ...

  10. hdu 5381 The sum of gcd 2015多校联合训练赛#8莫队算法

    The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...