Skip to content

Commit d85adef

Browse files
committed
Stay with the existing design of using configured data to model maps to retrieve the type mappings.
1 parent 6600b12 commit d85adef

File tree

4 files changed

+57
-51
lines changed

4 files changed

+57
-51
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<Authors>Jimmy Bogard</Authors>
44
<LangVersion>latest</LangVersion>
5-
<VersionPrefix>3.1.0-preview02</VersionPrefix>
5+
<VersionPrefix>3.1.0-preview03</VersionPrefix>
66
<WarningsAsErrors>true</WarningsAsErrors>
77
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
88
</PropertyGroup>

src/AutoMapper.Extensions.ExpressionMapping/MapperExtensions.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,22 @@ public static Dictionary<Type, Type> AddTypeMapping(this Dictionary<Type, Type>
279279
return typeMappings;
280280
}
281281

282-
private static void AddIncludedTypeMaps(this Dictionary<Type, Type> typeMappings, IConfigurationProvider configurationProvider, Type source, Type dest)
282+
private static void AddIncludedTypeMaps(this Dictionary<Type, Type> typeMappings, IConfigurationProvider configurationProvider, Type source/*model*/, Type dest/*data*/)//model to date
283283
{
284-
AddTypeMaps(configurationProvider.ResolveTypeMap(source, dest));
284+
//Stay with the existing design of using configured data to model maps to retrieve the type mappings.
285+
//This is needed for property map custom expressions.
286+
AddTypeMaps(configurationProvider.ResolveTypeMap(sourceType: dest/*data*/, destinationType: source/*model*/));
285287

286288
void AddTypeMaps(TypeMap typeMap)
287289
{
288290
if (typeMap == null)
289291
return;
290292

291-
foreach (var includedBase in typeMap.IncludedBaseTypes)
292-
typeMappings.AddTypeMapping(configurationProvider, includedBase.SourceType, includedBase.DestinationType);
293+
foreach (TypePair baseTypePair in typeMap.IncludedBaseTypes)
294+
typeMappings.AddTypeMapping(configurationProvider, baseTypePair.DestinationType/*model*/, baseTypePair.SourceType/*data*/);
293295

294-
foreach (var includedDerived in typeMap.IncludedDerivedTypes)
295-
typeMappings.AddTypeMapping(configurationProvider, includedDerived.SourceType, includedDerived.DestinationType);
296+
foreach (TypePair derivedTypePair in typeMap.IncludedDerivedTypes)
297+
typeMappings.AddTypeMapping(configurationProvider, derivedTypePair.DestinationType/*model*/, derivedTypePair.SourceType/*data*/);
296298
}
297299
}
298300

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/ExpressionMappingPropertyFromDerviedType.cs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,14 @@ public class ExpressionMappingPropertyFromDerviedType : AutoMapperSpecBase
1313
private List<BaseEntity> _source;
1414
private IQueryable<BaseEntity> entityQuery;
1515

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-
3716
protected override MapperConfiguration Configuration
3817
{
3918
get
4019
{
4120
var config = new MapperConfiguration(cfg =>
4221
{
4322
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>();
23+
cfg.AddProfile(typeof(DerivedTypeProfile));
5324
});
5425
return config;
5526
}
@@ -88,5 +59,38 @@ public void Should_support_propertypath_expressions_with_properties_from_sub_typ
8859
// Assert
8960
entityQuery.ToList().Count().ShouldBe(1);
9061
}
62+
63+
public class DerivedTypeProfile : Profile
64+
{
65+
public DerivedTypeProfile()
66+
{
67+
CreateMap<BaseEntity, BaseDTO>();
68+
69+
CreateMap<Entity, DTO>()
70+
.ForMember(dest => dest.Description, opts => opts.MapFrom(src => string.Concat(src.Id.ToString(), " - ", src.Name)))
71+
.IncludeBase<BaseEntity, BaseDTO>();
72+
}
73+
}
74+
75+
public class BaseDTO
76+
{
77+
public Guid Id { get; set; }
78+
}
79+
80+
public class BaseEntity
81+
{
82+
public Guid Id { get; set; }
83+
}
84+
85+
public class DTO : BaseDTO
86+
{
87+
public string Name { get; set; }
88+
public string Description { get; set; }
89+
}
90+
91+
public class Entity : BaseEntity
92+
{
93+
public string Name { get; set; }
94+
}
9195
}
9296
}

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapper.ForPath.Tests.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public void Works_for_inherited_properties()
2222
Expression<Func<DerivedModel, bool>> selection = s => s.Nested.NestedTitle2 == "nested test";
2323

