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