|
|
|
@ -30,47 +30,104 @@ public class AbpAutoMapperModule_Specific_ObjectMapper_Tests : AbpIntegratedTest |
|
|
|
[Fact] |
|
|
|
public void Specific_Object_Mapper_Should_Be_Used_For_Collections_If_Registered() |
|
|
|
{ |
|
|
|
// IEnumerable
|
|
|
|
_objectMapper.Map<IEnumerable<MyEntity>, IEnumerable<MyEntityDto2>>(new List<MyEntity>() |
|
|
|
{ |
|
|
|
new MyEntity { Number = 42 } |
|
|
|
}).First().Number.ShouldBe(43); //MyEntityToMyEntityDto2Mapper adds 1 to number of the source.
|
|
|
|
|
|
|
|
var destination = new List<MyEntityDto2>() |
|
|
|
{ |
|
|
|
new MyEntityDto2 { Number = 44 } |
|
|
|
}; |
|
|
|
var returnIEnumerable = _objectMapper.Map<IEnumerable<MyEntity>, IEnumerable<MyEntityDto2>>( |
|
|
|
new List<MyEntity>() |
|
|
|
{ |
|
|
|
new MyEntity { Number = 42 } |
|
|
|
}, destination); |
|
|
|
returnIEnumerable.First().Number.ShouldBe(43); |
|
|
|
ReferenceEquals(destination, returnIEnumerable).ShouldBeTrue(); |
|
|
|
|
|
|
|
// ICollection
|
|
|
|
_objectMapper.Map<ICollection<MyEntity>, ICollection<MyEntityDto2>>(new List<MyEntity>() |
|
|
|
{ |
|
|
|
new MyEntity { Number = 42 } |
|
|
|
}).First().Number.ShouldBe(43); //MyEntityToMyEntityDto2Mapper adds 1 to number of the source.
|
|
|
|
|
|
|
|
var returnICollection = _objectMapper.Map<ICollection<MyEntity>, ICollection<MyEntityDto2>>( |
|
|
|
new List<MyEntity>() |
|
|
|
{ |
|
|
|
new MyEntity { Number = 42 } |
|
|
|
}, destination); |
|
|
|
returnICollection.First().Number.ShouldBe(43); |
|
|
|
ReferenceEquals(destination, returnICollection).ShouldBeTrue(); |
|
|
|
|
|
|
|
// Collection
|
|
|
|
_objectMapper.Map<Collection<MyEntity>, Collection<MyEntityDto2>>(new Collection<MyEntity>() |
|
|
|
{ |
|
|
|
new MyEntity { Number = 42 } |
|
|
|
}, new Collection<MyEntityDto2>() //When mapping to an existing collection, the destination collection is cleared first
|
|
|
|
}).First().Number.ShouldBe(43); //MyEntityToMyEntityDto2Mapper adds 1 to number of the source.
|
|
|
|
|
|
|
|
var destination2 = new Collection<MyEntityDto2>() |
|
|
|
{ |
|
|
|
new MyEntityDto2 { Number = 44 } |
|
|
|
}).First().Number.ShouldBe(43); //MyEntityToMyEntityDto2Mapper adds 1 to number of the source.
|
|
|
|
}; |
|
|
|
var returnCollection = _objectMapper.Map<Collection<MyEntity>, Collection<MyEntityDto2>>( |
|
|
|
new Collection<MyEntity>() |
|
|
|
{ |
|
|
|
new MyEntity { Number = 42 } |
|
|
|
}, destination2); |
|
|
|
returnCollection.First().Number.ShouldBe(43); |
|
|
|
ReferenceEquals(destination2, returnCollection).ShouldBeTrue(); |
|
|
|
|
|
|
|
// IList
|
|
|
|
_objectMapper.Map<IList<MyEntity>, IList<MyEntityDto2>>(new List<MyEntity>() |
|
|
|
{ |
|
|
|
new MyEntity { Number = 42 } |
|
|
|
}, new Collection<MyEntityDto2>() //When mapping to an existing collection, the destination collection is cleared first
|
|
|
|
{ |
|
|
|
new MyEntityDto2 { Number = 44 } |
|
|
|
}).First().Number.ShouldBe(43); //MyEntityToMyEntityDto2Mapper adds 1 to number of the source.
|
|
|
|
|
|
|
|
var returnIList = _objectMapper.Map<IList<MyEntity>, IList<MyEntityDto2>>( |
|
|
|
new List<MyEntity>() |
|
|
|
{ |
|
|
|
new MyEntity { Number = 42 } |
|
|
|
}, destination); |
|
|
|
returnIList.First().Number.ShouldBe(43); |
|
|
|
ReferenceEquals(destination, returnIList).ShouldBeTrue(); |
|
|
|
|
|
|
|
// List
|
|
|
|
_objectMapper.Map<List<MyEntity>, List<MyEntityDto2>>(new List<MyEntity>() |
|
|
|
{ |
|
|
|
new MyEntity { Number = 42 } |
|
|
|
}, new List<MyEntityDto2>() //When mapping to an existing collection, the destination collection is cleared first
|
|
|
|
{ |
|
|
|
new MyEntityDto2 { Number = 44 } |
|
|
|
}).First().Number.ShouldBe(43); //MyEntityToMyEntityDto2Mapper adds 1 to number of the source.
|
|
|
|
|
|
|
|
var returnList = _objectMapper.Map<List<MyEntity>, List<MyEntityDto2>>( |
|
|
|
new List<MyEntity>() |
|
|
|
{ |
|
|
|
new MyEntity { Number = 42 } |
|
|
|
}, destination); |
|
|
|
returnList.First().Number.ShouldBe(43); |
|
|
|
ReferenceEquals(destination, returnList).ShouldBeTrue(); |
|
|
|
|
|
|
|
// Array
|
|
|
|
_objectMapper.Map<MyEntity[], MyEntityDto2[]>(new MyEntity[] |
|
|
|
{ |
|
|
|
new MyEntity { Number = 42 } |
|
|
|
}, new MyEntityDto2[] //When mapping to an existing collection, the destination collection is cleared first
|
|
|
|
{ |
|
|
|
new MyEntityDto2 { Number = 44 } |
|
|
|
}).First().Number.ShouldBe(43); //MyEntityToMyEntityDto2Mapper adds 1 to number of the source.
|
|
|
|
|
|
|
|
var destinationArray = new MyEntityDto2[] |
|
|
|
{ |
|
|
|
new MyEntityDto2 { Number = 40 } |
|
|
|
}; |
|
|
|
var returnArray = _objectMapper.Map<MyEntity[], MyEntityDto2[]>(new MyEntity[] |
|
|
|
{ |
|
|
|
new MyEntity { Number = 42 } |
|
|
|
}, destinationArray); |
|
|
|
|
|
|
|
returnArray.First().Number.ShouldBe(43); |
|
|
|
|
|
|
|
// array should not be changed. Same as AutoMapper.
|
|
|
|
destinationArray.First().Number.ShouldBe(40); |
|
|
|
ReferenceEquals(returnArray, destinationArray).ShouldBeFalse(); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
|