Create the following Supporting classes:
public class HolidayElement : ConfigurationElement
{
[ConfigurationProperty("Date", IsKey = true, IsRequired = true)]
public DateTime Date
{
get { return (DateTime)this["Date"]; }
}
}
public class HolidayElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new HolidayElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((HolidayElement)element).Date;
}
}
public class HolidayConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("Holidays")]
public HolidayElementCollection Dates
{
get { return (HolidayElementCollection)this["Holidays"]; }
}
}
You now can create a method to test for the date:
public static bool IsHoliday(DateTime dt)
{
HolidayConfigurationSection section =
(HolidayConfigurationSection)System.Configuration.ConfigurationManager.GetSection("HolidayConfigurationSection");
foreach (HolidayElement holiday in section.Dates)
{
if (DateTime.Compare(dt, holiday.Date) == 0)
return true;
}
return false;
}
Add the following to the config file (app.confg):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="HolidayConfigurationSection" type="TheApplication.Utility.Classes.HolidayConfigurationSection, TheApplication.Utility.Library" requirePermission="false" />
</configSections>
<HolidayConfigurationSection>
<Holidays>
<add Date="1/1/2008"/>
<add Date="5/26/2008"/>
<add Date="7/4/2008"/>
<add Date="9/1/2008"/>
<add Date="11/27/2008"/>
<add Date="11/28/2008"/>
<add Date="12/25/2008"/>
<add Date="12/26/2008"/>
</Holidays>
</HolidayConfigurationSection>
</configuration>
That's it. It makes it easy to change the date via the config file.
Enjoy...

No comments:
Post a Comment