Skip to content
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
8 changes: 8 additions & 0 deletions src/Mapster.Tests/WhenUsingDestinationValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@ public class WhenUsingDestinationValue
public void MapUsingDestinationValue()
{
TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileWithDebugInfo();
TypeAdapterConfig.GlobalSettings.Default.ShallowCopyForSameType(true);
TypeAdapterConfig<Invoice, InvoiceDto>.NewConfig().TwoWays();

var strings = new[] { "One, Two, Three" };
var dto = new InvoiceDto
{
Id = 1,
DocumentNumber = "AA001",
SupplierCompany = "COM01",
SupplierName = "Apple",
Numbers = Enumerable.Range(1, 5).ToList(),
Strings = strings,
};
var poco = dto.Adapt<Invoice>();
poco.Id.ShouldBe(dto.Id);
poco.DocumentNumber.ShouldBe("FOO");
poco.Supplier.Name.ShouldBe(dto.SupplierName);
poco.Supplier.Company.ShouldBe(dto.SupplierCompany);
poco.Numbers.ShouldBe(Enumerable.Range(1, 5));
poco.Strings.ShouldBe(strings);
}

public class ContractingParty
Expand All @@ -49,6 +53,9 @@ public class Invoice

[UseDestinationValue]
public ICollection<int> Numbers { get; } = new List<int>();

[UseDestinationValue]
public ICollection<string> Strings { get; } = new List<string>();
}

public class InvoiceDto
Expand All @@ -58,6 +65,7 @@ public class InvoiceDto
public string SupplierName { get; set; }
public string SupplierCompany { get; set; }
public IEnumerable<int> Numbers { get; set; }
public ICollection<string> Strings { get; set; }
}
}
}
18 changes: 12 additions & 6 deletions src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,17 +450,23 @@ protected Expression CreateAdaptExpression(Expression source, Type destinationTy
}
internal Expression CreateAdaptExpression(Expression source, Type destinationType, CompileArgument arg, MemberMapping? mapping, Expression? destination = null)
{
if (source.Type == destinationType &&
(arg.Settings.ShallowCopyForSameType == true || arg.MapType == MapType.Projection))
if (source.Type == destinationType && arg.MapType == MapType.Projection)
return source;

//adapt(source);
var exp = CreateAdaptExpressionCore(source, destinationType, arg, mapping, destination);
var notUsingDestinationValue = mapping is not { UseDestinationValue: true };
var exp = source.Type == destinationType && arg.Settings.ShallowCopyForSameType == true && notUsingDestinationValue
? source
: CreateAdaptExpressionCore(source, destinationType, arg, mapping, destination);

//transform(adapt(source));
var transform = arg.Settings.DestinationTransforms.Find(it => it.Condition(exp.Type));
if (transform != null)
exp = transform.TransformFunc(exp.Type).Apply(arg.MapType, exp);
if (notUsingDestinationValue)
{
var transform = arg.Settings.DestinationTransforms.Find(it => it.Condition(exp.Type));
if (transform != null)
exp = transform.TransformFunc(exp.Type).Apply(arg.MapType, exp);
}

return exp.To(destinationType);
}
}
Expand Down