castle windsor学习-----Inline dependencies 依赖
应用程序中的很多组件都会依赖其他的服务组件,很多依赖一些不合法的组件或者容器中没有的组件,例如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 依赖的更多相关文章
- [Castle Windsor]学习依赖注入
初次尝试使用Castle Windsor实现依赖注入DI,或者叫做控制反转IOC. 参考: https://github.com/castleproject/Windsor/blob/master/d ...
- Castle Windsor 学习-----Installer的几种安装方式
翻译 当使用依赖注入容器时,你首先要向容器中注册你的组件,Windsor使用installers(该类型实现IWindsorInstaller接口)来封装和隔离注册的逻辑,可以使用Configurat ...
- castle windsor学习-----XML Inline Parameters 内联参数
当使用XML配置的时候,可能要给组件指定各种各样的依赖 1.简单的参数 参数名称不区分大小写 <component id="ping" type="Acme.Crm ...
- castle windsor学习----- Referencing types in XML 在xm文件中引用类型
当从xml引用installer的语法如下 <install type="Acme.Crm.Infrastructure.ServicesInstaller, Acme.Crm.Inf ...
- castle windsor学习-----Registering components by conventions
注册多个组件 1.one-by-one注册组件可能是一项非常重复的工作,可以通过Classes或Types注册一组组件(你可以指定一些特定的特征) 三个步骤 注册多个类型通常采取以下结构 contai ...
- castle windsor学习-----Registering components one-by-one 注册类型
1.在容器中注册一个类型 container.Register( Component.For<IMyService>() .ImplementedBy<MyServiceImpl&g ...
- castle windsor学习-----Fluent Registration API 注册
使用xml配置和fluent注册两种搭配使用需要注意的是: 如果先在WindsorContainer构造函数指明用xml配置进行注册,如下设置 IWindsorContainer container ...
- castle windsor学习-------Container Events 容器的事件
所有的事件是实现IKernelEvents 接口,已容器的Kernel属性暴露出来 1. AddedAsChildKernel 当前的容器添加子容器或其他容器时触发 2. RemovedAsChild ...
- castle windsor学习-----How components are created
随机推荐
- windows和linux下目录分隔符兼容问题(换行回车兼容)
windows和linux下目录分隔符兼容 DIRECTORY_SEPARATOR 换行回车兼容 PHP_EOF
- linux下OpenSSL的RSA密钥生成
工具的安装: 一.源码安装 OpenSSL Version:openssl-1.0.0e.tar.gz ------------------------安装: 1.将下载的压缩包放在根目录, 2.在文 ...
- ArrayList remove注意事项
例子1: List<Integer>list=new ArrayList<>(); list.add(1); list.add(2); list.add(2); list.ad ...
- oracle中位图索引和B-tree索引的区别
1.适用系统的不同:位图索引适合OLAP系统,而B-tree索引适合OLTP系统. 2.占用存储空间不同:位图索引只需要很小的存储空间,而B-tree索引需要占用很大的存储空间. 3.创建需要的时间不 ...
- codeforces 427 div.2 F. Roads in the Kingdom
F. Roads in the Kingdom time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 利用asset存储mesh
做mesh导出的时候遇到了这个问题. 最后解决: 存储mesh数据:AssetDatabase.CreateAsset(meshfilter.mesh, "Assets/" + & ...
- 自定义验证----required属性
1,required属性 - 表示字段不能为空(注意:只有用户单击“提交”按钮提交表单的时候,浏览器才会执行验证.目前HTML5不支持指定验证的时间,而且验证消息的样式和内容各个浏览器不大一样,不能修 ...
- Windows下安装appium桌面版和命令行版
安装appium桌面版和命令行版 一 桌面版(打开很慢,常用于辅助元素定位) 1.官网下载window版本: github search appium desktop download late ...
- jQuery.callbacks 注释
(function( jQuery ) { // String to Object flags format cache var flagsCache = {}; // Convert String- ...
- debian安装oracle jdk
1 去官网下载linux jdk https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...