Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1981 Cloning module crashes #1982

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions src/OSPSuite.Core/Domain/Services/CloneManagerForBuildingBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ public interface ICloneManagerForBuildingBlock : ICloneManager
/// needs to be used.
/// </summary>
IFormulaCache FormulaCache { get; set; }

/// <summary>
/// Clones the given building block adding all cloned formulas into the <see cref="FormulaCache"/> of the clone.
/// </summary>
/// <typeparam name="T">Type of building block to clone</typeparam>
/// <param name="buildingBlock">The building block to clone</param>
/// <returns>The cloned building block</returns>
T CloneBuildingBlock<T>(T buildingBlock) where T : class, IBuildingBlock;
}

public class CloneManagerForBuildingBlock : CloneManagerStrategy, ICloneManagerForBuildingBlock
Expand All @@ -60,19 +52,24 @@ public CloneManagerForBuildingBlock(IObjectBaseFactory objectBaseFactory, IDataR
_clonedFormulasByOriginalFormulaId = new Cache<string, IFormula>();
}

public T Clone<T>(T objectToClone, IFormulaCache formulaCache) where T : class, IObjectBase
public override T Clone<T>(T objectToClone)
{
if (formulaCache == null)
throw new ArgumentNullException(Error.NullFormulaCachePassedToClone);
if(objectToClone is IBuildingBlock buildingBlock)
return cloneBuildingBlock(buildingBlock) as T;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the as T?


return base.Clone(objectToClone);
}

FormulaCache = formulaCache;
public T Clone<T>(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))
Expand All @@ -83,7 +80,7 @@ public IFormulaCache FormulaCache
}
}

public T CloneBuildingBlock<T>(T buildingBlock) where T : class, IBuildingBlock
private T cloneBuildingBlock<T>(T buildingBlock) where T : class, IBuildingBlock
{
var formulaCache = new FormulaCache();
var clone = Clone(buildingBlock, formulaCache);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using FakeItEasy;
using OSPSuite.BDDHelper;
using OSPSuite.BDDHelper.Extensions;
Expand All @@ -8,6 +7,7 @@
using OSPSuite.Core.Domain.Services;
using OSPSuite.Core.Domain.UnitSystem;
using OSPSuite.Helpers;
using System.Linq;

namespace OSPSuite.Core.Domain
{
Expand Down Expand Up @@ -157,7 +157,7 @@ protected override void Context()

protected override void Because()
{
_clone = sut.CloneBuildingBlock(_buildingBlock);
_clone = sut.Clone(_buildingBlock);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that test was failing exactly the same way

}

[Observation]
Expand Down