Skip to content

Commit

Permalink
Refactor TryToMapCollection method.
Browse files Browse the repository at this point in the history
  • Loading branch information
maliming committed Dec 18, 2024
1 parent cd3291e commit 803159b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,9 @@ public virtual TDestination Map<TSource, TDestination>(TSource source)
return specificMapper.Map(source);
}

if (!(typeof(TSource).IsEnum || typeof(TSource).IsPrimitive))
if (TryToMapCollection<TSource, TDestination>(scope, source, default, out var collectionResult))
{
var result = TryToMapCollection<TSource, TDestination>(scope, source, default);
if (result != null)
{
return result;
}
return collectionResult;
}
}

Expand Down Expand Up @@ -103,10 +99,9 @@ public virtual TDestination Map<TSource, TDestination>(TSource source, TDestinat
return specificMapper.Map(source, destination);
}

var result = TryToMapCollection(scope, source, destination);
if (result != null)
if (TryToMapCollection(scope, source, destination, out var collectionResult))
{
return result;
return collectionResult;
}
}

Expand All @@ -125,19 +120,21 @@ public virtual TDestination Map<TSource, TDestination>(TSource source, TDestinat
return AutoMap(source, destination);
}

protected virtual TDestination? TryToMapCollection<TSource, TDestination>(IServiceScope serviceScope, TSource source, TDestination? destination)
protected virtual bool TryToMapCollection<TSource, TDestination>(IServiceScope serviceScope, TSource source, TDestination? destination, out TDestination collectionResult)
{
if (!IsCollectionGenericType<TSource, TDestination>(out var sourceArgumentType, out var destinationArgumentType, out var definitionGenericType))
{
return default;
collectionResult = default!;
return false;
}

var mapperType = typeof(IObjectMapper<,>).MakeGenericType(sourceArgumentType, destinationArgumentType);
var specificMapper = serviceScope.ServiceProvider.GetService(mapperType);
if (specificMapper == null)
{
//skip, no specific mapper
return default;
collectionResult = default!;
return false;
}

var cacheKey = $"{mapperType.FullName}_{(destination == null ? "MapMethodWithSingleParameter" : "MapMethodWithDoubleParameters")}";
Expand Down Expand Up @@ -212,11 +209,13 @@ public virtual TDestination Map<TSource, TDestination>(TSource source, TDestinat
if (destination != null && destination.GetType().IsArray)
{
//Return the new collection if destination is an array, We won't change array just same behavior as AutoMapper.
return (TDestination)result;
collectionResult = (TDestination)result;
return true;
}

//Return the destination if destination exists. The parameter reference equals with return object.
return destination ?? (TDestination)result;
collectionResult = destination ?? (TDestination)result;
return true;
}

protected virtual bool IsCollectionGenericType<TSource, TDestination>(out Type sourceArgumentType, out Type destinationArgumentType, out Type definitionGenericType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Should_Map_Objects_With_AutoMap_Attributes()
public void Should_Map_Enum()
{
var dto = _objectMapper.Map<MyEnum, MyEnumDto>(MyEnum.Value3);
dto.ShouldBe(MyEnumDto.Value3);
dto.ShouldBe(MyEnumDto.Value2); //Value2 is same as Value3
}

//[Fact] TODO: Disabled because of https://github.com/AutoMapper/AutoMapper/pull/2379#issuecomment-355899664
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Volo.Abp.AutoMapper.SampleClasses;

namespace Volo.Abp.AutoMapper.SampleClasses;
public enum MyEnum { Value1, Value2, Value3 };
public enum MyEnum
{
Value1 = 1,
Value2,
Value3
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Volo.Abp.AutoMapper.SampleClasses;

namespace Volo.Abp.AutoMapper.SampleClasses;
public enum MyEnumDto { Value1, Value2, Value3 };
public enum MyEnumDto
{
Value1 = 2,
Value2,
Value3
}

0 comments on commit 803159b

Please sign in to comment.