7.5 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |||
|---|---|---|---|---|---|---|---|
|
2026-04-16T04:30:58.292022+00:00 | Qwen/Qwen3-Coder-Next-FP8 | 1 | f40d6df6287d52df |
SettingsDB
Documentation: Global Settings Module (DatabaseImport)
1. Purpose
This module provides a centralized, thread-safe mechanism for managing application-wide (global) settings stored in a SQL Server database. It abstracts the persistence logic for global configuration properties, ensuring that each setting is lazily loaded from the database on first access, falls back to a provided default value if missing, and caches the setting instance in memory for subsequent lookups. It is part of the DatabaseImporter module and serves as the canonical source for global configuration values used during database import operations.
2. Public Interface
SettingsDB (Static Class)
-
public static string GetGlobalValue(string id, string defaultValue)
Retrieves the global setting value forid. If the setting does not exist in the cache or database, it is created withdefaultValue, stored in the DB, and returned. Thread-safe via singleton + lock. -
public static bool GetGlobalValueBool(string id, bool defaultValue)
Retrieves the global setting as abool. Converts the stored string value usingbool.TryParse; if parsing fails, returnsdefaultValue. Thread-safe. -
public static void SetGlobalValue(string id, string value)
Sets the global settingidtovalue. Updates the in-memory cache and persists to the database viaStoreInDB(). Thread-safe. -
public static void SetGlobalValueBoolean(string id, bool value)
Sets the global settingidto the string representation ofvalue(usingInvariantCulture). Updates cache and persists. Thread-safe.
GlobalSetting (Concrete Class, inherits Setting)
public GlobalSetting(string id, string defaultPropertyValue)
Constructor. Initializes aGlobalSettingwithPropertyType = Global,UserId = "SYSTEM", and triggersGetPropertyValue(defaultPropertyValue).
Setting (Abstract Base Class)
-
public string PropertyId { get; }
Read-only identifier of the setting (e.g.,"ImportBatchSize"). -
public string PropertyValue { get; }
Current value of the setting (asstring). Note: No length validation is enforced at this layer. -
public string UserId { get; }
User context for the setting. ForGlobalSetting, this is always"SYSTEM". -
public void SetValue(string value)
Updates_propertyValueand persists the change viaStoreInDB(). -
protected abstract void GetPropertyValue(string defaultValue)
Implemented by subclasses to fetch the value from the database (or other source).GlobalSettingimplements this to query the DB.
3. Invariants
-
Singleton & Thread Safety:
SettingsDBis a singleton with lazy initialization. All public static methods (GetGlobalValue,SetGlobalValue, etc.) are guarded by alock (LOCK_OBJECT)on the singleton instance, ensuring thread-safe access to_settingsLookupand DB operations. -
Global Scope:
GlobalSettinginstances are always associated withUserId = "SYSTEM"andPropertyType = PropertyTypes.Global(value2). -
Default Fallback: If a setting does not exist in the database,
GetPropertyValuefalls back to the provideddefaultValue, stores it in the DB viaStoreInDB(), and returns it. -
Caching: Once instantiated, a
Settingobject is cached inSettingsDB._settingsLookup. Subsequent lookups for the sameidreuse the cached instance. -
DB Schema Assumption: The module assumes:
- A stored procedure
sp_SettingsGetexists, accepting@UserId(NVARCHAR) and@PropertyId(NVARCHAR), returning a result set with a column namedPropertyValue(viaDbOperations.Settings.UserFields.PropertyValue). - A stored procedure
sp_SettingsUpdateInsertexists, accepting parameters:@PropertyId,@PropertyType,@PropertyValue,@UserId,@new_id(OUTPUT),@errorNumber(OUTPUT),@errorMessage(OUTPUT). All string parameters are NVARCHAR(255).
- A stored procedure
4. Dependencies
Dependencies of this module:
System.DataandSystem.Data.SqlClient(forSqlDataAdapter,SqlCommand,SqlParameter,SqlDbType, etc.)DbOperations(static class, not shown) — provides:GetSQLCommand(bool)→ returnsSqlCommandConnection.QueryDataSet(SqlCommand)→ returnsDataSetSettings.UserFields.PropertyValue→ string constant for column name (e.g.,"PropertyValue")DbOperationsEnum.StoredProcedure.sp_SettingsGet,sp_SettingsUpdateInsert→ enum values for stored procedure names.
Dependencies on this module:
- Any code requiring global configuration (e.g., import batch size, timeout, feature flags) calls
SettingsDB.GetGlobalValue(...)orGetGlobalValueBool(...).
Inferred callers:
DatabaseImportermodule (e.g., import pipeline components that read global settings like"MaxRetryCount","DefaultSchema").
5. Gotchas
-
Exception Handling is Minimal:
GetPropertyValueandStoreInDBboth catchExceptionand silently fall back todefaultValueor do nothing, respectively. No logging is active (commented-outAPILogger.LogExceptionsuggests future intent). This makes debugging DB issues difficult. -
No Validation on Value Length:
PropertyValueis stored asNVARCHAR(255)in DB calls, but no validation occurs inSettingorGlobalSetting. Passing a >255-character value may cause a DB error (silently ignored inStoreInDB). -
Race Condition in Initialization (Low Risk):
WhileGetGlobalValuelocks before checking_settingsLookup, the first call for a newidcreates a newGlobalSettinginside the lock. However,GlobalSettingconstructor callsGetPropertyValue, which performs a DB round-trip while holding the lock. This can cause contention if many settings are requested concurrently for the first time. -
Hardcoded
"SYSTEM"User:
GlobalSettinghardcodesUserId = "SYSTEM". This is not configurable and assumes all global settings are owned by a system user. -
_allGlobalSettingCache is Unused:
TheGetPropertyValuemethod contains commented-out code for a_allGlobalSettingcache. This suggests a previous optimization attempt that was reverted or incomplete — the current implementation hits the DB on every first access for a newid. -
Culture-Specific Boolean Parsing:
GetGlobalValueBoolusesdefaultValue.ToString(CultureInfo.InvariantCulture)for the fallback string, butbool.TryParseis culture-insensitive for"True"/"False". This is safe, but the use ofInvariantCulturehere is redundant and could mislead. -
No Explicit Disposal of
SettingInstances:
Settinginstances are stored in_settingsLookupand never removed. If settings are frequently added/removed at runtime, this could lead to memory leaks. -
No Distinction Between
nulland Empty String:
If the DB storesNULLforPropertyValue,Convert.ToString(...)returns""(empty string), notnull. This may or may not be intended.
Documentation generated from provided source files. No external assumptions or behaviors inferred beyond what is explicitly present.