Part 3 - Dictionary configuration
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_aliases; public Dictionary
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
(); 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:
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
duck
broke
code
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.