Application Settings

The .net framework provides a powerful way to provide application settings in app.config file. There is also the ability to use command line parameters but they are accessed in completely different fashion. I created couple of classes to simplify parameters handling and provide a declarative syntax.

public class MyAppsSettings : Settings
{
    [CommandLineParam("/u")]
    public string UserName { get; set; }

    [CommandLineParam("/p")]
    public string Password { get; set; }

    [ConnectionString("MyDB")]
    public string MyDBConnStr { get; set; }

    [AppSetting("Mode")]
    public string Mode { get; set; }

    [AppSetting("DefaultMode", "Test")]
    public string DefaultMode { get; set; }
}

Here is the complete source code:

[AttributeUsage(AttributeTargets.Property)]
public class CommandLineParamAttribute : Attribute
{
    public string Name { get; set; }

    public string DefaultValue { get; set; }

    public CommandLineParamAttribute(string Name)
    {
        this.Name = Name;
        this.DefaultValue = string.Empty;
    }

    public CommandLineParamAttribute(string Name, string DefaultValue)
    {
        this.Name = Name;
        this.DefaultValue = DefaultValue;
    }
}

[AttributeUsage(AttributeTargets.Property)]
public class ConnectionStringAttribute : Attribute
{
    public string Name { get; set; }

    public ConnectionStringAttribute(string Name)
    {
        this.Name = Name;
    }
}

[AttributeUsage(AttributeTargets.Property)]
public class AppSettingAttribute : Attribute
{
    public string Key { get; set; }

    public string DefaultValue { get; set; }

    public AppSettingAttribute(string Key)
    {
        this.Key = Key;
        this.DefaultValue = string.Empty;
    }

    public AppSettingAttribute(string Key, string DefaultValue)
    {
        this.Key = Key;
        this.DefaultValue = DefaultValue;
    }
}

public class Settings
{
    public Settings()
    {
        CommandLineParams clp = new CommandLineParams();

        Type type = this.GetType();

        foreach (PropertyInfo pi in type.GetProperties())
        {
            object[] arr = pi.GetCustomAttributes(true);
            foreach (object o in arr)
            {
                if (o is CommandLineParamAttribute)
                {
                    string name = ((CommandLineParamAttribute)o).Name;
                    string value = ((CommandLineParamAttribute)o).DefaultValue;

                    if (clp.Contains(name)) value = clp[name];

                    pi.SetValue(this, value, null);
                }

                if (o is ConnectionStringAttribute)
                {
                    string name = ((ConnectionStringAttribute)o).Name;
                    string conn = ConfigurationManager.ConnectionStrings[name].ConnectionString;
                    pi.SetValue(this, conn, null);
                }

                if (o is AppSettingAttribute)
                {
                    string key = ((AppSettingAttribute)o).Key;
                    string value = ((AppSettingAttribute)o).DefaultValue;

                    if (ConfigurationManager.AppSettings.AllKeys.Contains(key))
                        value = ConfigurationManager.AppSettings[key];

                    pi.SetValue(this, value, null);
                }
            }
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s