From cf8b5e604c46db9b034daf1592b1493a0c47de7b Mon Sep 17 00:00:00 2001
From: Robert McIntosh <261477+rwmcintosh@users.noreply.github.com>
Date: Tue, 18 Apr 2023 13:22:18 -0400
Subject: [PATCH] Fixes #1981 Cloning module crashes
---
.../Services/CloneManagerForBuildingBlock.cs | 29 +++++++++----------
.../CloneManagerForBuildingBlockSpecs.cs | 4 +--
2 files changed, 15 insertions(+), 18 deletions(-)
diff --git a/src/OSPSuite.Core/Domain/Services/CloneManagerForBuildingBlock.cs b/src/OSPSuite.Core/Domain/Services/CloneManagerForBuildingBlock.cs
index 946b15b61..d80455f0c 100644
--- a/src/OSPSuite.Core/Domain/Services/CloneManagerForBuildingBlock.cs
+++ b/src/OSPSuite.Core/Domain/Services/CloneManagerForBuildingBlock.cs
@@ -38,14 +38,6 @@ public interface ICloneManagerForBuildingBlock : ICloneManager
/// needs to be used.
///
IFormulaCache FormulaCache { get; set; }
-
- ///
- /// Clones the given building block adding all cloned formulas into the of the clone.
- ///
- /// Type of building block to clone
- /// The building block to clone
- /// The cloned building block
- T CloneBuildingBlock(T buildingBlock) where T : class, IBuildingBlock;
}
public class CloneManagerForBuildingBlock : CloneManagerStrategy, ICloneManagerForBuildingBlock
@@ -60,19 +52,24 @@ public CloneManagerForBuildingBlock(IObjectBaseFactory objectBaseFactory, IDataR
_clonedFormulasByOriginalFormulaId = new Cache();
}
- public T Clone(T objectToClone, IFormulaCache formulaCache) where T : class, IObjectBase
+ public override T Clone(T objectToClone)
{
- if (formulaCache == null)
- throw new ArgumentNullException(Error.NullFormulaCachePassedToClone);
+ if(objectToClone is IBuildingBlock buildingBlock)
+ return cloneBuildingBlock(buildingBlock) as T;
+
+ return base.Clone(objectToClone);
+ }
- FormulaCache = formulaCache;
+ public T Clone(T objectToClone, IFormulaCache formulaCache) where T : class, IObjectBase
+ {
+ FormulaCache = formulaCache ?? throw new ArgumentNullException(Error.NullFormulaCachePassedToClone);
- return Clone(objectToClone);
+ return base.Clone(objectToClone);
}
public IFormulaCache FormulaCache
{
- get { return _formulaCache; }
+ get => _formulaCache;
set
{
if (Equals(value, _formulaCache))
@@ -83,7 +80,7 @@ public IFormulaCache FormulaCache
}
}
- public T CloneBuildingBlock(T buildingBlock) where T : class, IBuildingBlock
+ private T cloneBuildingBlock(T buildingBlock) where T : class, IBuildingBlock
{
var formulaCache = new FormulaCache();
var clone = Clone(buildingBlock, formulaCache);
@@ -110,7 +107,7 @@ protected override IFormula CreateFormulaCloneFor(IFormula srcFormula)
if (_clonedFormulasByOriginalFormulaId.Contains(srcFormula.Id))
return _clonedFormulasByOriginalFormulaId[srcFormula.Id];
- // Formula is neither present in the passed formula cahce nor
+ // Formula is neither present in the passed formula cache nor
// was already cloned. So create a new clone and insert it
// in both target formula cache and the cache of all cloned formulas
var clonedFormula = CloneFormula(srcFormula);
diff --git a/tests/OSPSuite.Core.Tests/Domain/CloneManagerForBuildingBlockSpecs.cs b/tests/OSPSuite.Core.Tests/Domain/CloneManagerForBuildingBlockSpecs.cs
index 5f3e9ac08..a8a1e8bf7 100644
--- a/tests/OSPSuite.Core.Tests/Domain/CloneManagerForBuildingBlockSpecs.cs
+++ b/tests/OSPSuite.Core.Tests/Domain/CloneManagerForBuildingBlockSpecs.cs
@@ -1,4 +1,3 @@
-using System.Linq;
using FakeItEasy;
using OSPSuite.BDDHelper;
using OSPSuite.BDDHelper.Extensions;
@@ -8,6 +7,7 @@
using OSPSuite.Core.Domain.Services;
using OSPSuite.Core.Domain.UnitSystem;
using OSPSuite.Helpers;
+using System.Linq;
namespace OSPSuite.Core.Domain
{
@@ -157,7 +157,7 @@ protected override void Context()
protected override void Because()
{
- _clone = sut.CloneBuildingBlock(_buildingBlock);
+ _clone = sut.Clone(_buildingBlock);
}
[Observation]