Browse Source

Enhance Mapperly mapping and update related tests

pull/23277/head
maliming 11 months ago
parent
commit
1f95fb511e
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 6
      framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/AbpMapperlyConventionalRegistrar.cs
  2. 2
      framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/IAbpMapperly.cs
  3. 18
      framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/MapperlyAutoObjectMappingProvider.cs
  4. 24
      framework/test/Volo.Abp.Mapperly.Tests/Volo/Abp/Mapperly/AbpMapperlyModule_Basic_Tests.cs
  5. 4
      framework/test/Volo.Abp.Mapperly.Tests/Volo/Abp/Mapperly/AbpMapperlyModule_Specific_ObjectMapper_Tests.cs
  6. 32
      framework/test/Volo.Abp.Mapperly.Tests/Volo/Abp/Mapperly/ObjectMapperExtensions_Tests.cs

6
framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/AbpMapperlyConventionalRegistrar.cs

@ -16,9 +16,9 @@ public class AbpMapperlyConventionalRegistrar : DefaultConventionalRegistrar
protected override List<Type> GetExposedServiceTypes(Type type)
{
var exposedServiceTypes = base.GetExposedServiceTypes(type);
var mapperlyInterfaces = type.GetInterfaces()
.Where(x => x.IsGenericType && typeof(IAbpMapperly<,>) == x.GetGenericTypeDefinition() ||
x.IsGenericType && typeof(IAbpReverseMapperly<,>) == x.GetGenericTypeDefinition()).ToList();
var mapperlyInterfaces = type.GetInterfaces().Where(x =>
x.IsGenericType && (typeof(IAbpMapperly<,>) == x.GetGenericTypeDefinition() ||
typeof(IAbpReverseMapperly<,>) == x.GetGenericTypeDefinition()));
return exposedServiceTypes
.Union(mapperlyInterfaces)
.Distinct()

2
framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/IAbpMapperly.cs

@ -1,6 +1,6 @@
namespace Volo.Abp.Mapperly;
public interface IAbpMapperly<TSource, TDestination>
public interface IAbpMapperly<in TSource, TDestination>
{
TDestination Map(TSource source);

18
framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/MapperlyAutoObjectMappingProvider.cs

@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Data;
using Volo.Abp.ObjectExtending;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Reflection;
namespace Volo.Abp.Mapperly;
@ -47,7 +48,8 @@ public class MapperlyAutoObjectMappingProvider : IAutoObjectMappingProvider
return destination;
}
throw new AbpException($"No {nameof(IAbpMapperly<TSource, TDestination>)} mapper found for {typeof(TSource).FullName} to {typeof(TDestination).FullName}");
throw new AbpException($"No {TypeHelper.GetFullNameHandlingNullableAndGenerics(typeof(IAbpMapperly<TSource, TDestination>))} or" +
$" {TypeHelper.GetFullNameHandlingNullableAndGenerics(typeof(IAbpReverseMapperly<TSource, TDestination>))} was found");
}
public virtual TDestination Map<TSource, TDestination>(TSource source, TDestination destination)
@ -63,7 +65,19 @@ public class MapperlyAutoObjectMappingProvider : IAutoObjectMappingProvider
return destination;
}
throw new AbpException($"No {nameof(IAbpMapperly<TSource, TDestination>)} mapper found for {typeof(TSource).FullName} to {typeof(TDestination).FullName}");
var reverseMapper = ServiceProvider.GetService<IAbpReverseMapperly<TSource, TDestination>>();
if (reverseMapper != null)
{
reverseMapper.BeforeReverseMap(source);
var destinationExtraProperties = GetExtraProperties(destination);
reverseMapper.ReverseMap(source, destination);
TryMapExtraProperties(reverseMapper.As<IAbpReverseMapperly<TDestination, TSource>>(), source, destination, destinationExtraProperties);
reverseMapper.AfterReverseMap(source, destination);
return destination;
}
throw new AbpException($"No {TypeHelper.GetFullNameHandlingNullableAndGenerics(typeof(IAbpMapperly<TSource, TDestination>))} or" +
$" {TypeHelper.GetFullNameHandlingNullableAndGenerics(typeof(IAbpReverseMapperly<TSource, TDestination>))} was found");
}
protected virtual ExtraPropertyDictionary GetExtraProperties<TDestination>(TDestination destination)

