7.4 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | ||||
|---|---|---|---|---|---|---|---|---|
|
2026-04-16T14:10:18.570863+00:00 | zai-org/GLM-5-FP8 | 1 | 0690ee9f2887691d |
Documentation: DTS.Common.Settings
1. Purpose
This module provides a persistence layer for application and user-specific configuration settings. It implements a caching singleton pattern (SettingsDB) to manage key-value pairs stored in a SQL database, supporting both global (system-wide) and user-specific contexts. The module abstracts database interaction details, offering typed accessors for primitives and arrays, and handles the creation of default values when settings do not exist in the database.
2. Public Interface
Class: DTS.Common.Settings.SettingsDB
A static facade and singleton manager for accessing all settings. This is the primary entry point for interacting with settings.
Methods:
static void RefreshSettings(): Clears the in-memory cache dictionary, forcing subsequent reads to fetch from the database.static string GetUserValue(string id, string defaultValue, string user): Retrieves a setting specific to a user. If the key does not exist in the cache or DB, it creates a newUserSettingusing the default value and persists it.static string GetGlobalValue(string id, string defaultValue, bool useCache = true): Retrieves a global setting. Accepts a flag to bypass the cache.static void GetAllGlobalValues(): Loads all global settings from the database into the local cache.static double GetGlobalValueDouble(string id, double defaultValue): Retrieves a global setting parsed as adouble.static int GetGlobalValueInt(string id, int defaultValue): Retrieves a global setting parsed as anint.static bool GetGlobalValueBool(string id, bool defaultValue, bool useCache = true): Retrieves a global setting parsed as abool.static double[] GetGlobalValueDoubleArray(string id, double[] defaultValues): Retrieves a global setting as an array of doubles.static int[] GetGlobalValueIntArray(string id, int[] defaultValues): Retrieves a global setting as an array of integers.static void SetUserValue(string id, string value, string user): Updates a user-specific setting in the cache and persists it to the database.static void SetGlobalValue(string id, string value): Updates a global setting in the cache and persists it to the database.static void SetGlobalValueDouble(string id, double value): Persists a global setting from adoubleinput.static void SetGlobalValueInt(string id, int value): Persists a global setting from anintinput.static void SetGlobalValueBoolean(string id, bool value): Persists a global setting from aboolinput.static void SetGlobalValueDoubleArray(string id, double[] values): Persists an array of doubles as a series of global settings.static void SetGlobalValueIntArray(string id, int[] values): Persists an array of integers as a series of global settings.
Class: DTS.Common.Settings.GlobalSetting
Represents a system-wide setting.
Methods:
GlobalSetting(string id, string defaultPropertyValue): Constructor that initializes the setting and loads/instantiates it in the DB.static GlobalSetting ReadXML(System.Xml.XmlElement root): Creates aGlobalSettinginstance from an XML element.static GlobalSetting[] GetAllGlobalSettings(): Fetches all global settings from the database.
Class: DTS.Common.Settings.UserSetting
Represents a user-specific setting.
Methods:
UserSetting(string id, string defaultValue, string user): Constructor that initializes the setting for a specific user.
Class: DTS.Common.Settings.Setting
Abstract base class for settings.
Properties:
string PropertyId: The unique identifier for the setting.PropertyTypes PropertyType: Enum indicatingUser(1) orGlobal(2).string PropertyValue: The string value of the setting.string UserId: The user ID associated with the setting (or "SYSTEM" for global).
Methods:
void SetValue(string value): Sets the value of the property and persists the change to the database.
3. Invariants
- Cache Key Uniqueness: User settings are cached with a composite key format
{id}_{user}, whereas global settings are cached using just theid. - Global User Identity: All
GlobalSettinginstances use the constant string"SYSTEM"as theUserIdin database transactions. - Auto-Creation: Retrieving a non-existent setting via
GetUserValueorGetGlobalValuetriggers an insert operation (StoreInDB) to persist the provided default value. - Array Storage Convention: Arrays are stored as discrete keys in the database.
- Length is stored in key
{id}_x_Count. - Elements are stored in keys
{id}_x_{index}.
- Length is stored in key
- Thread Safety: All public static methods in
SettingsDButilize alockon a staticLOCK_OBJECTto ensure thread-safe access to the singleton_settingsLookupdictionary.
4. Dependencies
Internal Dependencies:
DTS.Common.Storage: Used forDbOperations,DbOperationsEnum, and database connection management.DTS.Common.Utilities.Logging: Used forAPILoggerto log exceptions and messages.
External Dependencies:
System.Data&System.Data.SqlClient: Used for ADO.NET database operations (SqlConnection,SqlCommand,SqlParameter).System.Xml: Used inGlobalSetting.ReadXML.
Database Dependencies:
- Stored Procedure
sp_SettingsGet: Used to retrieve settings. - Stored Procedure
sp_SettingsUpdateInsert: Used to create or update settings.
5. Gotchas
- Silent Failures in
UserSetting: TheGetPropertyValuemethod inUserSettingcatches all exceptions, sets the value to the default, and continues. Database errors or connection issues will be silently ignored, and the user will receive the default value without any indication that the DB read failed. - Ignored DB Errors in
Setting.StoreInDB: TheStoreInDBmethod retrieves@errorNumberand@errorMessageoutput parameters, but if@errorNumberis non-zero, the code block is empty (commented out). Database errors during save are effectively ignored, though the exception is caught and logged. - Null Default Handling:
GlobalSetting.GetPropertyValueexplicitly throwsNullDefaultValueExceptionif the setting is missing from the DB and the provided default value isnull. In contrast,UserSettinghandles this gracefully by setting the internal value to null/empty. - Legacy Setting Migration:
SettingsDBcontains specific hardcoded logic forImportCreateDynamicGroups. When setting this boolean value, the code also updates a legacy keyCSVImportCreateDynamicGroupsto maintain backward compatibility with pre-Version 93 clients. - Redundant Ternary Logic: In
GetGlobalValueBool, the return statement!bool.TryParse(sValue, out b) ? b : bis redundant; it returnsbin both branches. - Connection Disposal: The code manually calls
cmd.Connection.Dispose()inside afinallyblock withinusingstatements. Whileusingdisposes theSqlCommand, manually disposing the connection (which might be shared or managed byDbOperations) could lead to unexpected behavior depending on the implementation ofDbOperations.GetSQLCommand(true).