Tuesday, April 17, 2007
2_more_config_arrays.png
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.
posted @ Tuesday, April 17, 2007 8:40:16 AM (New Zealand Standard Time, UTC+12:00)    Comments [0] | Trackback | Tracked by:
"Windsor Container Tutorials - BitterCoder's Wiki" (Insane World) [Trackback]
"Windsor Container Tutorials - BitterCoder's Wiki" (Insane World) [Trackback]
"Windsor Container Tutorials - BitterCoder's Wiki" (Insane World) [Trackback]
Search
FeedCount

Tags...
Who am I?
Alex Henderson
Alex Henderson
Auckland, New Zealand
Managing Director at Dev|Defined Limited

"Self Confessed Coding Junky for 15 years"
View Alex Henderson's profile on LinkedIn
 
Mobile: +64-21-402-969
Email: bittercoder 'at' gmail 'dot' com
MSN: bittercoder_nz@hotmail
Skype: alex.devdefined
Navigation