应用程序中的很多组件都会依赖其他的服务组件,很多依赖一些不合法的组件或者容器中没有的组件,例如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)
);

最新文章

  1. 重写AgileEAS.NET SOA 中间件平台账号密码的加密算法
  2. objective-c中的@selector()和 c /c++的函数指针
  3. 另类的SQL注入方法
  4. Command Pattern 命令模式
  5. 利用iframe实现无刷新上传处理
  6. ThinkPHP 3.2.3 关联模型的使用
  7. ZOJ2532_Internship
  8. php 上传图片
  9. Nodejs - windows的系统变量(环境变量)
  10. Java Servlet-http协议
  11. 2014.7.7 模拟赛【小K的农场】
  12. C# 将XML转换成DataSet【转】
  13. PowerDesigner反projectM连接ySql没有mySql odbc驱动器
  14. PAT (Advanced Level) 1056. Mice and Rice (25)
  15. #MainTest
  16. 拨开字符编码的迷雾--MySQL数据库字符编码
  17. python 调用 R,使用rpy2
  18. ubuntu安装输入法
  19. 记一次Mysql魔鬼实训
  20. sed命令讲解

热门文章

  1. stage3D基础五-----Working with 3D cameras(转)
  2. Unicode utf8等编码类型的原理
  3. poj 2367
  4. Vue.js 2 入门与提高(一)
  5. 蒙特卡洛方法计算圆周率的三种实现-MPI openmp pthread
  6. spring AOP pointcut expression表达式解析
  7. getMeasuredHeight(),getScrollY(),getHeight()的区别和联系
  8. Laravel手记:执行route:cache时报LogicException
  9. redis 集群 搭建
  10. 【BZOJ3672】[Noi2014]购票 树分治+斜率优化