mirror of
https://github.com/wagesj45/CapyKit.git
synced 2024-10-18 07:01:50 -05:00
Jordan Wages
87bd044b31
The `SettingsHelper` is an agnostic way to access settings values uniformly through code. The accessor methods are controlled by the consumer. Documentation has been a bit wonky. I changed the max number of version number components allowed, so each new build should not create new changes on every single bit of documentation now.
3.5 KiB
3.5 KiB
SettingsHelper Class
Static class containing helper methods for retrieving and setting application settings.
Definition
Namespace: CapyKit.Helpers
Assembly: CapyKit (in CapyKit.dll) Version: 1.0.0
C#
public static class SettingsHelper
F#
[<AbstractClassAttribute>]
[<SealedAttribute>]
type SettingsHelper = class end
Inheritance | Object → SettingsHelper |
Remarks
The specific means of accessing and storing the settings are determined by the consumer, allowing for flexibility in various environments such as App.config
or Web.config
.
Example
This example demonstrates how to set up the SettingsHelper class with custom accessor and detector methods that read from an App.config file. The setup is done at the beginning of the application execution, before any other usage of the helper methods.
C#
public int main(string[] args)
{
// Set up SettingsHelper with custom accessor and detector methods
Func<string, object> accessor = (key) =>
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
return config.AppSettings.Settings[key].Value;
};
Func<string, bool> detector = (key) =>
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
return config.AppSettings.Settings.AllKeys.Contains(key);
};
SettingsHelper.SetAccessorMethod(accessor);
SettingsHelper.SetDetectorMethod(detector);
// Use the helper to retrieve and set settings
SettingsHelper.SetApplicationSetting<int>("MySettingKey", 42);
int newSetting = SettingsHelper.GetApplicationSetting<int>("MySettingKey");
Console.WriteLine("New setting: {0}", newSetting);
int mySetting = SettingsHelper.GetApplicationSetting<int>("MySettingKey");
Console.WriteLine("Retrieved setting: {0}", mySetting);
}
Constructors
SettingsHelper |
Methods
GetApplicationSetting(T) | Retrieves a setting with the given key . Returns the setting as an uncast T. |
SetAccessorMethod | Sets the function used to retrieve application settings. |
SetDetectorMethod | Sets the function used to detect if an application setting with a given key exists. |
Fields
accessor | Private delegate function that retrieves a setting with the given key . Returns the setting as an uncast Object. |
detector | Private delegate function that detects if a setting with a given key exists. Returns true if the setting exists, false if not. |