mirror of https://github.com/abpframework/abp.git
14 changed files with 30 additions and 407 deletions
@ -1,29 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using AutoMapper; |
|||
|
|||
namespace Volo.Abp.AutoMapper |
|||
{ |
|||
public class AbpAutoMapAttribute : AbpAutoMapAttributeBase |
|||
{ |
|||
public AbpAutoMapAttribute(params Type[] targetTypes) |
|||
: base(targetTypes) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public override void CreateMap(IMapperConfigurationExpression configuration, Type type) |
|||
{ |
|||
if (TargetTypes.IsNullOrEmpty()) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
foreach (var targetType in TargetTypes) |
|||
{ |
|||
configuration.CreateMap(type, targetType, MemberList.Source); |
|||
configuration.CreateMap(targetType, type, MemberList.Destination); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,17 +0,0 @@ |
|||
using System; |
|||
using AutoMapper; |
|||
|
|||
namespace Volo.Abp.AutoMapper |
|||
{ |
|||
public abstract class AbpAutoMapAttributeBase : Attribute |
|||
{ |
|||
public Type[] TargetTypes { get; } |
|||
|
|||
protected AbpAutoMapAttributeBase(params Type[] targetTypes) |
|||
{ |
|||
TargetTypes = targetTypes; |
|||
} |
|||
|
|||
public abstract void CreateMap(IMapperConfigurationExpression configuration, Type type); |
|||
} |
|||
} |
|||
@ -1,36 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using AutoMapper; |
|||
|
|||
namespace Volo.Abp.AutoMapper |
|||
{ |
|||
public class AbpAutoMapFromAttribute : AbpAutoMapAttributeBase |
|||
{ |
|||
public MemberList MemberList { get; set; } = MemberList.Destination; |
|||
|
|||
public AbpAutoMapFromAttribute(params Type[] targetTypes) |
|||
: base(targetTypes) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public AbpAutoMapFromAttribute(MemberList memberList, params Type[] targetTypes) |
|||
: this(targetTypes) |
|||
{ |
|||
MemberList = memberList; |
|||
} |
|||
|
|||
public override void CreateMap(IMapperConfigurationExpression configuration, Type type) |
|||
{ |
|||
if (TargetTypes.IsNullOrEmpty()) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
foreach (var targetType in TargetTypes) |
|||
{ |
|||
configuration.CreateMap(targetType, type, MemberList); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,36 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using AutoMapper; |
|||
|
|||
namespace Volo.Abp.AutoMapper |
|||
{ |
|||
public class AbpAutoMapToAttribute : AbpAutoMapAttributeBase |
|||
{ |
|||
public MemberList MemberList { get; set; } = MemberList.Source; |
|||
|
|||
public AbpAutoMapToAttribute(params Type[] targetTypes) |
|||
: base(targetTypes) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public AbpAutoMapToAttribute(MemberList memberList, params Type[] targetTypes) |
|||
: this(targetTypes) |
|||
{ |
|||
MemberList = memberList; |
|||
} |
|||
|
|||
public override void CreateMap(IMapperConfigurationExpression configuration, Type type) |
|||
{ |
|||
if (TargetTypes.IsNullOrEmpty()) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
foreach (var targetType in TargetTypes) |
|||
{ |
|||
configuration.CreateMap(type, targetType, MemberList); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,17 +0,0 @@ |
|||
using System; |
|||
using System.Reflection; |
|||
using AutoMapper; |
|||
|
|||
namespace Volo.Abp.AutoMapper |
|||
{ |
|||
internal static class AutoMapperConfigurationExtensions |
|||
{ |
|||
public static void CreateAutoAttributeMaps(this IMapperConfigurationExpression configuration, Type type) |
|||
{ |
|||
foreach (var autoMapAttribute in type.GetTypeInfo().GetCustomAttributes<AbpAutoMapAttributeBase>()) |
|||
{ |
|||
autoMapAttribute.CreateMap(configuration, type); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,75 +0,0 @@ |
|||
using AutoMapper; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AutoMapper |
|||
{ |
|||
public class AutoMapper_Inheritance_Tests |
|||
{ |
|||
private readonly IMapper _mapper; |
|||
|
|||
public AutoMapper_Inheritance_Tests() |
|||
{ |
|||
var config = new MapperConfiguration(configuration => |
|||
{ |
|||
configuration.CreateAutoAttributeMaps(typeof(MyTargetClassToMap)); |
|||
configuration.CreateAutoAttributeMaps(typeof(EntityDto)); |
|||
configuration.CreateAutoAttributeMaps(typeof(DerivedEntityDto)); |
|||
}); |
|||
|
|||
_mapper = config.CreateMapper(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Map_Derived_To_Target() |
|||
{ |
|||
var derived = new MyDerivedClass { Value = "fortytwo" }; |
|||
var target = _mapper.Map<MyTargetClassToMap>(derived); |
|||
target.Value.ShouldBe("fortytwo"); |
|||
} |
|||
|
|||
public class MyBaseClass |
|||
{ |
|||
public string Value { get; set; } |
|||
} |
|||
|
|||
public class MyDerivedClass : MyBaseClass |
|||
{ |
|||
|
|||
} |
|||
|
|||
[AbpAutoMapFrom(typeof(MyBaseClass))] |
|||
public class MyTargetClassToMap |
|||
{ |
|||
public string Value { get; set; } |
|||
} |
|||
|
|||
//[Fact] //TODO: That's a problem but related to AutoMapper rather than ABP.
|
|||
public void Should_Map_EntityProxy_To_EntityDto_And_To_DrivedEntityDto() |
|||
{ |
|||
var proxy = new EntityProxy() { Value = "42"}; |
|||
var target = _mapper.Map<EntityDto>(proxy); |
|||
var target2 = _mapper.Map<DerivedEntityDto>(proxy); |
|||
target.Value.ShouldBe("42"); |
|||
target2.Value.ShouldBe("42"); |
|||
} |
|||
|
|||
private class Entity |
|||
{ |
|||
public string Value { get; set; } |
|||
} |
|||
|
|||
private class DerivedEntity : Entity { } |
|||
|
|||
private class EntityProxy : DerivedEntity { } |
|||
|
|||
[AbpAutoMapFrom(typeof(Entity))] |
|||
private class EntityDto |
|||
{ |
|||
public string Value { get; set; } |
|||
} |
|||
|
|||
[AbpAutoMapFrom(typeof(DerivedEntity))] |
|||
private class DerivedEntityDto : EntityDto { } |
|||
} |
|||
} |
|||
@ -1,157 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using AutoMapper; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AutoMapper |
|||
{ |
|||
public class AutoMapping_Tests |
|||
{ |
|||
private readonly IMapper _mapper; |
|||
|
|||
public AutoMapping_Tests() |
|||
{ |
|||
var config = new MapperConfiguration(configuration => |
|||
{ |
|||
configuration.CreateAutoAttributeMaps(typeof(MyClass1)); |
|||
configuration.CreateAutoAttributeMaps(typeof(MyClass2)); |
|||
}); |
|||
|
|||
_mapper = config.CreateMapper(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Map_Null_Tests() |
|||
{ |
|||
MyClass1 obj1 = null; |
|||
var obj2 = _mapper.Map<MyClass2>(obj1); |
|||
obj2.ShouldBe(null); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Map_Null_Existing_Object_Tests() |
|||
{ |
|||
MyClass1 obj1 = null; |
|||
|
|||
var obj2 = new MyClass2 { TestProp = "before map" }; |
|||
_mapper.Map(obj1, obj2); |
|||
obj2.TestProp.ShouldBe("before map"); |
|||
} |
|||
|
|||
[Fact] |
|||
public void MapTo_Tests() |
|||
{ |
|||
var obj1 = new MyClass1 { TestProp = "Test value" }; |
|||
|
|||
var obj2 = _mapper.Map<MyClass2>(obj1); |
|||
obj2.TestProp.ShouldBe("Test value"); |
|||
|
|||
var obj3 = _mapper.Map<MyClass3>(obj1); |
|||
obj3.TestProp.ShouldBe("Test value"); |
|||
} |
|||
|
|||
[Fact] |
|||
public void MapTo_Existing_Object_Tests() |
|||
{ |
|||
var obj1 = new MyClass1 { TestProp = "Test value" }; |
|||
|
|||
var obj2 = new MyClass2(); |
|||
_mapper.Map(obj1, obj2); |
|||
obj2.TestProp.ShouldBe("Test value"); |
|||
|
|||
var obj3 = new MyClass3(); |
|||
_mapper.Map(obj2, obj3); |
|||
obj3.TestProp.ShouldBe("Test value"); |
|||
|
|||
Assert.ThrowsAny<Exception>(() => //Did not define reverse mapping!
|
|||
{ |
|||
_mapper.Map(obj3, obj2); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public void MapFrom_Tests() |
|||
{ |
|||
var obj2 = new MyClass2 { TestProp = "Test value" }; |
|||
|
|||
var obj1 = _mapper.Map<MyClass1>(obj2); |
|||
obj1.TestProp.ShouldBe("Test value"); |
|||
} |
|||
|
|||
[Fact] |
|||
public void IgnoreMap_Tests() |
|||
{ |
|||
var obj2 = new MyClass2 {TestProp = "Test value", AnotherValue = 42}; |
|||
var obj3 = _mapper.Map<MyClass3>(obj2); |
|||
obj3.TestProp.ShouldBe("Test value"); |
|||
obj3.AnotherValue.ShouldBe(0); //Ignored because of IgnoreMap attribute!
|
|||
} |
|||
|
|||
[Fact] |
|||
public void MapTo_Collection_Tests() |
|||
{ |
|||
var list1 = new List<MyClass1> |
|||
{ |
|||
new MyClass1 {TestProp = "Test value 1"}, |
|||
new MyClass1 {TestProp = "Test value 2"} |
|||
}; |
|||
|
|||
var list2 = _mapper.Map<List<MyClass2>>(list1); |
|||
list2.Count.ShouldBe(2); |
|||
list2[0].TestProp.ShouldBe("Test value 1"); |
|||
list2[1].TestProp.ShouldBe("Test value 2"); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Map_Should_Set_Null_Existing_Object_Tests() |
|||
{ |
|||
MyClass1 obj1 = new MyClass1 { TestProp = null }; |
|||
var obj2 = new MyClass2 { TestProp = "before map" }; |
|||
_mapper.Map(obj1, obj2); |
|||
obj2.TestProp.ShouldBe(null); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Map_Nullable_Value_To_Null_If_It_Is_Null_On_Source() |
|||
{ |
|||
var obj1 = new MyClass1(); |
|||
var obj2 = _mapper.Map<MyClass2>(obj1); |
|||
obj2.NullableValue.ShouldBeNull(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Map_Nullable_Value_To__Not_Null_If_It_Is__Not_Null_On_Source() |
|||
{ |
|||
var obj1 = new MyClass1 { NullableValue = 42 }; |
|||
var obj2 = _mapper.Map<MyClass2>(obj1); |
|||
obj2.NullableValue.ShouldBe(42); |
|||
} |
|||
|
|||
[AbpAutoMap(typeof(MyClass2), typeof(MyClass3))] |
|||
private class MyClass1 |
|||
{ |
|||
public string TestProp { get; set; } |
|||
|
|||
public long? NullableValue { get; set; } |
|||
} |
|||
|
|||
[AbpAutoMapTo(typeof(MyClass3))] |
|||
private class MyClass2 |
|||
{ |
|||
public string TestProp { get; set; } |
|||
|
|||
public long? NullableValue { get; set; } |
|||
|
|||
public int AnotherValue { get; set; } |
|||
} |
|||
|
|||
private class MyClass3 |
|||
{ |
|||
public string TestProp { get; set; } |
|||
|
|||
[IgnoreMap] |
|||
public int AnotherValue { get; set; } |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using AutoMapper; |
|||
|
|||
namespace Volo.Abp.AutoMapper.SampleClasses |
|||
{ |
|||
public class MyMapProfile : Profile |
|||
{ |
|||
public MyMapProfile() |
|||
{ |
|||
CreateMap<MyEntity, MyEntityDto>().ReverseMap(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using AutoMapper; |
|||
|
|||
namespace Volo.Abp.TestApp.Domain |
|||
{ |
|||
public class TestAutoMapProfile : Profile |
|||
{ |
|||
public TestAutoMapProfile() |
|||
{ |
|||
CreateMap<PersonEto, Person>().ReverseMap(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue