?? Operator and its use case

Long time I asked myself, for what the heck I could use the ?? operator. Last days I found the perfect scenario for using this operator.

Think about accessing your application config file using the configuration manager. This could look like:

string value = ConfigurationManager.AppSettings["AppKey"];
if (value == null)
    value = "DefaultValue";

If you want to do it a little bit more elegant, you could use this code:

string value = ConfigurationManager.AppSettings["AppKey"];
value = (value != null) ? value : "DefaultValue";

Or you say:

string value = ConfigurationManager.AppSettings["AppKey"] ?? "DefaultValue";

I think, that’s Impressive! If you have some other use cases for this operator I would like to know them.

Cheers
- Gerhard

kick it on DotNetKicks.com

Public Holiday Toolkit v1.0

Today I’m proud to publish the Public Holiday Toolkit. To give you a short summary. Using this toolkit enables you to show public holidays for several countries within your .NET application. This can be interessting for many use cases. Think about calendars, appointments and so on.

What does the Toolkit offer?

The Public Holiday Toolkit comprises the holiday ruleset for Germany, New Zealand, Norway, Austria, Poland, Swiss, United Kingdom and the USA including all administrative districts. Furthermore it comprises an API for accessing the public holiday definitions, an Example Application and a Holiday Editor to create new rulesets for other countries.

How can I use it?

The holiday API is quite simple and contains only 4 methods.

/// <summary>
/// This class encapsulates all business methods to work with public holidays.
/// </summary>
public interface IHolidayService
{
    /// <summary>
    /// Loads a list with all countries and their public holiday definitions.
    /// </summary>
    List<Country> GetHolidayDefinitionsFromXml(string xmlFile);
    
    /// <summary>
    /// Saves a list with countries and their public holiday definitions.
    /// Existing files will be overwritten and replaced by the new holiday definition.
    /// </summary>
    void SetHolidayDefinitionsToXml(string xmlFile, List<Country> countries);
    
    /// <summary>
    /// Searches a specific country and district object for their public
    /// holidays within a given time frame. The result is not the holiday class
    /// itself, but a proxy object that contains the holiday definition.
    /// This is because holidays can be occure more than onces, if the
    /// timeframe covers more than one year.
    /// </summary>
    List<HolidayEntry> SearchHolidays(Country country, District district, DateTime startRange, DateTime endRange);
    
    /// <summary>
    /// Searches the weekends for a given time frame.
    /// </summary>
    List<HolidayEntry> SearchWeekends(DateTime startRange, DateTime endRange);
}

Do you have an example ?

Sure. An example application is provided within the download of the Public Holiday Toolkit. It’s only a small application, but it shows how to read the holiday definitions and show them within your application.

Public Holiday Toolkit Demo Application

What about creating public holiday definitions for other countries?

If you need to create or update public holiday definitions you can use the Holiday Editor within the Toolkit. The Holiday Editor enables you to modify the Holiday Definitions. I would be very glad if you could share new or updated holiday definitions with me and the public. Please send your created holiday definitions to me and I will publish them on this blog.

Here’s a screenshot of the Holiday Editor.

Holiday Editor

Where can I get it?

Simply download the Public Holiday Toolkit from the following location: http://www.ad-factum.de/wordpress/PHT_1.0.zip

What else do I have to know?

The Public Holiday Toolkit uses the AdFactum ObjectMapper .NET in order to store and read the public holiday definitions. License informations about the AdFactum ObjectMapper .NET can be found here.

What’s left?

For now? Nothing. Feel free to use the holiday definition file and service component within your .NET application. As I already said: I would be happy If you could share some new holiday definitions of other countries with me.

Cheers
- Gerhard

kick it on DotNetKicks.com