应用程序中的很多组件都会依赖其他的服务组件,很多依赖一些不合法的组件或者容器中没有的组件,例如int类型、string类型、TimeSpan类型

Windsor支持以上的场景,注册API有DependsOn方法。该方法接收一个参数(由Dependency类的静态方法返回值提供)

1. 支持静态依赖 Dependency.OnValue

var twitterApiKey = @"the key goes here";

container.Register(
Component.For<ITwitterCaller>().ImplementedBy<MyTwitterCaller>()
.DependsOn(Dependency.OnValue("APIKey", twitterApiKey))
);

这个例子通过名称进行依赖匹配,它将提供对应的值给MyTwitterCaller类中名为“APIKey”的属性或者构造函数参数

2.通过类型依赖

var config = new TwitterApiConfiguration {
// set all the properties here...
}; container.Register(
Component.For<ITwitterCaller>().ImplementedBy<MyTwitterCaller>()
.DependsOn(Dependency.OnValue<TwitterApiConfiguration>(config))
);

3. 设置属性 Setting up properties: Property.ForKey()

container.Register(
Component.For<ICustomer>().ImplementedBy<CustomerImpl>()
.DependsOn(Property.ForKey("Name").Eq("Caption Hook"), Property.ForKey("Age").Eq()));

4. 明确的服务依赖 Dependency.OnComponent()

container.Register(
Component.For<ITransactionProcessingEngine>().ImplementedBy<TransactionProcessingEngine>()
.DependsOn(Dependency.OnComponent("Logger", "secureLogger"))
);

5. 依赖配置文件 appSettings dependencies: Dependency.OnAppSettingsValue()

container.Register(
Component.For<ITwitterCaller>().ImplementedBy<MyTwitterCaller>()
.DependsOn(Dependency.OnAppSettingsValue("timeout", "twitterApiTimeout"))
);

6.

container.Register(
Component.For<MainViewModel>()
.DependsOn(Dependency.OnResource<MyApp.Properties.Resources>("DisplayName", "MainWindowTitle"))
);

Embedded resource dependencies: Dependency.OnResource()

7. Supplying dynamic dependencies

container.Register(
Component.For<ClassWithArguments>()
.LifestyleTransient()
.DynamicParameters((k, d) => d["createdTimestamp"] = DateTime.Now)
);

castle windsor学习-----Inline dependencies 依赖的更多相关文章

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

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

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

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

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

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

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

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

  5. castle windsor学习-----Registering components by conventions

    注册多个组件 1.one-by-one注册组件可能是一项非常重复的工作,可以通过Classes或Types注册一组组件(你可以指定一些特定的特征) 三个步骤 注册多个类型通常采取以下结构 contai ...

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

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

  7. castle windsor学习-----Fluent Registration API 注册

    使用xml配置和fluent注册两种搭配使用需要注意的是: 如果先在WindsorContainer构造函数指明用xml配置进行注册,如下设置 IWindsorContainer container ...

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

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

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

随机推荐

  1. ubuntu 14.04 anaconda安装

    Python的准备工作 Python 一个备受欢迎的点是社区支持很多,有非常多优秀的库或者模块.但是某些库之间有时候也存在依赖,所以要安装这些库也是挺繁琐的过程.但总有人忍受不了这种 繁琐,都会开发出 ...

  2. C/C++ 内存管理总结

    C内存管理 存储时: 执行程序在存储时(没有调入到内存)分为代码区(text).数据区(data)和未初始化数据区(bss)3个部分. 1 代码区(text segment) 存放CPU执行的机器指令 ...

  3. 搭建SSH框架整合Struts2和Spring时,使用@Autowired注解无法自动注入

    © 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架,在进行Struts2和Spring整合时,使用Spring的@Autowired自动注入失败,运行报错java.lan ...

  4. HTTP响应头缓存控制

    在一般Web开发中,有时需要设置特殊的HTTP响应头阻止客户端(一般是浏览器)缓存(使用)该次请求的响应. 这时候大部分开发人员都是百度或谷歌几段代码复制粘贴即了事. 以下简述一下关于缓存控制的几种H ...

  5. CentOS Linux解决网卡报错Bringing up interface eth0.....

    问题描述:在VMware里克隆出来的CentOS Linux,开机执行命令:ifconfig...没有看到eth0网卡.然后重启网卡又报以下错误:Bringing up interface eth0: ...

  6. Linux中的提示符

    root的提示符:# 一般用户的提示符:$

  7. 安装mingw后,在命令窗体编译c文件

    1.编译test.cpp文件 #include<iostream> int main(int argc,char **argv) {   std::cout<<"he ...

  8. LNMP环境搭建(二:MySQL)

    1.获取MySQL官方的rpm包,根据操作系统与需要安装的MySQL版本进行选择,官方地址:https://www.mysql.com/downloads/ # cd /usr/local/src # ...

  9. Q: Why can't I access the Site Settings of my SharePoint site? 'File Not Found'

    Q: I am trying to access the Site Settings of my SharePoint site, but I get a File Not Found error, ...

  10. EasyDSS视频点播服务器实现的多码率点播功能的说明

    EasyDSS流媒体音视频直播与点播服务器软件,是一套提供一站式的转码.点播.直播.检索.回放.录像下载服务的高性能RTMP/HLS/HTTP-FLV流媒体服务,极大地简化了流媒体相关业务的开发和集成 ...