24
framework/test/Volo.Abp.Mapperly.Tests/Volo/Abp/Mapperly/AbpMapperlyModule_Basic_Tests.cs

@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Mapperly.SampleClasses;
using Volo.Abp.ObjectMapping;
@ -29,10 +30,31 @@ public class AbpMapperlyModule_Basic_Tests : AbpIntegratedTest<MapperlyTestModul
dto.Number.ShouldBe(42);
}
[Fact]
public void Should_Map_Objects_With_Existing_Target_Object()
{
var dto = new MyEntityDto {Id = Guid.Empty, Number = 42};
_objectMapper.Map<MyEntity, MyEntityDto>(new MyEntity { Id = Guid.NewGuid(), Number = 43 }, dto);
dto.Number.ShouldBe(43);
dto.Id.ShouldNotBe(Guid.Empty);
}
[Fact]
public void Should_Map_Enum()
{
var dto = _objectMapper.Map<MyEnum, MyEnumDto>(MyEnum.Value3);
dto.ShouldBe(MyEnumDto.Value2); //Value2 is same as Value3
}
[Fact]
public void Should_Throw_Exception_If_Mapper_Is_Not_Found()
{
var exception = Assert.Throws<AbpException>(() =>_objectMapper.Map<MyEntity, MyClassDto>(new MyEntity()));
exception.Message.ShouldBe("No " +
"Volo.Abp.Mapperly.IAbpMapperly<Volo.Abp.Mapperly.SampleClasses.MyEntity,Volo.Abp.Mapperly.MyClassDto> or " +
"Volo.Abp.Mapperly.IAbpReverseMapperly<Volo.Abp.Mapperly.SampleClasses.MyEntity,Volo.Abp.Mapperly.MyClassDto>" +
" was found");
}
}

4
framework/test/Volo.Abp.Mapperly.Tests/Volo/Abp/Mapperly/AbpAutoMapperModule_Specific_ObjectMapper_Tests.cs → framework/test/Volo.Abp.Mapperly.Tests/Volo/Abp/Mapperly/AbpMapperlyModule_Specific_ObjectMapper_Tests.cs

@ -11,11 +11,11 @@ using Xunit;
namespace Volo.Abp.Mapperly;
public class AbpAutoMapperModule_Specific_ObjectMapper_Tests : AbpIntegratedTest<MapperlyTestModule>
public class AbpMapperlyModule_Specific_ObjectMapper_Tests : AbpIntegratedTest<MapperlyTestModule>
{
private readonly IObjectMapper _objectMapper;
public AbpAutoMapperModule_Specific_ObjectMapper_Tests()
public AbpMapperlyModule_Specific_ObjectMapper_Tests()
{
_objectMapper = ServiceProvider.GetRequiredService<IObjectMapper>();
}

32
framework/test/Volo.Abp.Mapperly.Tests/Volo/Abp/Mapperly/ObjectMapperExtensions_Tests.cs

@ -1,32 +0,0 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Mapperly.SampleClasses;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Testing;
using Xunit;
namespace Volo.Abp.Mapperly;
public class ObjectMapperExtensions_Tests : AbpIntegratedTest<MapperlyTestModule>
{
private readonly IObjectMapper _objectMapper;
public ObjectMapperExtensions_Tests()
{
_objectMapper = ServiceProvider.GetRequiredService<IObjectMapper>();
}
[Fact]
public void Should_Map_Objects_With_AutoMap_Attributes()
{
var dto = _objectMapper.Map<MyEntity, MyEntityDto>(
new MyEntity
{
Number = 42
}
);
dto.As<MyEntityDto>().Number.ShouldBe(42);
}
}
Loading…
Cancel
Save