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