Skip to content

Commit

Permalink
Fixes issue bbtsoftware#134.
Browse files Browse the repository at this point in the history
CreateConvertHelper method CreateTarget split into Create and Convert methods. Operations adapted, created instances are added to list properties before convert is called.
  • Loading branch information
Stefan Lindegger committed Mar 20, 2023
1 parent 40491f2 commit 71ccfcd
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ public CreateConvertFromStrategyHelper(IGenericStrategyProvider<ICreateConvertSt
}

/// <inheritdoc/>
public TTarget CreateTarget(TSource source, ICollection<IBaseAdditionalProcessing> additionalProcessings)
public void Convert(TSource source, TTarget target, ICollection<IBaseAdditionalProcessing> additionalProcessings)
{
source.NotNull(nameof(source));
additionalProcessings.NotNull(nameof(additionalProcessings));
var strategy = this.strategyProvider.GetStrategy(source);
strategy.Convert(source, target, additionalProcessings);
}

/// <inheritdoc/>
public TTarget Create(TSource source)
{
var strategy = this.strategyProvider.GetStrategy(source);

var concreteTarget = strategy.CreateTarget(source);
strategy.Convert(source, concreteTarget, additionalProcessings);

return concreteTarget;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ public CreateConvertFromStrategyHelperReverse(IGenericStrategyProvider<ICreateCo
}

/// <inheritdoc/>
public TTarget CreateTarget(
TSource source,
TReverseRelation reverseRelation,
ICollection<IBaseAdditionalProcessing> additionalProcessings)
public void Convert(TSource source, TTarget target, ICollection<IBaseAdditionalProcessing> additionalProcessings)
{
source.NotNull(nameof(source));
reverseRelation.NotNull(nameof(reverseRelation));
var strategy = this.strategyProvider.GetStrategy(source);
strategy.Convert(source, target, additionalProcessings);
}

/// <inheritdoc/>
public TTarget Create(TSource source, TReverseRelation reverseRelation)
{
var strategy = this.strategyProvider.GetStrategy(source);
var concreteTarget = strategy.CreateTarget(source);
concreteTarget.SetPropertyValue(this.reverseRelationExpr, reverseRelation);
strategy.Convert(source, concreteTarget, additionalProcessings);

return concreteTarget;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,18 @@ public void SetupReverseRelation(Expression<Func<TTarget, TReverseRelation>> rev
}

/// <inheritdoc/>
public TTarget CreateTarget(
TSource source,
TReverseRelation reverseRelation,
ICollection<IBaseAdditionalProcessing> additionalProcessings)
public TTarget Create(TSource source, TReverseRelation reverseRelation)
{
source.NotNull(nameof(source));
reverseRelation.NotNull(nameof(reverseRelation));
additionalProcessings.NotNull(nameof(additionalProcessings));

var target = this.instanceCreator.Create();
target.SetPropertyValue(this.reverseRelationExpr, reverseRelation);
this.convert.Convert(source, target, additionalProcessings);
return target;
}

/// <inheritdoc/>
public void Convert(TSource source, TTarget target, ICollection<IBaseAdditionalProcessing> additionalProcessings)
{
this.convert.Convert(source, target, additionalProcessings);
}
}

/// <inheritdoc/>
Expand Down Expand Up @@ -92,15 +90,15 @@ public CreateTargetConvertTargetHelper(
}

/// <inheritdoc/>
public TTarget CreateTarget(
TSource source,
ICollection<IBaseAdditionalProcessing> additionalProcessings)
public void Convert(TSource source, TTarget target, ICollection<IBaseAdditionalProcessing> additionalProcessings)
{
source.NotNull(nameof(source));
additionalProcessings.NotNull(nameof(additionalProcessings));
this.convert.Convert(source, target, additionalProcessings);
}

/// <inheritdoc/>
public TTarget Create(TSource source)
{
var target = this.instanceCreator.Create();
this.convert.Convert(source, target, additionalProcessings);
return target;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,18 @@ public void SetupReverseRelation(Expression<Func<TTarget, TReverseRelation>> rev
}

/// <inheritdoc/>
public TTarget CreateTarget(
TSource source,
TReverseRelation reverseRelation,
ICollection<IBaseAdditionalProcessing> additionalProcessings)
public TTarget Create(TSource source, TReverseRelation reverseRelation)
{
source.NotNull(nameof(source));
reverseRelation.NotNull(nameof(reverseRelation));
additionalProcessings.NotNull(nameof(additionalProcessings));

var target = this.instanceCreator.Create();
target.SetPropertyValue(this.reverseRelationExpr, reverseRelation);
this.convert.Convert(source, target, additionalProcessings);
return target;
}

/// <inheritdoc/>
public void Convert(TSource source, TTarget target, ICollection<IBaseAdditionalProcessing> additionalProcessings)
{
this.convert.Convert(source, target, additionalProcessings);
}
}

/// <inheritdoc/>
Expand Down Expand Up @@ -93,15 +91,15 @@ public CreateTargetImplConvertTargetHelper(
}

/// <inheritdoc/>
public TTarget CreateTarget(
TSource source,
ICollection<IBaseAdditionalProcessing> additionalProcessings)
public void Convert(TSource source, TTarget target, ICollection<IBaseAdditionalProcessing> additionalProcessings)
{
source.NotNull(nameof(source));
additionalProcessings.NotNull(nameof(additionalProcessings));
this.convert.Convert(source, target, additionalProcessings);
}

/// <inheritdoc/>
public TTarget Create(TSource source)
{
var target = this.instanceCreator.Create();
this.convert.Convert(source, target, additionalProcessings);
return target;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public CreateTargetImplConvertTargetImplHelper(
}

/// <inheritdoc/>
public TTarget CreateTarget(
TSource source,
ICollection<IBaseAdditionalProcessing> additionalProcessings)
public void Convert(TSource source, TTarget target, ICollection<IBaseAdditionalProcessing> additionalProcessings)
{
source.NotNull(nameof(source));
additionalProcessings.NotNull(nameof(additionalProcessings));
this.convert.Convert(source, (TTargetImpl)target, additionalProcessings);
}

/// <inheritdoc/>
public TTarget Create(TSource source)
{
var target = this.instanceCreator.Create();
this.convert.Convert(source, target, additionalProcessings);

return target;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,19 @@ public void SetupReverseRelation(Expression<Func<TTarget, TReverseRelation>> rev
}

/// <inheritdoc/>
public TTarget CreateTarget(
TSource source,
TReverseRelation reverseRelation,
ICollection<IBaseAdditionalProcessing> additionalProcessings)
public TTarget Create(TSource source, TReverseRelation reverseRelation)
{
source.NotNull(nameof(source));
reverseRelation.NotNull(nameof(reverseRelation));
additionalProcessings.NotNull(nameof(additionalProcessings));

var target = this.instanceCreator.Create();
target.SetPropertyValue(this.reverseRelationExpr, reverseRelation);
this.convert.Convert(source, target, additionalProcessings);

return target;
}

/// <inheritdoc/>
public void Convert(
TSource source, TTarget target, ICollection<IBaseAdditionalProcessing> additionalProcessings)
{
this.convert.Convert(source, (TTargetImpl)target, additionalProcessings);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public void CreateTarget_NoReverseRelation_CreateAndConvert()

var processings = new List<IBaseAdditionalProcessing>();
var source = new SourceDerivedLeaf();
var target = testCandidate.CreateTarget(source, processings);
var target = testCandidate.Create(source);
testCandidate.Convert(source, target, processings);

target.Should().NotBeNull();
target.Should().BeOfType<TargetDerivedLeaf>();
Expand All @@ -55,7 +56,8 @@ public void CreateTarget_WithReverseRelation_CreateAndConvert()
var processings = new List<IBaseAdditionalProcessing>();
var source = new SourceDerivedLeaf();
var reverseRelation = new TargetRoot();
var target = testCandidate.CreateTarget(source, reverseRelation, processings);
var target = testCandidate.Create(source, reverseRelation);
testCandidate.Convert(source, target, processings);

target.Should().NotBeNull();
target.Should().BeOfType<TargetDerivedLeaf>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public void CreateTarget_NoReverseRelation_CreateAndConvert()

var processings = new List<IBaseAdditionalProcessing>();
var source = new SourceTreeLeaf();
var target = testCandidate.CreateTarget(source, processings);
var target = testCandidate.Create(source);
testCandidate.Convert(source, target, processings);

target.Should().NotBeNull();
target.OriginId.Should().Be(source.Id);
Expand All @@ -48,7 +49,8 @@ public void CreateTarget_WithReverseRelation_CreateAndConvert()
var processings = new List<IBaseAdditionalProcessing>();
var source = new SourceTreeLeaf();
var reverseRelation = new TargetTree();
var target = testCandidate.CreateTarget(source, reverseRelation, processings);
var target = testCandidate.Create(source, reverseRelation);
testCandidate.Convert(source, target, processings);

target.Should().NotBeNull();
target.OriginId.Should().Be(source.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public void CreateTarget_NoReverseRelation_CreateAndConvert()

var processings = new List<IBaseAdditionalProcessing>();
var source = new SourceTreeLeaf();
var target = testCandidate.CreateTarget(source, processings);
var target = testCandidate.Create(source);
testCandidate.Convert(source, target, processings);

target.Should().NotBeNull();
target.OriginId.Should().Be(source.Id);
Expand All @@ -48,7 +49,8 @@ public void CreateTarget_WithReverseRelation_CreateAndConvert()
var processings = new List<IBaseAdditionalProcessing>();
var source = new SourceTreeLeaf();
var reverseRelation = new TargetTree();
var target = testCandidate.CreateTarget(source, reverseRelation, processings);
var target = testCandidate.Create(source, reverseRelation);
testCandidate.Convert(source, target, processings);

target.Should().NotBeNull();
target.OriginId.Should().Be(source.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public void CreateTarget_NoReverseRelation_CreateAndConvert()

var processings = new List<IBaseAdditionalProcessing>();
var source = new SourceTreeLeaf();
var target = testCandidate.CreateTarget(source, processings);
var target = testCandidate.Create(source);
testCandidate.Convert(source, target, processings);

target.Should().NotBeNull();
target.OriginId.Should().Be(source.Id);
Expand All @@ -48,7 +49,8 @@ public void CreateTarget_WithReverseRelation_CreateAndConvert()
var processings = new List<IBaseAdditionalProcessing>();
var source = new SourceTreeLeaf();
var reverseRelation = new TargetTree();
var target = testCandidate.CreateTarget(source, reverseRelation, processings);
var target = testCandidate.Create(source, reverseRelation);
testCandidate.Convert(source, target, processings);

target.Should().NotBeNull();
target.OriginId.Should().Be(source.Id);
Expand Down
23 changes: 18 additions & 5 deletions src/BBT.StructureTools/Convert/ICreateConvertHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ public interface ICreateConvertHelper<in TSource, TTarget, TReverseRelation, TCo
void SetupReverseRelation(Expression<Func<TTarget, TReverseRelation>> reverseRelationExpr);

/// <summary>
/// Creates a new <typeparamref name="TTarget"/> converted from <paramref name="source"/>.
/// Creates <typeparamref name="TTarget"/> with <paramref name="reverseRelation"/>.
/// </summary>
TTarget CreateTarget(
TTarget Create(
TSource source,
TReverseRelation reverseRelation,
TReverseRelation reverseRelation);

/// <summary>
/// Converts <paramref name="source"/> into <paramref name="target"/>.
/// </summary>
void Convert(
TSource source,
TTarget target,
ICollection<IBaseAdditionalProcessing> additionalProcessings);
}

Expand All @@ -43,10 +50,16 @@ public interface ICreateConvertHelper<in TSource, TTarget, TConvertIntention>
where TConvertIntention : IBaseConvertIntention
{
/// <summary>
/// Creates a new <typeparamref name="TTarget"/> converted from <paramref name="source"/>.
/// Creates <typeparamref name="TTarget"/>.
/// </summary>
TTarget Create(TSource source);

/// <summary>
/// Converts <paramref name="source"/> into <paramref name="target"/>.
/// </summary>
TTarget CreateTarget(
void Convert(
TSource source,
TTarget target,
ICollection<IBaseAdditionalProcessing> additionalProcessings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ public void Execute(
return;
}

var targetValue = this.createConvertHelper.CreateTarget(
source,
target,
additionalProcessings);
var targetValue = this.createConvertHelper.Create(source, target);

target.SetPropertyValue(
this.targetExpression,
targetValue);

this.createConvertHelper.Convert(
source,
targetValue,
additionalProcessings);
}
}
}
15 changes: 5 additions & 10 deletions src/BBT.StructureTools/Convert/Strategy/OperationCreateToMany.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public void Execute(

var sourceValues = this.sourceFunc.Invoke(source);

var copies = new List<TTargetValue>();

foreach (var sourceValue in sourceValues)
{
if (!this.convertHelper.ContinueConvertProcess<TSourceValue, TTargetValue>(
Expand All @@ -78,15 +76,12 @@ public void Execute(
continue;
}

var copy = this.createConvertHelper.CreateTarget(
sourceValue,
additionalProcessings);
copies.Add(copy);
}
var copy = this.createConvertHelper.Create(sourceValue);

target.AddRangeFilterNullValues(
this.targetExpression,
copies);
target.Add(this.targetExpression, copy);

this.createConvertHelper.Convert(sourceValue, copy, additionalProcessings);
}
}
}
}
Loading

0 comments on commit 71ccfcd

Please sign in to comment.