| 123456789101112131415161718192021222324252627282930313233343536373839 |
- using SqlSugar;
- namespace Admin.NET.Plugin.AiDOP.Service.S8.Rules;
- /// <summary>
- /// S8-DYNAMIC-SQLSUGAR-SCOPE-FACTORY-1:S8 SQL evaluator 独立 SqlSugarScope 构造工厂。
- /// 职责:
- /// 1) 调用 S8DataSourceEndpointNormalizer 规范化 raw endpoint 并派生 EndpointHash;
- /// 2) 以 ConfigId="s8-eval-{endpointHash 前 16 位}" 构造 ConnectionConfig:
- /// 同 endpoint → 同 ConfigId,让 SqlSugar 内部按 ConfigId dedupe,
- /// 并让底层 ADO.NET 连接池按 normalized connection string 与主库共享;
- /// 3) 注入 S8EvaluatorGuard.ApplyCommandTimeout 显式 CommandTimeout:
- /// 独立 scope 不继承 SqlSugarSetup.SetDbAop 的全局 30s;
- /// 4) 返回 SqlSugarScope;不缓存、不持有长生命周期连接、不实现 IDisposable,
- /// 调用方负责 using var scope 自动 Dispose。
- /// 不读 endpoint 原文输出日志、不在异常 message 中携带 connection string 或密码。
- /// </summary>
- public sealed class S8SqlSugarScopeFactory : ITransient
- {
- private const int ConfigIdHashLength = 16;
- private const string ConfigIdPrefix = "s8-eval-";
- public SqlSugarScope CreateScope(string rawEndpoint, DbType dbType, int commandTimeoutSeconds)
- {
- var normalize = S8DataSourceEndpointNormalizer.Normalize(rawEndpoint);
- var configId = ConfigIdPrefix + normalize.EndpointHash.Substring(0, ConfigIdHashLength);
- var scope = new SqlSugarScope(new ConnectionConfig
- {
- ConfigId = configId,
- DbType = dbType,
- ConnectionString = normalize.NormalizedConnectionString,
- InitKeyType = InitKeyType.Attribute,
- IsAutoCloseConnection = true,
- });
- S8EvaluatorGuard.ApplyCommandTimeout(scope, commandTimeoutSeconds);
- return scope;
- }
- }
|