Files
DP44/enriched-qwen3-coder-next/Common/DTS.Common.Core/Config.md
2026-04-17 14:55:32 -04:00

68 lines
5.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
source_files:
- Common/DTS.Common.Core/Config/DTSConfig.cs
generated_at: "2026-04-16T02:05:43.907445+00:00"
model: "Qwen/Qwen3-Coder-Next-FP8"
schema_version: 1
sha256: "2693b2d1a0d0acee"
---
# Config
### **1. Purpose**
The `DTSConfig` class provides a centralized, thread-safe mechanism for loading and accessing an *alternate* application configuration file (e.g., for plugin-specific or test-specific configs), overriding the default `app.config`/`web.config`. It enables runtime configuration of the config file path via `DTSConfigInit`, then exposes typed accessors (`GetAppSetting`, `GetSection`) to read settings and custom sections from that alternate file using .NETs `System.Configuration` APIs. This supports scenarios like plugin isolation or environment-specific overrides without modifying the main executables config.
---
### **2. Public Interface**
- **`static void DTSConfigInit(string path)`**
Initializes the alternate configuration by setting the config file path and loading the configuration. Internally calls `SetAltConfigPath(path)`. If `path` is `null` or empty, falls back to `DTSConstants.CustomConfigPath`. Must be called before other config accessors to ensure `AltConfig` is populated.
- **`static string GetAppSetting(string key)`**
Retrieves the value of an `<add key="..." value="..."/>` entry from the `appSettings` section of the alternate configuration. Returns `string.Empty` if the key is not found. Logs a warning via `APILogger` when the key is absent.
- **`static object GetSection(string sectionName)`**
Retrieves a custom configuration section (e.g., `<section name="..." type="..." />`) by name from the alternate configuration. Returns `null` if the section is not defined. Logs a warning via `APILogger` when the section is absent.
- **`static string AltConfigPathGet()`**
Thread-safe getter for the current alternate config file path (stored in `AltConfigPath`). Returns the path string (may be `null` or empty if not set or reset).
- **`static void AltConfigPathSet(string path)`**
Thread-safe setter for the alternate config file path. Updates `AltConfigPath` but *does not reload* the configuration—`SetAltConfigPath` must be called to apply the new path and reinitialize `AltConfig`.
---
### **3. Invariants**
- **Thread Safety**: All access to `AltConfigPath` and `AltConfig` is guarded by a private static lock object (`MyLock`). Reads (`AltConfigPathGet`, `GetAltConfig`, `GetAppSetting`, `GetSection`) and writes (`AltConfigPathSet`, `SetAltConfigPath`) are serialized.
- **Fallback Behavior**: When `AltConfigPath` is `null` or empty, `SetAltConfigPath` uses `DTSConstants.CustomConfigPath` as the config file path.
- **Configuration Loading**: `AltConfig` is lazily initialized on first call to `SetAltConfigPath` (via `DTSConfigInit` or direct invocation) and *not* reloaded automatically on subsequent path changes—`SetAltConfigPath` must be called again to refresh it.
- **Error Handling**: Missing keys/sections are *not* thrown as exceptions; instead, they return `string.Empty`/`null` with a log message. No fallback to default config is attempted.
---
### **4. Dependencies**
- **Internal Dependencies**:
- `DTS.Common.Utilities.Logging.APILogger` — Used for logging missing config entries/sections.
- `System.Configuration.ConfigurationManager`, `ExeConfigurationFileMap`, `ConfigurationUserLevel` — For loading and accessing configuration files.
- `DTSConstants.CustomConfigPath` — Fallback path when `AltConfigPath` is unset.
- **External Dependencies**:
- `System.Configuration` assembly (standard .NET Framework/Standard).
- No direct runtime dependencies beyond .NET config APIs.
- **Depended Upon By**:
- Plugin code (per `GetSection`s XML comment) to access plugin-specific configuration sections.
- Any module requiring runtime-configurable settings without modifying the main executables config.
---
### **5. Gotchas**
- **`AltConfigPathSet` does not reload config**: Calling `AltConfigPathSet` alone updates the stored path but *does not* reinitialize `AltConfig`. The configuration must be reloaded via `SetAltConfigPath` (e.g., `DTSConfigInit`) to apply changes.
- **No fallback to main config**: If the alternate config file is missing, invalid, or lacks a requested key/section, the system logs a warning but returns `string.Empty`/`null`—it does *not* fall back to the default `app.config`.
- **`DTSConstants.CustomConfigPath` is assumed but not defined here**: The fallback path relies on an external constant (`DTSConstants.CustomConfigPath`). Its value and behavior are not visible in this file.
- **Thread-safety only covers state, not config validity**: While access to `AltConfig` is thread-safe, concurrent calls to `SetAltConfigPath` may cause transient inconsistencies (e.g., one thread sets a path while another reads `AltConfig` before reinitialization completes).
- **No validation of config file path**: `SetAltConfigPath` does not validate whether the provided path exists or is readable; `ConfigurationManager.OpenMappedExeConfiguration` will throw if the file is inaccessible (e.g., `FileNotFoundException`), but this is not handled internally.