
So welcome to part 2, today we're going to look at some more config - this time it's arrays...
So we have a class, that tells us when we can down tools and goto the beach...
public class HolidayService
{
private DateTime[] _holidays;
public DateTime[] Holidays
{
get
{
return _holidays;
}
set { _holidays = value; }
}
public bool IsHoliday(DateTime date)
{
if (_holidays != null)
{
DateTime matchDate = date.Date;
foreach (DateTime test in Holidays)
{
if (test.Date.Equals(matchDate))
{
return true;
}
}
}
return false;
}
}
We can set the days we are allowed off by adding dates to the holidays array...
So lets look at our code for running this service:
internal class Program
{
private static void Main(string[] args)
{
WindsorContainer container = new WindsorContainer(new XmlInterpreter());
HolidayService holidayService = container.Resolve<HolidayService>();
DateTime xmas = new DateTime(2007, 12, 25);
DateTime newYears = new DateTime(2008, 1, 1);
if (holidayService.IsHoliday(xmas))
Console.WriteLine("merry xmas!");
else
Console.WriteLine("xmas is only for management!");
if (holidayService.IsHoliday(newYears))
Console.WriteLine("happy new year");
else
Console.WriteLine("new year, you haven't done all the work for last year!");
Console.Read();
}
}
The service configuration is just like last time, we have the component registered with no parameters... so if we run the program we shall see on the screen:
xmas is only for management!
new year, you haven't done all the work for last year!
Mum will be dissapointed, she'd done a roast 'n all... so lets get some configuration in their so we can have a few days off...
<configuration>
<configSections>
<section name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>
<castle>
<components>
<component id="holidays.service" type="IoC.Tutorials.Part2.HolidayService, IoC.Tutorials.Part2" >
<parameters>
<Holidays>
<array>
<item>2007-12-24</item>
<item>2007-12-25</item>
<item>2008-1-1</item>
</array>
</Holidays>
</parameters>
</component>
</components>
</castle>
</configuration>
Now let's see if we're going to have a merry xmas and a happy new year - a quick run of our application....
merry xmas!
happy new year
You don't just have to use arrays either, you can do lists too - though I didn't show them here, just use
list instead of
array in the xml configuration...
See you for part 3 where we do dictionary configurations.