Handling application initialization parameters

Sometimes you need enable or disable some application options as the outcome / result of some initialization parameters. Typically this work is done at the application main entry point or in a notification such as start event receiving this arguments. But it’s pretty “dirt” do all this work at the application start. May that some application modules are actually unloaded and you “don’t have actually a way” to disable such option or the same argument have a different meaning cross modules.

The fact is that with Catel, you can delay the processing of such initialization parameters using the StartUpInfoProvider and make the right interpretation of any parameter at any application point.

Using the StartUpInfoProvider

Now Catel comes with StartUpInfoProvider. This class provides the access to the initialization parameters of the application. Basically if you are in building a NET application you can access to the command line arguments array or if you are building a Silverlight application you can access to the initialization parameter dictionary.

The interface of this service is defined below:

    /// <summary>
    /// The IStartUpInfoProvider interface.
    /// </summary>
    public interface IStartUpInfoProvider
    {
#if SILVERLIGHT
        /// <summary>
        /// Gets the silverlight application initialization parameters.
        /// </summary>
        IDictionary<string, string> InitParms { get; }
#endif
#if NET
        /// <summary>
        /// Gets the application command line argument.
        /// </summary>
        string[] Arguments { get; }
#endif
    }

We are evaluating the way to automatically map the command line array into a meaning full strong typed options and switches map or dictionary to direct access to command line options

Advantages of the StartUpInfoProvider

Think that you want to test or debug an application that require analyses the initialization parameters to work. Typically you have options to modify this argument in a configuration windows of the visual studio or in the test page of the Silverlight application.

By default, there no easy way to mock or fake the initialization parameters for an application. Think about it:

Process.GetCurrentProcess().StartInfo.Arguments 

or

Application.Current.Host.InitParams

Such thing are actually “unmockable”. But now, thanks to Catel, you can do it registering fakes or mock instances into the service locator. Just like this:

var startUpInfoProviderMock = new Moq<IStartUpInfoProvider>();


....

ServiceLocator.Default.RegisterInstance<IStartUpInfoProvider>(startUpInfoProviderMock.Object);