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
26 changes: 24 additions & 2 deletions src/Mapster.Tests/WhenConfiguringMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ public void NewInstanceConfigurationTest()
obj.Name = "Tim";
obj.Child = new TestNewInstanceF() { Name = "Kıvanç" };

TypeAdapterConfig<TestNewInstanceD, TestNewInstanceE>
TypeAdapterConfig<TestNewInstanceD, TestNewInstanceE>
.NewConfig()
.ShallowCopyForSameType(true);
.ShallowCopyForSameType(true);

var newObj2 = TypeAdapter.Adapt<TestNewInstanceD, TestNewInstanceE>(obj);

Expand All @@ -156,6 +156,28 @@ public void NewInstanceConfigurationTest()
Assert.IsTrue(newObj2.Child.Name == "Antalya");
}

[TestMethod]
public void WhenDirectAssignmentForSameTypeConfigurate()
{
TestNewInstanceD obj = new TestNewInstanceD();
obj.Name = "Tim";
obj.Child = new TestNewInstanceF() { Name = "Kıvanç" };

var config = new TypeAdapterConfig();
config.NewConfig<TestNewInstanceF, TestNewInstanceF>()
.DirectAssignmentForSameType(true);

var newObj2 = TypeAdapter.Adapt<TestNewInstanceD, TestNewInstanceE>(obj, config);

Assert.IsTrue(newObj2.Name == "Tim");
Assert.IsTrue(obj.Child.Name == newObj2.Child.Name);

obj.Child.Name = "Antalya";

Assert.IsTrue(newObj2.Child.Name == "Antalya");
}


#region Data

private Source _source;
Expand Down
8 changes: 6 additions & 2 deletions src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,14 @@ internal Expression CreateAdaptExpression(Expression source, Type destinationTyp
if (_source.Type == destinationType && arg.MapType == MapType.Projection)
return _source;

TypeAdapterRule? rule;
var tuple = new TypeTuple(_source.Type, destinationType);
arg.Context.Config.RuleMap.TryGetValue(tuple, out rule);

//adapt(_source);
var notUsingDestinationValue = mapping is not { UseDestinationValue: true };
var exp = _source.Type == destinationType && arg.Settings.ShallowCopyForSameType == true && notUsingDestinationValue &&
!arg.Context.Config.HasRuleFor(_source.Type, destinationType)
var exp = _source.Type == destinationType && arg.Settings.ShallowCopyForSameType == true && notUsingDestinationValue
&& rule == null
? _source
: CreateAdaptExpressionCore(_source, destinationType, arg, mapping, destination);

Expand Down
13 changes: 13 additions & 0 deletions src/Mapster/Adapters/ClassAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,5 +291,18 @@ private static Expression SetValueByReflection(MemberMapping member, MemberExpre

return Expression.MemberInit(newInstance, lines);
}

protected override Expression CreateExpressionBody(Expression source, Expression? destination, CompileArgument arg)
{
TypeAdapterRule? rule;
var tuple = new TypeTuple(source.Type, arg.DestinationType);
arg.Context.Config.RuleMap.TryGetValue(tuple, out rule);

if (source.Type == arg.DestinationType && !arg.UseDestinationValue
&& arg.Settings.DirectAssignmentForSameType.GetValueOrDefault() && arg.IsNotCustomConverterFactory(rule))
return source;

return base.CreateExpressionBody(source, destination, arg);
}
}
}
8 changes: 8 additions & 0 deletions src/Mapster/TypeAdapterSetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ public static TSetter ShallowCopyForSameType<TSetter>(this TSetter setter, bool
return setter;
}

public static TSetter DirectAssignmentForSameType<TSetter>(this TSetter setter, bool value) where TSetter : TypeAdapterSetter
{
setter.CheckCompiled();

setter.Settings.DirectAssignmentForSameType = value;
return setter;
}

public static TSetter EnumMappingStrategy<TSetter>(this TSetter setter, EnumMappingStrategy strategy) where TSetter : TypeAdapterSetter
{
setter.CheckCompiled();
Expand Down
7 changes: 7 additions & 0 deletions src/Mapster/TypeAdapterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,18 @@ public bool? PreserveReference
get => Get(nameof(PreserveReference));
set => Set(nameof(PreserveReference), value);
}
public bool? DirectAssignmentForSameType
{
get => Get(nameof(DirectAssignmentForSameType));
set => Set(nameof(DirectAssignmentForSameType), value);
}

public bool? ShallowCopyForSameType
{
get => Get(nameof(ShallowCopyForSameType));
set => Set(nameof(ShallowCopyForSameType), value);
}

public bool? IgnoreNullValues
{
get => Get(nameof(IgnoreNullValues));
Expand Down
13 changes: 13 additions & 0 deletions src/Mapster/Utils/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,5 +466,18 @@ public static bool IsNotSelfCreation(this Type type)

return type.GetFieldsAndProperties().All(it => (it.SetterModifier & (AccessModifier.Public | AccessModifier.NonPublic)) == 0);
}

public static bool IsNotCustomConverterFactory(this CompileArgument arg, TypeAdapterRule? rule)
{
if(rule != null)
{
if(arg.MapType == MapType.Map && rule.Settings.ConverterFactory != null)
return false;
if (arg.MapType == MapType.MapToTarget && rule.Settings.ConverterToTargetFactory != null)
return false;
}

return true;
}
}
}
Loading