tags:

views:

35

answers:

2

This is my app.config:

<appSettings>
    <add key="PreRootFolder" value="D:\" />
    <add key="RootFolder" value="webSite" />
    <add key="Folder" value="folder_a" />        
</appSettings>

Since I prefer to build the path in the application rather than have many keys for each part of the path... (hard to maintain) so I build the path this way:

string prePath = ConfigurationManager.AppSettings["PreRootFolder"];
string rootFolder = ConfigurationManager.AppSettings["RootFolder"];
string folder= ConfigurationManager.AppSettings["Folder"];

// global param (actually accessed by ((MainFormName)mainParent)).g_fullOriginalRoot         
string g_fullOriginalRoot = prePath + "\\" + rootFolder + "\\" + fodler; 

I do this in the application MDI parent form (so it never die)

I did this because I found out that I call those keys many times and now I can get the data from the parent.

I wonder, if my solution is correct?

Are app.config parameters loaded as globals?

+2  A: 

It's reasonable to use a globally accessible (static or singleton) object that memoises the settings in this case.

Many of the disadvantages of globals are less serious when communication is one-way (read-only settings or write-only logging being classic cases). Also, since the settings are inherently global, it matches what is being modelled.

I'd prefer to have this done in its own class than in the MDI form though. The form should model things that have to do with the form, other objects should model other concerns.

Jon Hanna
@Jon: I work with files and the keys I have are the part of the paths:key1= "C:\source", key2="D:\target" key3="relativePath\"...So having pretty much the same keys for all forms, that are very likely to be used feels like worth the risk of initiating(and potentially not using) and it also seems like redundant work to do it on every class. Does my logic make sense?
Asaf
Yep. Thouch "C:\source" has nothing to do with a window, so I'd put it in a non-form class. It can also be memoised, where obtaining a given property loads that information on first use.
Jon Hanna
+1  A: 

Another thing to consider is if it is valid for this value to change while the app is running. Do you want to allow the user to change the value and have the application pick up the new value, or is it sufficient for the application to load the value at startup and then assume the value never changes during the running of the application.

This consideration is, of course, more important for a long-running application than for short-sessioned applications.

Jason
If it can change while an app is running, without forcing the app to restart, then it's not a setting.
Jon Hanna