views:

767

answers:

3

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?

A: 

See if this helps at all:

http://davidhayden.com/blog/dave/archive/2007/10/04/DependencyInjectionParametersPropertiesCastleWindsor.aspx

sliderhouserules
Thanks, sliderhouserules; but that doesn't quite answer my question. David Hayden is referring to his home-grown IoC container in that article, not Windsor. However, that is *exactly* the kind of thing I'd like to see!
Damian Powell
Ah, sorry about that. Didn't read his article closely enough. Should have double-checked before I answered here.
sliderhouserules
+1  A: 

I wrote a post about a similar case a couple of months ago. It uses a SubDependencyResolver to inject the appropriate parameters. In your case, you can just change DynamicConfigurationSettings for ConfigurationManager.

Mauricio Scheffer
Nice one. Thanks, Mausch. That's the pointer in the right direction that I was looking for.
Damian Powell
+4  A: 

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.

http://windsorappcfgprops.codeplex.com/

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.

Damian Powell
There's another approach based on DictionaryAdapter component, which is provided OOTB in Windsor 2.5 or newer http://codebetter.com/blogs/benhall/archive/2010/07/22/improving-testability-with-the-castle-dictionary-adapter.aspx
Krzysztof Koźmic
Thanks for the link, Krzysztof. I prefer that approach too.
Damian Powell

related questions