Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Let's say I have a configuration file with a property in it:

<add key="LoadedCode" value="L" />

I know I can reference this using the ConfigurationManager:

System.Configuration.ConfigurationManager.AppSettings["LoadedCode"];

This has some drawbacks. It can get bulky when it gets used in multiple locations. I lose support for refactoring and "Find All References" because I'm using an item in the AppSettings collection. I don't have strong-typing in cases where the config setting isn't a string. So I wrap it into a static property in a static class:

public static string LoadedCode
{
    get { return System.Configuration.ConfigurationManager.AppSettings["LoadedCode"]; }
}

Great so far. Now I want to use this on multiple pages and this leads me to my conundrum. Should I reference this static property everywhere, or should each page have its own local wrapper?

On the one hand, a local wrapper pushes all uses on the page through a single point which can be modified without impacting other pages if needed. On the other hand, it adds a layer of indirection to the dependency chain and it looks a bit like overkill.

Is there a best practice for this type of behavior, or am I simply over-thinking things?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

I don't do much , but if this StackOverflow answer applies then you could just do this:

using Settings = MyProject.Namespace.Properties.Settings;

var loadedCode = Settings.Default.LoadedCode;

Like this other StackOverflow answer is saying:

We prefer to use Properties.Settings (aka. settings.settings) because it's strongly typed.

Now I know for a fact that this works with an app.config, but it might not work with a web.config file... but I think it's worth trying.


Alternatively, this Haacked article describes how to use the configuration API to define your own ConfigurationSection classes, which would be the ultimate way to address this issue. Might be overkill if the above works though.

share|improve this answer
    
it does work the same for a web.config file, it's the same thing as a app.config file except it's for a web application. –  Malachi May 21 at 17:54
3  
This might be of interest: haacked.com/archive/2007/03/12/… –  Mat's Mug May 21 at 17:56
    
I think you are right, I don't think I fully understood the question. that link in your comment is more along the lines of this question I think –  Malachi May 21 at 18:00
1  
That haacked article answered my question in a way I didn't expect. I never thought about using the configuration API. That gives me the benefits of a property approach while also letting me shape the config in a more natural way. I'll mark this as the answer and I encourage you to move that up into your post's body. –  Rakuen42 May 23 at 13:00

You shouldn't have to use the fully qualified name, you should be able to do something like this

using System.Configuration;

public static string LoadedCode
{
    get { return ConfigurationManager.AppSettings["LoadedCode"]; }
}

I think you can even go farther depending on how you set up your References and Web.Config File

What I do is create my variable and set it to the value of the Configuration that I need, so this is where you would "convert" it to the right type. Doing it this way you also only retrieve the data if you need it.

String loadedCode = Convert.ToString(ConfigurationManager.AppSettings["LoadedCode"]);

And then when you need to use that value you just call loadedCode

I don't see a reason to create a method just to grab this unless it is going to be used by the entire class and you don't want it available outside the class, then you would have something like this

private static string _loadedCode = ConfigurationManager.AppSettings["LoadedCode"];

And then you would call it accordingly as _loadedCode

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.