|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace AutoMapper.Extensions.ExpressionMapping.UnitTests |
| 8 | +{ |
| 9 | + public class MappingMemberInitWithCustomExpressions |
| 10 | + { |
| 11 | + [Fact] |
| 12 | + public void Map_member_init_using_custom_expressions() |
| 13 | + { |
| 14 | + //Arrange |
| 15 | + var config = GetConfiguration(); |
| 16 | + config.AssertConfigurationIsValid(); |
| 17 | + var mapper = config.CreateMapper(); |
| 18 | + Expression<Func<PlayerDto, PlayerDto>> selection = s => new PlayerDto |
| 19 | + { |
| 20 | + NameDto = s.NameDto, |
| 21 | + StatsADto = new StatsDto |
| 22 | + { |
| 23 | + SpeedValueDto = s.StatsADto.SpeedValueDto, |
| 24 | + PowerDto = s.StatsADto.PowerDto, |
| 25 | + RatingDto = s.StatsADto.RatingDto, |
| 26 | + StatsBuilderDto = new BuilderDto |
| 27 | + { |
| 28 | + IdDto = s.StatsADto.StatsBuilderDto.IdDto, |
| 29 | + CityDto = s.StatsADto.StatsBuilderDto.CityDto |
| 30 | + } |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + //Act |
| 35 | + Expression<Func<Player, Player>> selectionMapped = mapper.MapExpression<Expression<Func<Player, Player>>>(selection); |
| 36 | + List<Player> result = Players.Select(selectionMapped).ToList(); |
| 37 | + |
| 38 | + //Assert |
| 39 | + Assert.Equal("Jack", result[0].Name); |
| 40 | + Assert.Equal(1, result[0].StatsA.Power); |
| 41 | + Assert.Equal(2, result[0].StatsA.SpeedValue); |
| 42 | + Assert.Equal(5, result[0].StatsA.Rating); |
| 43 | + Assert.Equal(1, result[0].StatsA.StatsBuilder.Id); |
| 44 | + Assert.Equal("Atlanta", result[0].StatsA.StatsBuilder.City); |
| 45 | + } |
| 46 | + |
| 47 | + MapperConfiguration GetConfiguration() |
| 48 | + => new |
| 49 | + ( |
| 50 | + cfg => |
| 51 | + { |
| 52 | + cfg.AddExpressionMapping(); |
| 53 | + |
| 54 | + cfg.CreateMap<Player, PlayerDto>() |
| 55 | + .ForMember(dest => dest.NameDto, opt => opt.MapFrom(src => src.Name)) |
| 56 | + .ForMember(dest => dest.StatsADto, opt => opt.MapFrom(src => src.StatsA)); |
| 57 | + |
| 58 | + cfg.CreateMap<Stats, StatsDto>() |
| 59 | + .ForMember(dest => dest.SpeedValueDto, opt => opt.MapFrom(src => src.SpeedValue)) |
| 60 | + .ForMember(dest => dest.PowerDto, opt => opt.MapFrom(src => src.Power)) |
| 61 | + .ForMember(dest => dest.RatingDto, opt => opt.MapFrom(src => src.Rating)) |
| 62 | + .ForMember(dest => dest.StatsBuilderDto, opt => opt.MapFrom(src => src.StatsBuilder)); |
| 63 | + |
| 64 | + cfg.CreateMap<Builder, BuilderDto>() |
| 65 | + .ForMember(dest => dest.IdDto, opt => opt.MapFrom(src => src.Id)) |
| 66 | + .ForMember(dest => dest.CityDto, opt => opt.MapFrom(src => src.City)); |
| 67 | + } |
| 68 | + ); |
| 69 | + |
| 70 | + readonly IQueryable<Player> Players = new List<Player> |
| 71 | + { |
| 72 | + new Player |
| 73 | + { |
| 74 | + Name = "Jack", |
| 75 | + StatsA = new Stats |
| 76 | + { |
| 77 | + Power = 1, |
| 78 | + SpeedValue = 2, |
| 79 | + Rating = 5, |
| 80 | + StatsBuilder = new Builder |
| 81 | + { |
| 82 | + Id = 1, |
| 83 | + City = "Atlanta" |
| 84 | + } |
| 85 | + } |
| 86 | + }, |
| 87 | + new Player |
| 88 | + { |
| 89 | + Name = "Jane", |
| 90 | + StatsA = new Stats |
| 91 | + { |
| 92 | + Power = 1, |
| 93 | + SpeedValue = 3, |
| 94 | + Rating = 6, |
| 95 | + StatsBuilder = new Builder |
| 96 | + { |
| 97 | + Id = 2, |
| 98 | + City = "Charlotte" |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + }.AsQueryable(); |
| 103 | + |
| 104 | + public class Player |
| 105 | + { |
| 106 | + public string Name { get; set; } |
| 107 | + public Stats StatsA { get; set; } |
| 108 | + } |
| 109 | + |
| 110 | + public class Stats |
| 111 | + { |
| 112 | + public int SpeedValue { get; set; } |
| 113 | + public int Power { get; set; } |
| 114 | + public int Rating { get; set; } |
| 115 | + public Builder StatsBuilder { get; set; } |
| 116 | + } |
| 117 | + |
| 118 | + public class Builder |
| 119 | + { |
| 120 | + public int Id { get; set; } |
| 121 | + public string City { get; set; } |
| 122 | + } |
| 123 | + |
| 124 | + public class PlayerDto |
| 125 | + { |
| 126 | + public string NameDto { get; set; } |
| 127 | + public StatsDto StatsADto { get; set; } |
| 128 | + } |
| 129 | + |
| 130 | + public class StatsDto |
| 131 | + { |
| 132 | + public int SpeedValueDto { get; set; } |
| 133 | + public int PowerDto { get; set; } |
| 134 | + public int RatingDto { get; set; } |
| 135 | + public BuilderDto StatsBuilderDto { get; set; } |
| 136 | + } |
| 137 | + |
| 138 | + public class BuilderDto |
| 139 | + { |
| 140 | + public int IdDto { get; set; } |
| 141 | + public string CityDto { get; set; } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments