Files

104 lines
6.5 KiB
Markdown
Raw Permalink Normal View History

2026-04-17 14:55:32 -04:00
---
source_files:
- DataPRO/Modules/PreviousDBVersions/Version57/DatabaseExport/SettingsDB/Setting.cs
- DataPRO/Modules/PreviousDBVersions/Version57/DatabaseExport/SettingsDB/GlobalSetting.cs
- DataPRO/Modules/PreviousDBVersions/Version57/DatabaseExport/SettingsDB/SettingsDB.cs
generated_at: "2026-04-16T04:58:48.501962+00:00"
model: "Qwen/Qwen3-Coder-Next-FP8"
schema_version: 1
sha256: "f305fd0e552dc54c"
---
# SettingsDB
## Documentation: `SettingsDB` Module (Version 57)
### 1. Purpose
This module provides a centralized, thread-safe mechanism for managing **global application settings** stored in a database. It abstracts database access for reading and writing string-based settings, caching them in-memory via a singleton pattern to avoid repeated DB queries. It is part of a legacy database export module (`Version57`) and is intended for use during migration or export workflows where persistent global configuration is needed. The module does *not* support user-specific settings in the current implementation.
---
### 2. Public Interface
#### `SettingsDB` (Static Class)
A singleton-managed static class for global setting operations.
- **`public static string GetGlobalValue(string id, string defaultValue)`**
Retrieves the global setting value for `id`. If not found in DB or cache, returns `defaultValue` after initializing and caching a `GlobalSetting`.
*Thread-safe via lock.*
- **`public static int GetGlobalValueInt(string id, int defaultValue)`**
Retrieves the global setting as an `int`. Converts the stored string value using `int.TryParse` with `CultureInfo.InvariantCulture`. Returns `defaultValue` on parse failure.
- **`public static bool GetGlobalValueBool(string id, bool defaultValue)`**
Retrieves the global setting as a `bool`. Converts the stored string value using `bool.TryParse`. Returns `defaultValue` on parse failure.
- **`public static void SetGlobalValue(string id, string value)`**
Stores `value` as the global setting for `id`. Updates in-memory cache and calls `SetValue()` on the cached `GlobalSetting`.
*Note: Actual DB persistence is commented out (`//StoreInDB();`) in `Setting.SetValue()` and `GlobalSetting.GetPropertyValue()`.*
- **`public static void SetGlobalValueInt(string id, int value)`**
Stores `value` as a string (using `CultureInfo.InvariantCulture`) for global setting `id`.
- **`public static void SetGlobalValueBoolean(string id, bool value)`**
Stores `value` as a string (`"True"`/`"False"`) for global setting `id`.
#### `Setting` (Abstract Base Class)
- **`public string PropertyId { get; }`**
Read-only identifier for the setting (e.g., `"Theme"`).
- **`public string PropertyValue { get; }`**
Current value of the setting (string). *No length validation is performed.*
- **`protected abstract void GetPropertyValue(string defaultValue)`**
Implemented by subclasses to populate `_propertyValue` from DB or fallback to `defaultValue`.
- **`public void SetValue(string value)`**
Updates `_propertyValue` in-memory. *DB persistence is disabled (commented out).*
#### `GlobalSetting` (Concrete Class)
- **`public GlobalSetting(string id, string defaultPropertyValue)`**
Constructor for global settings. Hardcodes `_userId = "SYSTEM"`.
- **`protected override void GetPropertyValue(string defaultValue)`**
Queries DB table `DbOperations.Settings.Table` for a row matching `PropertyId = @PropertyId AND UserId = "SYSTEM"`.
- On success: sets `_propertyValue` from DB column `PropertyValue`.
- On failure (no row, exception): sets `_propertyValue = defaultValue`.
*DB persistence on write is disabled (commented out).*
---
### 3. Invariants
- **Singleton enforcement**: `SettingsDB.Instance` is lazily initialized and thread-safe via `lock (_MyLock)`.
- **Cache consistency**: `_SettingsLookup` dictionary keys are `PropertyId` strings; each entry is a `Setting` instance (currently only `GlobalSetting` used).
- **Global-only scope**: `_userId` is hardcoded to `"SYSTEM"` in `GlobalSetting`; no user-specific settings are supported.
- **DB schema assumptions**:
- Table name: `DbOperations.Settings.Table`
- Columns: `PropertyId`, `UserId`, `PropertyValue` (all `NVarChar`).
- **No persistence**: `StoreInDB()` calls are commented out in both `Setting.SetValue()` and `GlobalSetting.GetPropertyValue()`. Thus, `SetGlobalValue*` methods *only update the in-memory cache* and do not write to the database.
- **Culture invariance**: All numeric/boolean conversions use `CultureInfo.InvariantCulture`.
---
### 4. Dependencies
- **Internal dependencies**:
- `DbOperations` (namespace/class referenced but not provided): Used for DB access (`GetCommand`, `CreateParam`, `QueryDataSet`, `Settings.Table`, `Settings.UserFields.*`).
- `System`, `System.Collections.Generic`, `System.Data` (explicitly imported).
- **Assumed external contracts**:
- `DbOperations.Settings.UserFields.PropertyId`, `UserId`, `PropertyValue` must correspond to valid column names in the settings table.
- Database table `DbOperations.Settings.Table` must exist with schema matching usage.
- **Depended on by**: Unknown (module is in `PreviousDBVersions/Version57/...` path, suggesting it is legacy/migration code).
---
### 5. Gotchas
- **No DB persistence**: Despite the modules purpose, *all writes are in-memory only*. `StoreInDB()` calls are commented out in both `Setting.SetValue()` and `GlobalSetting.GetPropertyValue()`. This is likely intentional for export/migration workflows but is a critical behavioral constraint.
- **No user-specific settings**: The `UserId` property and related logic are commented out in `Setting`; only `"SYSTEM"` is supported.
- **No validation**: `PropertyValue` accepts arbitrary strings without length checks, despite a "max length" being noted in comments.
- **Thread-safety scope**: Thread-safety applies only to cache access (`_SettingsLookup`), *not* to DB operations (which are not active).
- **Culture sensitivity**: Boolean/integer parsing uses `InvariantCulture`, but string storage does not enforce formatting (e.g., `"True"` vs `"true"`). `bool.TryParse` is case-sensitive (only `"True"`/`"False"` work).
- **Exception swallowing**: `GlobalSetting.GetPropertyValue()` silently falls back to `defaultValue` on any exception, masking DB errors.
- **Hardcoded `"SYSTEM"`**: The string `"SYSTEM"` is duplicated in `GlobalSetting` constructor and `GetPropertyValue`; no constant or enum is used.
- **No cache invalidation**: `SetGlobalValue*` updates the cache but does not clear it on external DB changes.
*None identified beyond these.*