|
| 1 | +using Shouldly; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Linq.Expressions; |
| 6 | +using System.Text; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace AutoMapper.Extensions.ExpressionMapping.UnitTests |
| 10 | +{ |
| 11 | + public class ExpressionMappingPropertyFromDerviedType : AutoMapperSpecBase |
| 12 | + { |
| 13 | + private List<BaseEntity> _source; |
| 14 | + private IQueryable<BaseEntity> entityQuery; |
| 15 | + |
| 16 | + public class BaseDTO |
| 17 | + { |
| 18 | + public Guid Id { get; set; } |
| 19 | + } |
| 20 | + |
| 21 | + public class BaseEntity |
| 22 | + { |
| 23 | + public Guid Id { get; set; } |
| 24 | + } |
| 25 | + |
| 26 | + public class DTO : BaseDTO |
| 27 | + { |
| 28 | + public string Name { get; set; } |
| 29 | + public string Description { get; set; } |
| 30 | + } |
| 31 | + |
| 32 | + public class Entity : BaseEntity |
| 33 | + { |
| 34 | + public string Name { get; set; } |
| 35 | + } |
| 36 | + |
| 37 | + protected override MapperConfiguration Configuration |
| 38 | + { |
| 39 | + get |
| 40 | + { |
| 41 | + var config = new MapperConfiguration(cfg => |
| 42 | + { |
| 43 | + cfg.AddExpressionMapping(); |
| 44 | + |
| 45 | + cfg.CreateMap<BaseEntity, BaseDTO>(); |
| 46 | + cfg.CreateMap<BaseDTO, BaseEntity>(); |
| 47 | + |
| 48 | + cfg.CreateMap<Entity, DTO>() |
| 49 | + .ForMember(dest => dest.Description, opts => opts.MapFrom(src => string.Concat(src.Id.ToString(), " - ", src.Name))) |
| 50 | + .IncludeBase<BaseEntity, BaseDTO>(); |
| 51 | + cfg.CreateMap<DTO, Entity>() |
| 52 | + .IncludeBase<BaseDTO, BaseEntity>(); |
| 53 | + }); |
| 54 | + return config; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + protected override void Because_of() |
| 59 | + { |
| 60 | + //Arrange |
| 61 | + _source = new List<BaseEntity> { |
| 62 | + new Entity { Id = Guid.NewGuid(), Name = "Sofia" }, |
| 63 | + new Entity { Id = Guid.NewGuid(), Name = "Rafael" }, |
| 64 | + new BaseEntity { Id = Guid.NewGuid() } |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void Should_support_propertypath_expressions_with_properties_from_sub_types_using_explicit_cast() |
| 70 | + { |
| 71 | + // Act |
| 72 | + Expression<Func<BaseDTO, bool>> dtoQueryExpression = r => (r is DTO ? ((DTO)r).Name : "") == "Sofia"; |
| 73 | + Expression<Func<BaseEntity, bool>> entityQueryExpression = Mapper.MapExpression<Expression<Func<BaseEntity, bool>>>(dtoQueryExpression); |
| 74 | + entityQuery = _source.AsQueryable().Where(entityQueryExpression); |
| 75 | + |
| 76 | + // Assert |
| 77 | + entityQuery.ToList().Count().ShouldBe(1); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void Should_support_propertypath_expressions_with_properties_from_sub_types_using_as_keyword() |
| 82 | + { |
| 83 | + // Act |
| 84 | + Expression<Func<BaseDTO, bool>> dtoQueryExpression = r => (r is DTO ? (r as DTO).Name : "") == "Sofia"; |
| 85 | + Expression<Func<BaseEntity, bool>> entityQueryExpression = Mapper.MapExpression<Expression<Func<BaseEntity, bool>>>(dtoQueryExpression); |
| 86 | + entityQuery = _source.AsQueryable().Where(entityQueryExpression); |
| 87 | + |
| 88 | + // Assert |
| 89 | + entityQuery.ToList().Count().ShouldBe(1); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments