Repository navigation
支持从 JSON 配置注册数据库 - #45
Merged
Merged
Conversation
- `DbBaseType` 作为方言身份,新增 `IDatabaseRegistry` 可选能力接口, `RegisterDatabase` 以扩展方法挂在 `IExpressionContextSetup` 上, 不破坏第三方实现。 - `ExpressionSqlOptions` 拆为平台无关(Shared)与实例两部分, 便于后续按 TFM 差异化实现。 - 注册表(customDatabases / databaseHandlers / databaseCreators) 统一改为大小写不敏感;新增 `RegisterCreator` / `TryGetCreator` / `ApplyDatabase` / `RemoveDatabase`,支持同名键覆盖与删除。 - 新增 `DatabaseOptions` / `DatabaseDefinition` 配置模型与 `DatabaseConnectionString.Build<T>` 通用连接串拼装: 已有连接串为底、分散字段覆盖,无分散字段时原样返回不归一化。
- `AddLightOrm` 增加可选 `IConfiguration` 参数:配置节 `LightOrmDatabases` 经 AOT 友好的手写绑定(`BindingExtensions`)映射为 `DatabaseList`。 - 新增 `DatabaseListReloader`:构造期同步一次 `CurrentValue`(修复此前 "初始配置永不生效"),随后订阅 `OnChange`;重载在 try/catch 内执行并 记录日志,避免文件监听线程被异常打断。 - 重载支持覆盖同名键、以及删除配置中已消失的库(按 key 差集调用 `RemoveDatabase`);`Type` 找不到登记过的创建器时抛出可读异常并列出 已支持方言名。 - 移除 `AddLightOrm` 内的静态 `reloadToken` / `initialLoaded`,订阅生命周期 交给容器,随 provider 释放。
每个 provider 新增 `Create(DatabaseOptions)` 与无参 `UseXxx()` 重载, 后者只需登记 `RegisterDatabase(DbBaseType.Xxx, d => XxxProvider.Create(d))`。 字段映射(只覆盖"拼出可用连接串必需"的字段,超时/连接池等调优项不收): - Dameng:`DataSource` 优先,否则 `Server`+`Port`(默认 5236)+`UserId`/`Password`。 - PostgreSQL / KingbaseES:`DataSource`→`Host`(回退 `Server`)、`Port`、 `UserId`→`Username`;`Settings["TableSpace"]`。 - SqlServer:`Server`→`Data Source`;`SqlConnectionStringBuilder` 无 Port, 端口折进 `Data Source=host,port`,Server 已含 `,`/`\` 时再给 Port 直接报错。 - MySql:`Port` 转 `uint`;顺手把构造期用正则提取库名的写法换成 `MySqlConnectionStringBuilder(cs).Database`(原正则只认字母下划线, 库名带数字会误判为需要换库)。 - Sqlite:`DataSource` 优先,其次 `Database` 当库文件路径。 - Oracle:`DataSource`(TNS 别名 / 描述符 / EZ Connect)原样透传 → `ServiceName` 拼 `host:port/service` → `Sid` 拼完整描述符 → 裸 `Server` 当 TNS 别名。`Sid` 不用 `host:port/SID`:Oracle 语法里该位置 是 service_name,只在 service 与 SID 同名时"看起来能用",RAC 下必失败; 同时禁止 `Server`+`Port` 却无 ServiceName/Sid(拼出的 `host:port` 既不匹配别名也不是合法 EZ Connect)。
覆盖初始加载、同名键覆盖、删除条目、未知 Type 的可读报错、 Type 大小写不敏感五种场景;用临时目录 + `reloadOnChange: true` 真实走一遍文件监听,`[DoNotParallelize]` 避免静态注册表互相污染, 用完按 key 调 `RemoveDatabase` 清理。 `TestBase` 保持不消费 configuration:多注册几个库会让 `DatabaseProviders.First()` 类断言失效。
`db.json` 随输出复制(PreserveNewest),启动时 `AddJsonFile(optional: false, reloadOnChange: true)`, 缺文件直接启动失败而不是运行期抛 `KeyNotFoundException`; `UseDameng()` 只登记创建器,实际库由配置提供。 示例配置用 `Key = "MainDb"`,否则 `DefaultDbKey` 找不到默认库。 依赖由 `Microsoft.Extensions.Hosting` 换成 `Microsoft.Extensions.Configuration.Json`,不再引入整套 Host。
记录配置驱动注册的落地范围、字段取舍口径(只覆盖拼串必需字段)、 Oracle DataSource 语法结论(`host:port/xxx` 是 service_name, SID 必须完整描述符),以及沙箱下 `dotnet build` 需 `--no-restore`。
- README:新增「从配置文件注册数据库(可选)」小节,给出 db.json 与 `AddLightOrm(..., configuration)` 的最小示例、Key/Type/覆盖规则说明; 「简单配置」与「关键 API 入口」补指针,「核心库内部结构」补 `Options/` 目录。 - examples.md:新增「从配置文件注册数据库」章节,含完整字段表、 各方言拼串规则、优先级与热重载、以及两条易踩的坑 (Type 未登记会报错并列出可选值;键名拼错会被静默忽略)。 - 版本日志:补 v2026.11.08 条目。 - `AddLightOrm` 的 XML summary 内嵌 LightOrmDatabases 的 JSON 结构, 逐字段标注含义与默认值,IDE 悬浮即可查,不必翻文档。
net462 / netstandard2.0 的 BCL 不带 `[NotNullWhen]` 注解, `string.IsNullOrEmpty` 之后编译器仍认为值可能为 null, `ParseInt` / `ParseBool` 的调用补 `!` 断言。仅消除警告,行为不变。
|
| GitGuardian id | GitGuardian status | Secret | Commit | Filename | |
|---|---|---|---|---|---|
| 39820933 | Triggered | Generic Password | 16e6163 | DatabaseUtils.Avalonia/db.json | View secret |
🛠 Guidelines to remediate hardcoded secrets
- Understand the implications of revoking this secret by investigating where it is used in your code.
- Replace and store your secret safely. Learn here the best practices.
- Revoke and rotate this secret.
- If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.
To avoid such incidents in the future consider
- following these best practices for managing and storing secrets including API keys and other credentials
- install secret detection on pre-commit to catch secret before it leaves your machine and ease remediation.
🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
让 LightORM 支持从 JSON 配置注册数据库:配置里写一条
{ Key, Option { Type, Server, ... } },框架按
Type找到对应方言的创建器并生成 provider,不再需要用代码逐个UseXxx(connStr)。支持配置文件热重载(改连接串、加库、删库即时生效)。
使用方式
{ "LightOrmDatabases": [ { "Key": "MainDb", "Option": { "Type": "Dameng", "Server": "localhost", "Port": 5236, "UserId": "SYSDBA", "Password": "SYSDBA001" } } ] }Type大小写不敏感;也可直接给ConnectionString,再用分散字段覆盖其中部分键。变更内容
内核(LightORM)
DbBaseType作为方言身份;新增可选能力接口IDatabaseRegistry,RegisterDatabase以扩展方法挂在IExpressionContextSetup上 —— 不改接口成员,第三方实现不受影响。customDatabases/databaseHandlers/databaseCreators统一改为大小写不敏感;新增
RegisterCreator/TryGetCreator/ApplyDatabase(replace)/RemoveDatabase,支持同名键覆盖(原
TryAdd会让重载静默失效)与删除。DatabaseOptions/DatabaseDefinition/DatabaseList,以及
DatabaseConnectionString.Build<T>:以已有连接串为底、分散字段覆盖,无分散字段时原样返回(不重新归一化,避免改写用户手写串)。
ExpressionSqlOptions拆为平台无关(Shared)与实例两部分。依赖注入扩展
AddLightOrm增加可选IConfiguration参数,配置节LightOrmDatabases经手写 AOT 友好绑定(
BindingExtensions)映射为DatabaseList,不用反射绑定(并为将来 AOT 留路)。
DatabaseListReloader:构造期同步一次CurrentValue(修复"初始配置永不生效"),随后订阅
OnChange;重载包在 try/catch + 日志里,不让文件监听线程被异常打断;按 key 差集清理配置中已删除的库。
Type找不到创建器时抛可读异常并列出已支持方言名。AddLightOrm内的静态reloadToken/initialLoaded,订阅生命周期交给容器。方言接线(7 个)
Create(DatabaseOptions)与无参UseXxx()重载。Datasource / Server / Port / UserId / Password / Database / ServiceName / Sid 等。
(?<=Database=)([A-Z|a-z|_]+)提取库名,库名带数字或非 ASCII 会被误判为"需要切库",改用
MySqlConnectionStringBuilder(cs).Database。Oracle 专项
DataSource原样透传(TNS 别名 / 完整描述符 / EZ Connect 均可);ServiceName拼host:port/service;Sid拼完整描述符;裸
Server当 TNS 别名。host:port/SID:Oracle 语法里该位置是 service_name,仅在 service 与 SID 同名(单实例默认安装)时"看起来能用",RAC 下(SID=ORCL1 / service=ORCL)必失败。
Server+Port却不给ServiceName/Sid:拼出的host:port既不能精确匹配 TNS 别名,也不是合法 EZ Connect,改为抛可读异常。
示例 / 测试 / 版本
DatabaseUtils.Avalonia改为从db.json注册(optional: false,缺文件直接启动失败),依赖由
Microsoft.Extensions.Hosting换成Microsoft.Extensions.Configuration.Json。DatabaseConfigurationTest:初始加载、同名键覆盖、条目删除、未知 Type 报错、大小写不敏感,5/5 通过。2026.11.08.1;移除已不需要的Microsoft.Extensions.Configuration.Binder引用。兼容性
AddLightOrm的新参数是可选参数,现有调用无需修改(二进制层面签名变化,需同版本重编译)。
IExpressionContextSetup成员,第三方实现不会编译失败。configuration = null直接跳过整条链路)。验证
src/LightORM.Extensions.DependencyInjection、7 个 provider、test/LightORMTest全部编译通过(net462;netstandard2.0;net8.0;net10.0)。dotnet test --filter DatabaseConfigurationTest:5/5 通过(net10.0)。待办 / 已知项
DatabaseOptions(配置模型)与既有DataBaseOption<T>(选项对象)命名易混,后续可考虑重命名。LightORMTest的通用用例。