Dam*_*ell 13 castle-windsor appsettings
如何使用Windsor容器将appSettings条目(从app.config或web.config)的值注入服务?如果我想将一个Windsor属性的值注入服务中,我会做这样的事情:
<properties>
<importantIntegerProperty>666</importantIntegerProperty>
</properties>
<component
id="myComponent"
service="MyApp.IService, MyApp"
type="MyApp.Service, MyApp"
>
<parameters>
<importantInteger>#{importantIntegerProperty}</importantInteger>
</parameters>
</component>
Run Code Online (Sandbox Code Playgroud)
但是,我真正想做的是#{importantIntegerProperty}从应用程序设置变量中获取值,该变量可能是这样定义的:
<appSettings>
<add key="importantInteger" value="666"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
编辑:澄清; 我意识到,温莎和David Hayden的文章指出,滑盖式枪口指的是实际上是关于他自己的(David Hayden的)IoC容器,而不是温莎.
我肯定不是第一个遇到这个问题的人,所以我想知道的是其他人如何解决这个问题?
我最终根据网络上各种来源的提示想出了一个解决方案.最终结果虽然涉及从Windsor逐字复制三个类并稍微修改它们.最终的结果是编码复合,让您享受.
http://windsorappcfgprops.codeplex.com/
所以它是基于温莎1.0.3我最初写这个代码前一段时间-是的,我花了那久才向周围发布的结果!
代码允许您在app.config(或web.config,显然)中使用此代码:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="theAnswer" value="42"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
...并从您的Windsor XML配置文件中访问它,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<castle>
<components>
<component
id="answerProvider"
service="Acme.IAnswerProvider, Acme"
type="Acme.AnswerProvider, Acme"
>
<parameters>
<theAnswer>#{AppSetting.theAnswer}</theAnswer>
</parameters>
</component>
</components>
</castle>
Run Code Online (Sandbox Code Playgroud)
解决方案中有一个有效的例子.
| 归档时间: |
|
| 查看次数: |
3967 次 |
| 最近记录: |