应用程序中的很多组件都会依赖其他的服务组件,很多依赖一些不合法的组件或者容器中没有的组件,例如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. 解密和解压浏览器上加密的js文件

    F12 -> 进入Sources -> 找到任意一个加密的js文件,如图 点击最下方的 {} 即可解压

  2. [ Laravel 5.6 文档 ]laravel数据库操作分页(自定义分页实现和自定义分页样式)

    简介 在其他框架中,分页可能是件非常痛苦的事,Laravel 让这件事变得简单.易于上手.Laravel 的分页器与查询构建器和 Eloquent ORM 集成在一起,并开箱提供方便的.易于使用的.基 ...

  3. Atitit.跨平台预定义函数 魔术方法 魔术函数 钩子函数 api兼容性草案 v2 q216  java c# php js.docx

    Atitit.跨平台预定义函数 魔术方法 魔术函数 钩子函数 api兼容性草案 v2 q216  java c# php js.docx 1.1. 预定义函数 魔术方法 魔术函数是什么1 1.2. & ...

  4. Flask--请求进来后流程

    启动先执行manage.py 中的    app.run() class Flask(_PackageBoundObject): def run(self, host=None, port=None, ...

  5. ”非常危险“的Linux命令

    Linux命令是一种很有趣且有用的东西,但在你不知道会带来什么后果的时候,它又会显得非常危险.所以,在输入某些命令前,请多多检查再敲回车. rm –rf rm –rf是删除文件夹和里面附带内容的一种最 ...

  6. python的list求和与求积

    python中,无论是对的list求和还是求积,我都给出了两种方法. 1.对list求和 1.1 s=0 for i in range(10): s+=i 1.2 s=sum(range(10)) 2 ...

  7. [转]maven2中snapshot快照库和release发布库的区别和作用

    Post by 铁木箱子 in 技术杂谈 on 2010-08-03 17:17 [转载声明] 转载时必须标注:本文来源于铁木箱子的博客http://www.mzone.cc[原文地址] 原文永久地址 ...

  8. Markdown常用语法学习

    Markdown常用语法学习,这些就够用了. 演示地址: https://github.com/YalongYan/Markdown-- 特别提示:  标题'##'后面必须加一个空格,否则编译不对.# ...

  9. 承载(Host)通用语言执行时

    承载(Host)通用语言执行时(CLR) 还有一种使用COM 的方法是是把须要集成的 F# 代码与已有的 C/C++ 应用程序集成到一起.开成自己定义的承载通用语言执行时.通用语言执行时就是 C++ ...

  10. iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

    本文转载至 http://www.tuicool.com/articles/3ymMzub CSDN博客原文  http://blog.csdn.net/hmt20130412/article/det ...