castle windsor学习-----Inline dependencies 依赖
2024-08-24 22:31:22
应用程序中的很多组件都会依赖其他的服务组件,很多依赖一些不合法的组件或者容器中没有的组件,例如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)
);
最新文章
- 重写AgileEAS.NET SOA 中间件平台账号密码的加密算法
- objective-c中的@selector()和 c /c++的函数指针
- 另类的SQL注入方法
- Command Pattern 命令模式
- 利用iframe实现无刷新上传处理
- ThinkPHP 3.2.3 关联模型的使用
- ZOJ2532_Internship
- php 上传图片
- Nodejs - windows的系统变量(环境变量)
- Java Servlet-http协议
- 2014.7.7 模拟赛【小K的农场】
- C# 将XML转换成DataSet【转】
- PowerDesigner反projectM连接ySql没有mySql odbc驱动器
- PAT (Advanced Level) 1056. Mice and Rice (25)
- #MainTest
- 拨开字符编码的迷雾--MySQL数据库字符编码
- python 调用 R,使用rpy2
- ubuntu安装输入法
- 记一次Mysql魔鬼实训
- sed命令讲解
热门文章
- stage3D基础五-----Working with 3D cameras(转)
- Unicode utf8等编码类型的原理
- poj 2367
- Vue.js 2 入门与提高(一)
- 蒙特卡洛方法计算圆周率的三种实现-MPI openmp pthread
- spring AOP pointcut expression表达式解析
- getMeasuredHeight(),getScrollY(),getHeight()的区别和联系
- Laravel手记:执行route:cache时报LogicException
- redis 集群 搭建
- 【BZOJ3672】[Noi2014]购票 树分治+斜率优化