public interface IEncoder
{
string Encode(string source);
}
public class NullEncoder : IEncoder
public string Encode(string source)
return source;
public class SillyEncoder : IEncoder
private char[] _mixedUp = "YACBDFEGIHJLKMONPRSQTUWVXZ".ToCharArray();
string upperSource = source.ToUpper();
char[] encoded = new char[source.Length];
for (int i = 0; i < encoded.Length; i++)
encoded[i] = MapCharacter(upperSource[i]);
return new string(encoded);
private char MapCharacter(char ch)
if ((ch >= 'A') && (ch <= 'Z'))
return _mixedUp[ch - 'A'];
return ch;
public class SecretMessageSender
private readonly IEncoder _encoder;
private readonly string _from;
public SecretMessageSender(string from, IEncoder encoder)
_from = from;
_encoder = encoder;
public void SendMessage(string to, string body)
Console.WriteLine("to: {0}\r\nfrom: {1}\r\n\r\n{2}", to, _from, _encoder.Encode(body));
static void Main(string[] args)
WindsorContainer container = new WindsorContainer(new XmlInterpreter());
SecretMessageSender sender = container.Resolve<SecretMessageSender>();
sender.SendMessage("hammet", "castle is great!");
Console.Read();
<configuration>
<configSections>
<section name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>
<castle>
<components>
<component id="encoder.silly"
service="IoC.Tutorials.Part9.IEncoder, IoC.Tutorials.Part9"
type="IoC.Tutorials.Part9.SillyEncoder, IoC.Tutorials.Part9" />
<component id="encoder.null"
type="IoC.Tutorials.Part9.NullEncoder, IoC.Tutorials.Part9" />
<component id="messageSender"
type="IoC.Tutorials.Part9.SecretMessageSender, IoC.Tutorials.Part9">
<parameters>SecretMessageSender
<from>alex@bittercoder.com</from>
</parameters>
</component>
</components>
</castle>
</configuration>
<encoder>${encoder.null}</encoder>