From 30ae93b8ca145cac13c8fa6fb0074918fc91fd5a Mon Sep 17 00:00:00 2001 From: hjhan Date: Tue, 11 Aug 2026 09:23:01 +0800 Subject: [PATCH] =?UTF-8?q?fix(interest):=20SwapDealIndexFixer=20=E6=94=B9?= =?UTF-8?q?=E7=94=A8=E5=A7=94=E6=89=98,=20=E4=BF=AE=E5=A4=8D=20CS0122=20?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题: SwapDealIndexFixer(internal)访问 SwapDealService.TryGetFloatRate (protected) 报 CS0122, 因非嵌套类无权访问 protected 成员。 修复: SwapDealIndexFixer 改为接受 Func 委托, SwapDealService.IndexFixer 属性传入 TryGetFloatRate 的包装。 - 无需访问 protected 成员 - SwapDealIndexFixer 保持独立文件(clean code) - virtual 接缝仍保留(测试 override TryGetFloatRate 仍生效) 验证: YLErpDAL.csproj 编译 0 错误, 全量480测试7失败(基线一致,零回归), CI_007/CI_008 通过。 --- .../Modules/SwapModule/SwapDealIndexFixer.cs | 19 +++++++++++-------- .../Modules/SwapModule/SwapDealService.cs | 4 +++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/YLErpDAL/Modules/SwapModule/SwapDealIndexFixer.cs b/YLErpDAL/Modules/SwapModule/SwapDealIndexFixer.cs index 0ba1acc9..e8fe8931 100644 --- a/YLErpDAL/Modules/SwapModule/SwapDealIndexFixer.cs +++ b/YLErpDAL/Modules/SwapModule/SwapDealIndexFixer.cs @@ -4,21 +4,24 @@ using YLErp.Derivatives.Interest; namespace YLErp.Modules.SwapModule; /// -/// SwapDealService 专用取价器:通过实例方法委托给 TryGetFloatRate(virtual), -/// 而非 Fr007IndexFixer.Instance 那样直接调 EodPriceQueryService。 +/// 把一个取价委托包装成 IIndexFixer。 /// -/// 为什么不用静态 Instance:SwapDealService.TryGetFloatRate 是 protected virtual, -/// 测试通过 override 它注入 stub 利率。静态 Instance 绕过这个接缝会让 45 个测试失败。 -/// 本类把 TryGetFloatRate 包成 IIndexFixer,既保留 virtual 接缝,又用上 IndexFixerBase。 +/// 为什么不直接用 Fr007IndexFixer.Instance:SwapDealService.TryGetFloatRate 是 +/// protected virtual,测试 override 它注入 stub。静态 Instance 直接调 +/// EodPriceQueryService 会绕过这个接缝,导致测试失败。 +/// 本类接受取价委托(SwapDealService 传入 this.TryGetFloatRate 的包装), +/// 既保留 virtual 接缝,又用上 IndexFixerBase,且无需访问 protected 成员。 /// internal sealed class SwapDealIndexFixer : IndexFixerBase, IIndexFixer { - private readonly SwapDealService _owner; - internal SwapDealIndexFixer(SwapDealService owner) => _owner = owner; + private readonly Func _tryGet; + + internal SwapDealIndexFixer(Func tryGet) + => _tryGet = tryGet; public bool TryGetFixing(DateTime fixingDate, string underlyingCode, out decimal rate) { - bool ok = _owner.TryGetFloatRate(fixingDate, underlyingCode, out double r); + var (ok, r) = _tryGet(fixingDate, underlyingCode); rate = Convert.ToDecimal(r); return ok; } diff --git a/YLErpDAL/Modules/SwapModule/SwapDealService.cs b/YLErpDAL/Modules/SwapModule/SwapDealService.cs index c2a74be4..5bc0a222 100644 --- a/YLErpDAL/Modules/SwapModule/SwapDealService.cs +++ b/YLErpDAL/Modules/SwapModule/SwapDealService.cs @@ -26,7 +26,9 @@ namespace YLErp.Modules.SwapModule private IIndexFixer _indexFixer; /// FR007 取价器,委托 TryGetFloatRate(保留 virtual 接缝供测试 stub)。 - protected virtual IIndexFixer IndexFixer => _indexFixer ??= new SwapDealIndexFixer(this); + protected virtual IIndexFixer IndexFixer + => _indexFixer ??= new SwapDealIndexFixer((d, c) => + TryGetFloatRate(d, c, out double r) ? (true, r) : (false, 0d)); #region 可测试化接缝(Seams)——override 这些虚方法可在测试中替换 DB/外部调用,生产代码行为不变