Creating a generic settings repository in C#

by Mark Wiseman on June 25th, 2012 | Posted in Revium Sandbox | Read the comments

Every project has a group of settings that can be used to configure a web site. Sometimes we store these in the web.config file and sometimes we store them in a database and typically what happens is you create a number of functions to read settings like:

public static string GetSetting_AsString(string settingKey) { /*...*/ }
public static string GetSetting_AsInt(string settingKey) { /*...*/ }
public static string GetSetting_AsBool(string settingKey) { /*...*/ }

This is boring and I finally got sick of copying and pasting all of these functions and decided to start living the Generics dream! With one simple function I have been able to replace all other generic functions with one beautiful one.

And here it is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public static class SettingRepository
{
    public static T GetSetting<T>(string settingKey, T defaultValue )
    {
        if (string.IsNullOrEmpty(settingKey))
            return defaultValue;

        ProjectEntities dc = new ProjectEntities();
        Setting setting = dc.Settings.Where(s => s.SettingKey == settingKey).FirstOrDefault();

        if (setting == null)
            return defaultValue;

        return (T)Convert.ChangeType(setting.SettingValue, typeof(T));
    }
}

This function gets a setting from a table named Setting in our database.
To retrieve strongly type settings is really easy.

public static void SettingRepositoryTest()
{
    string stringSetting = SettingRepository.GetSetting("stringKey", "Default Value");
    int intSetting = SettingRepository.GetSetting("intKey", 99);
    bool boolSetting = SettingRepository.GetSetting("boolKey", true);
    DateTime dateTimeSetting = SettingRepository.GetSetting("dateTimeKey", DateTime.Now);
}

Related posts:

  1. Kentico, Smart Search and filtering attachments
  2. CSV parsing – the easy way!
  3. FluentSecurity + MvcSiteMapProvider = Better .Net Security Management
  4. Limiting LINQ String Field Lengths
  5. Asp.Net MVC convert View to Word Document

Tags: .net, c#, generics

« FluentSecurity + MvcSiteMapProvider = Better .Net Security Management

Kentico 6.0 Custom BizForm Validation »

2 Responses to “Creating a generic settings repository in C#”

  1. faris says:
    September 25, 2012 at 6:50 am

    Hello ,

    Nice article.
    I have my setting in a table.
    Could you please send the the complete code for ProjectEntities for all the project ?

    Regards,

  2. Mark Wiseman says:
    September 25, 2012 at 9:20 am

    @faris The Settings table I have uses the following schema

    SettingId int IDENTITY(1,1) NOT NULL
    SettingKey varchar(25) NOT NULL
    DisplayName varchar(50) NULL
    SettingValue varchar(50) NOT NULL

Leave a Reply

Click here to cancel reply.

Recent Articles

  • ISAF Sailing World Cup
  • Revium Supports the Prostate Cancer Foundation of Australia
  • Kentico FAQ Module
  • Advanced Visitor Tracking in Analytics
  • Kentico, Smart Search and filtering attachments
  • Enhancing JIRA’s Issue Navigator
  • Mobile Browsing
  • Revium help win gold for Australia

Twitter

  • All things Gold! http://t.co/9DkkjmAr 2012-09-13
  • Mat Belcher - our favourite London Gold Medalist dropped into the office to say thank you. http://t.co/TxHbe2y6 2012-09-13
  • You beauty - http://t.co/1kbcBZwg #london2012 @belcherpage2012 2012-08-10
  • More updates...

Revium Logo

  • Home
  • About
  • Expertise
  • Showcase
  • Contact

  • news
  • blog
  • sandbox
  • twitter
  • rss
  • visit our facebook page

We are Revium, hear us roar!

The news.

26 Feb

Revium Supports the Prostate Cancer Foundation of Australia

We here at Revium are proud to say that we are supporting the Prostate Cancer Foundation of Australia, this month we have been able…

Continue reading
View archive

The blog.

11 Apr

ISAF Sailing World Cup

Mat Belcher and his team have been successful in taking out round 3 of the ISAF Sailing World Cup in Palma de Mallorca a…

Continue reading
View archive

The sandbox.

20 Dec

Kentico, Smart Search and filtering attachments

We had a scenario recently where we had 2 indexes set up in Kentico to search different folders within our site. Everything worked as…

Continue reading
View archive

eNewsletter.

 

© Copyright 2013. All Rights Reserved.

Revium Pty Ltd

info@revium.com.au Work +61 3 9429 2000

10 Harvey Street
Richmond, Victoria, 3121 Australia
View map

Disclaimer and privacy Revium Pty Ltd

Find us: web development, seo

 
Partner logos