public interface IMessageOfTheDay
{
string GetMessageOfTheDay();
}
public class StaticMessageOfTheDay : IMessageOfTheDay
private string _message;
public string Message
set { _message = value; }
public string GetMessageOfTheDay()
return _message;
public class WikiQuotesMessageOfTheDay : IMessageOfTheDay
WebClient client = new WebClient();
string content = client.DownloadString("http://en.wikiquote.org/wiki/Main_Page");
string toFind = "<div style=\"background: #fff5f5\">";
int start = content.IndexOf(toFind) + toFind.Length;
int length = content.IndexOf("<a", start) - start;
return content.Substring(start, length);
private static void Main(string[] args)
WindsorContainer container = new WindsorContainer(new XmlInterpreter());
IMessageOfTheDay motd = container.Resolve<IMessageOfTheDay>();
Console.WriteLine("MOTD: {0}", motd.GetMessageOfTheDay());
Console.Read();
And so how do we swap between implementations, well we need to introduce an additional attribute to our components configuration called "service" where we specifiy what service our component implements... in this case it will be "IMessageOfTheDay" - so heres the configuration when using the Static MOTD class:
<configuration>
<configSections>
<section name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>
<castle>
<components>
<component id="motd.service"
service="IoC.Tutorials.Part7.IMessageOfTheDay, IoC.Tutorials.Part7"
type="IoC.Tutorials.Part7.StaticMessageOfTheDay, IoC.Tutorials.Part7">
<parameters>
<message>Welcome to my container tutorials</message>
</parameters>
</component>
</components>
</castle>
</configuration>
type="IoC.Tutorials.Part7.WikiQuotesMessageOfTheDay, IoC.Tutorials.Part7" />