Tuesday, April 17, 2007
3_more_config_dictionaries.png

So in this part we look at creating a component which has a dictionary which gets configured... in this case we're creating a little service to handle word substitutions...

So lets have a look at the service:

public class AliasService

{

    private Dictionary<string, string> _aliases;

 

    public Dictionary<string, string> Aliases

    {

        get { return _aliases; }

        set { _aliases = value; }

    }

 

    public string Evaluate(string term)

    {

        if (_aliases == null) return term;

 

        while (_aliases.ContainsKey(term))

        {

            term = _aliases[term];

        }

 

        return term;

    }

}


And the application code that uses the service:

static void Main(string[] args)

{

    WindsorContainer container = new WindsorContainer(new XmlInterpreter());

 

    AliasService aliasService = container.Resolve<AliasService>();

 

    string sentence = "a dog ate my homework";

 

    foreach (string word in sentence.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))

    {

        Console.Write("{0} ", aliasService.Evaluate(word));

    }

 

    Console.Read();

}


Our component is again registered without any parameters, lets run it and see what pearls of wisdom it can share:

a dog at my homework

Hmm... I'm not sure that's a great excuse for late delivery... let's do some configuration and try again:

<configuration>

 

  <configSections>

    <section name="castle"

        type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />

  </configSections>

 

  <castle>

    <components>

      <component id="aliases.service" type="IoC.Tutorials.Part3.AliasService, IoC.Tutorials.Part3">

        <parameters>

          <Aliases>

            <dictionary>

              <entry key="dog">duck</entry>

              <entry key="ate">broke</entry>

              <entry key="homework">code</entry>

            </dictionary>

          </Aliases>

        </parameters>

      </component>

    </components>

  </castle>

 

</configuration>


So, you can see we map keys to values via an entry key... and here's the result:

a duck broke my code

You may need to tweak the mappings, depending on what you actually broke...

Now at this point you might be wondering just how the strings are getting converted into strongly typed values for the last 3 parts - and to be honest, you won't need to know most of time, just do what "seems" right and it'll probably work... but when it doesn't, check out the castle projects wiki topic on component parameters, configuration and type converters.
posted @ Tuesday, April 17, 2007 9:14:30 AM (New Zealand Standard Time, UTC+12:00)    Comments [2] | Trackback | Tracked by:
"Windsor Container Tutorials - BitterCoder's Wiki" (Insane World) [Trackback]
"Windsor Container Tutorials - BitterCoder's Wiki" (Insane World) [Trackback]
"Windsor Container Tutorials - BitterCoder's Wiki" (Insane World) [Trackback]
Monday, May 14, 2007 11:10:47 PM (New Zealand Standard Time, UTC+12:00)
Thanks for the Castle walk-throughs. These should be incorporated into the Castle wiki to make them more discoverable. :) Something to note for anyone else trying Part 3 out is that there is a bug in Castle 1.0 RC2, which causes creation of the AliasService to fail with an invalid key type for the generic Dictionary<string, string>. Switching to build 382 from the build server corrected the problem. So the problem was corrected sometime after RC2 release. Hopefully this saves someone else some head scratching...
Tuesday, May 15, 2007 2:36:27 AM (New Zealand Standard Time, UTC+12:00)
Hi James,

Yeah, these tutorials are built against the "bleeding edge" ... some of them should work with RC2, but some generics etc. scenarios may trip up the older release. I believe the stronghold guys are looking to put out another "official" release shortly (next couple of weeks) which will be the version I will eventually target for all the tutorials.

I will be looking to move them onto the using.castleproject.org Wiki once the whole set is complete, in the mean time I'm just keeping them on my Wiki to gauge some feedback from people, and also to capture a few additional blog subscribers at the same time :)
Comments are closed.
Search
FeedCount

Tags...
Who am I?
Alex Henderson
Alex Henderson
Auckland, New Zealand
Managing Director at Dev|Defined Limited

"Self Confessed Coding Junky for 15 years"
View Alex Henderson's profile on LinkedIn
 
Mobile: +64-21-402-969
Email: bittercoder 'at' gmail 'dot' com
MSN: bittercoder_nz@hotmail
Skype: alex.devdefined
Navigation