I’ve started with reviewing the Locale and Setting modules. The code is very clear to me, and I’d like to scribble points I’ve made to myself here.
Settings is a module which rides the registry for saving/fetching settings. This is useful, since you could drop any kind of object and get any kind of object (not just strings) from the registry.
So they did. You can get/set booleans:
public bool GetBoolean(string key, bool defaultValue)
{
return bool.Parse(GetString(key, defaultValue.ToString()));
}
While GetString relies on GetObject (this is the default value version, more on that later):
public object GetObject(string key, object defaultValue)
{
try
{
using (RegistryKey pdnKey = CreateSettingsKey(false))
{
return pdnKey.GetValue(key, defaultValue);
}
}
catch (Exception)
{
return defaultValue;
}
}
Which is the heart of the thing.
Elegance
The do their best to provide a clean and efficient interface:
Settings.CurrentUser.[SetString/GetString/SetObject/GetObject...]
Settings.SystemWide.[SetString/GetString/SetObject/GetObject...]
Which is done simply by instantiating two singletons to the respective HKCU/HKLM keys (CurrentUser, SystemWide are both Settings objects).
Another point to make is the use of “default value”. For some reason I find this a Good Thing. A get method takes a default value, an returns it instead of the null-value it should usually return when encountering error/absence of data.
All in all, this is a module that cries for reuse. And I will definitely put it in may favorite sources box, after I make sure it is generic enough.