|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using AutoMapper.EquivalencyExpression; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace AutoMapper.Collection |
| 10 | +{ |
| 11 | + public class ValueTypeTests |
| 12 | + { |
| 13 | + [Fact] |
| 14 | + public void MapValueTypes() |
| 15 | + { |
| 16 | + Mapper.Reset(); |
| 17 | + Mapper.Initialize(c => |
| 18 | + { |
| 19 | + c.AddCollectionMappers(); |
| 20 | + |
| 21 | + c.CreateMap<Country, CountryDto>() |
| 22 | + .ForMember(x => x.Nationalities, m => m.MapFrom(x => x.Persons)) |
| 23 | + .ReverseMap(); |
| 24 | + |
| 25 | + c.CreateMap<int, PersonNationality>() |
| 26 | + .EqualityComparison((src, dest) => dest.NationalityCountryId == src); |
| 27 | + }); |
| 28 | + |
| 29 | + var persons = new[] |
| 30 | + { |
| 31 | + new PersonNationality{PersonId = 1, NationalityCountryId = 101}, |
| 32 | + new PersonNationality{PersonId = 2, NationalityCountryId = 102}, |
| 33 | + new PersonNationality{PersonId = 3, NationalityCountryId = 103}, |
| 34 | + new PersonNationality{PersonId = 4, NationalityCountryId = 104}, |
| 35 | + }; |
| 36 | + |
| 37 | + var country = new Country { Persons = new List<PersonNationality>(persons) }; |
| 38 | + var countryDto = new CountryDto { Nationalities = new List<int> { 104, 103, 105 } }; |
| 39 | + |
| 40 | + Mapper.Map(countryDto, country); |
| 41 | + |
| 42 | + Assert.NotStrictEqual(new[] { persons[3], persons[2], country.Persons.Last() }, country.Persons); |
| 43 | + Assert.Equal(0, country.Persons.Last().PersonId); |
| 44 | + } |
| 45 | + |
| 46 | + public class PersonNationality |
| 47 | + { |
| 48 | + public int PersonId { get; set; } |
| 49 | + public int NationalityCountryId { get; set; } |
| 50 | + } |
| 51 | + |
| 52 | + public class Country |
| 53 | + { |
| 54 | + public IList<PersonNationality> Persons { get; set; } |
| 55 | + } |
| 56 | + |
| 57 | + public class CountryDto |
| 58 | + { |
| 59 | + public IList<int> Nationalities { get; set; } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments