The Bitter Coder Tutorials, Binsor Style, Arrays

Sat, Jul 12, 2008 2-minute read
In the last post, I started doing the Bitter Coder tutorials, but using Binsor for the configuration.  This is the second in the series, and focuses on configuring arrays...the original is here. So, our HolidayService tells us when we can go to the beach, yes?

namespace BitterCoder.Tutorials.Binsor.Core

{

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;

}

}

}

And the code to run this service:

static void Main(string[] args)

{

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();

}

And the config:

component "holiday.service", HolidayService:

Holidays=(

DateTime(2007,12,24),

DateTime(2007,12,25),

DateTime(2008,1,1)

)

The array syntax in Binsor is the objects surrounding by parentheses.  If you has a list (or a List<DateTime>) you would have to use brackets ([ ]).  Also, we can new-up our dates here, minus the "new" keyword.  One more thing I'd like to mention, is I am constantly getting bit by not putting the colon (:) at the end of the component line, which you ONLY do if you are adding parameters. BTW, if you take away the Holiday parameter (and the trailing : on the component line) you'll get the default behavior, which shows up in the console as: xmas is only for management! new year, you haven't done all the work for last year! With the Holiday parameter (and the trailing : ) in the windsor.boo file, you get: merry xmas! happy new year And we're done with tutorial #2. Next, we take a look at dictionaries...