2424
//Act
25-
Expression<Func<DerivedDataModel, bool>> selectionMapped = mapper.Map<Expression<Func<DerivedDataModel, bool>>>(selection);
26-
List<DerivedDataModel> items = DataObjects.Where(selectionMapped).ToList();
25+
Expression<Func<DerivedData, bool>> selectionMapped = mapper.Map<Expression<Func<DerivedData, bool>>>(selection);
26+
List<DerivedData> items = DataObjects.Where(selectionMapped).ToList();
2727

2828
//Assert
2929
Assert.True(items.Count == 1);
@@ -36,8 +36,8 @@ public void Works_for_inherited_properties_on_base_types()
3636
Expression<Func<RootModel, bool>> selection = s => ((DerivedModel)s).Nested.NestedTitle2 == "nested test";
3737

3838
//Act
39-
Expression<Func<DataModel, bool>> selectionMapped = mapper.MapExpression<Expression<Func<DataModel, bool>>>(selection);
40-
List<DataModel> items = DataObjects.Where(selectionMapped).ToList();
39+
Expression<Func<RootData, bool>> selectionMapped = mapper.MapExpression<Expression<Func<RootData, bool>>>(selection);
40+
List<RootData> items = DataObjects.Where(selectionMapped).ToList();
4141

4242
//Assert
4343
Assert.True(items.Count == 1);
@@ -122,11 +122,11 @@ public void Throws_exception_when_mapped_string_is_a_child_of_the_parameter()
122122

123123
private void SetupQueryableCollection()
124124
{
125-
DataObjects = new DerivedDataModel[]
125+
DataObjects = new DerivedData[]
126126
{
127-
new DerivedDataModel() { OtherID = 2, Title2 = "nested test", ID = 1, Title = "test", DescendantField = "descendant field" },
128-
new DerivedDataModel() { OtherID = 3, Title2 = "nested", ID = 4, Title = "title", DescendantField = "some text" }
129-
}.AsQueryable<DerivedDataModel>();
127+
new DerivedData() { OtherID = 2, Title2 = "nested test", ID = 1, Title = "test", DescendantField = "descendant field" },
128+
new DerivedData() { OtherID = 3, Title2 = "nested", ID = 4, Title = "title", DescendantField = "some text" }
129+
}.AsQueryable<DerivedData>();
130130

131131
Orders = new OrderDto[]
132132
{
@@ -146,7 +146,7 @@ private void SetupQueryableCollection()
146146
}
147147

148148
private static IQueryable<OrderDto> Orders { get; set; }
149-
private static IQueryable<DerivedDataModel> DataObjects { get; set; }
149+
private static IQueryable<DerivedData> DataObjects { get; set; }
150150

151151
private void SetupAutoMapper()
152152
{
@@ -181,7 +181,7 @@ public class DerivedModel : RootModel
181181
public string DescendantField { get; set; }
182182
}
183183

184-
public class DataModel
184+
public class RootData
185185
{
186186
public int ID { get; set; }
187187
public string Title { get; set; }
@@ -190,7 +190,7 @@ public class DataModel
190190
public string Title2 { get; set; }
191191
}
192192

193-
public class DerivedDataModel : DataModel
193+
public class DerivedData : RootData
194194
{
195195
public string DescendantField { get; set; }
196196
}
@@ -231,10 +231,10 @@ public class ForPathCustomerProfile : Profile
231231
{
232232
public ForPathCustomerProfile()
233233
{
234-
CreateMap<RootModel, DataModel>()
235-
.Include<DerivedModel, DerivedDataModel>();
234+
CreateMap<RootData, RootModel>()
235+
.Include<DerivedData, DerivedModel>();
236236

237-
CreateMap<DerivedDataModel, DerivedModel>()
237+
CreateMap<DerivedData, DerivedModel>()
238238
.ForPath(d => d.Nested.NestedTitle, opt => opt.MapFrom(src => src.Title))
239239
.ForPath(d => d.Nested.NestedTitle2, opt => opt.MapFrom(src => src.Title2))
240240
.ReverseMap();

0 commit comments

Comments
 (0)