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/外部调用,生产代码行为不变