博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
castle windsor学习-----Inline dependencies 依赖
阅读量:4623 次
发布时间:2019-06-09

本文共 1821 字,大约阅读时间需要 6 分钟。

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

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

1. 支持静态依赖 Dependency.OnValue

var twitterApiKey = @"the key goes here";container.Register(    Component.For
().ImplementedBy
() .DependsOn(Dependency.OnValue("APIKey", twitterApiKey)));

 

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

2.通过类型依赖

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

 

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

container.Register(    Component.For
().ImplementedBy
() .DependsOn(Property.ForKey("Name").Eq("Caption Hook"), Property.ForKey("Age").Eq(45)));

 

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

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

 

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

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

 

6. 

container.Register(    Component.For
() .DependsOn(Dependency.OnResource
("DisplayName", "MainWindowTitle")));

 

Embedded resource dependencies: Dependency.OnResource()

 

7. Supplying dynamic dependencies

 

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

 

转载于:https://www.cnblogs.com/lanpingwang/p/6537138.html

你可能感兴趣的文章
C语言中的地址传递(传指针,传递给形参的指针仍然是实参指针的一份拷贝)
查看>>
redis缓存数据库及Python操作redis
查看>>
opencms忘记Admin用户登录密码解决方案
查看>>
forms组件
查看>>
create-react-app 配置sass
查看>>
02_关系数据库
查看>>
在win7电脑中如何查看运行进程的PID标识符
查看>>
[Vue] vue-cli3.0安装
查看>>
C++学习之字符串
查看>>
图像化列表
查看>>
2014年10月9日——语言基础2
查看>>
How to Create Modifiers Using the API QP_MODIFIERS_PUB.PROCESS_MODIFIERS
查看>>
待飞笔记(第一天 )
查看>>
解惑好文:移动端H5页面高清多屏适配方案
查看>>
traefik添加多证书
查看>>
忽略UserInterfaceState.xcuserstate
查看>>
ReactNative--Flexbox布局
查看>>
java实现读取文件大全
查看>>
[Cordova] 无法显示Alert视窗
查看>>
借助过度区选择阈值
查看>>