使用Windsor注入应用程序设置

编程入门 行业动态 更新时间:2024-10-19 12:44:14
本文介绍了使用Windsor注入应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使用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>

但是,我真正想做的是取#{importantIntegerProperty} 从应用程序设置变量中定义,如下所示:

< appSettings> < add key = importantInteger value = 666 /> < / appSettings>

编辑:进行澄清;我意识到,使用温莎和 David Hayden,这本来就不可能 sliderhouserules 所指的文章实际上是关于他自己的(戴维海顿的)IoC容器,而不是温莎。 p>

我肯定不是第一个遇到这个问题的人,所以我想知道别人是如何解决这个问题的?

解决方案

我最终基于网络上各种来源的提示提出了一个解决方案。尽管最终结果涉及从温莎逐字复制几乎三个类并对其进行一些修改。最终结果取决于Codeplex,您可以享受。

http ://windsorappcfgprops.codeplex/

我最初是在一段时间之前编写此代码的,所以它基于Windsor 1.0.3-是的,它花了我那很久才发布结果!

该代码允许您将其包含在app.config(或web.config)中,显然):

<?xml version = 1.0 encoding = utf-8吗? < configuration> < appSettings> < add key = theAnswer value = 42 /> < / appSettings> < / configuration>

...并从您的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>

解决方案中有一个有效的示例。

How can I inject the value of an appSettings entry (from app.config or web.config) into a service using the Windsor container? If I wanted to inject the value of a Windsor property into a service, I would do something like this:

<properties> <importantIntegerProperty>666</importantIntegerProperty> </properties> <component id="myComponent" service="MyApp.IService, MyApp" type="MyApp.Service, MyApp" > <parameters> <importantInteger>#{importantIntegerProperty}</importantInteger> </parameters> </component>

However, what I'd really like to do is take the value represented by #{importantIntegerProperty} from an app settings variable which might be defined like this:

<appSettings> <add key="importantInteger" value="666"/> </appSettings>

EDIT: To clarify; I realise that this is not natively possible with Windsor and the David Hayden article that sliderhouserules refers to is actually about his own (David Hayden's) IoC container, not Windsor.

I'm surely not the first person to have this problem so what I'd like to know is how have other people solved this issue?

解决方案

I came up with a solution for this eventually based on hints from various sources on the web. The end result though involved pretty much copying three classes from Windsor verbatim and modifying them just a little bit. The end result is up on codeplex for your enjoyment.

windsorappcfgprops.codeplex/

I originally wrote this code quite some time ago so it's based on Windsor 1.0.3 - yes, it took me that long to get around to publishing the result!

The code allows you to have this in your app.config (or web.config, obviously):

<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="theAnswer" value="42"/> </appSettings> </configuration>

...and access it from your Windsor XML config file like this:

<?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>

There's a working example in the solution.

更多推荐

使用Windsor注入应用程序设置

本文发布于:2023-11-07 23:02:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567679.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   Windsor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!