Skip to content

Commit 87ddbe7

Browse files
authored
Merge pull request #20 from Tasteful/feature/upgrade-automapper8
Upgrade to support AutoMapper 8
2 parents fb2f400 + 9408f3e commit 87ddbe7

File tree

6 files changed

+35
-36
lines changed

6 files changed

+35
-36
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>1.0.5</VersionPrefix>
5+
<VersionPrefix>2.0.0</VersionPrefix>
66
<WarningsAsErrors>true</WarningsAsErrors>
77
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
88
</PropertyGroup>

src/AutoMapper.Extensions.ExpressionMapping/AutoMapper.Extensions.ExpressionMapping.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<Summary>Expression mapping (OData) extensions for AutoMapper</Summary>
55
<Description>Expression mapping (OData) extensions for AutoMapper</Description>
6-
<TargetFrameworks>netstandard2.0;netstandard1.3;net45</TargetFrameworks>
6+
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<AssemblyOriginatorKeyFile>..\..\AutoMapper.snk</AssemblyOriginatorKeyFile>
99
<SignAssembly>true</SignAssembly>
@@ -17,7 +17,7 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="AutoMapper" Version="7.0.1" />
20+
<PackageReference Include="AutoMapper" Version="8.0.0" />
2121
</ItemGroup>
2222

2323
</Project>

src/AutoMapper.Extensions.ExpressionMapping/ExpressionMapper.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public bool IsMatch(TypePair context) => typeof(LambdaExpression).IsAssignableFr
2121
&& typeof(LambdaExpression).IsAssignableFrom(context.DestinationType)
2222
&& context.DestinationType != typeof(LambdaExpression);
2323

24-
public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression) =>
24+
public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, IMemberMap memberMap, Expression sourceExpression, Expression destExpression, Expression contextExpression) =>
2525
Call(null,
2626
MapMethodInfo.MakeGenericMethod(sourceExpression.Type, destExpression.Type),
2727
sourceExpression,
@@ -151,11 +151,11 @@ private static bool BothAreNullable(Expression node, Expression newLeft)
151151
private static bool BothAreNonNullable(Expression node, Expression newLeft)
152152
=> !node.Type.IsNullableType() && !newLeft.Type.IsNullableType();
153153

154-
protected override Expression VisitLambda<T>(Expression<T> expression)
154+
protected override Expression VisitLambda<T>(Expression<T> node)
155155
{
156-
return expression.Parameters.Any(b => b.Type == _oldParam.Type)
157-
? VisitLambdaExpression(expression)
158-
: VisitAllParametersExpression(expression);
156+
return node.Parameters.Any(b => b.Type == _oldParam.Type)
157+
? VisitLambdaExpression(node)
158+
: VisitAllParametersExpression(node);
159159
}
160160

161161
private Expression VisitLambdaExpression<T>(Expression<T> expression)
@@ -207,8 +207,8 @@ protected override Expression VisitMember(MemberExpression node)
207207
if (replacedExpression == node.Expression)
208208
replacedExpression = _parentMappingVisitor.Visit(node.Expression);
209209

210-
if (propertyMap.CustomExpression != null)
211-
return propertyMap.CustomExpression.ReplaceParameters(replacedExpression);
210+
if (propertyMap.CustomMapExpression != null)
211+
return propertyMap.CustomMapExpression.ReplaceParameters(replacedExpression);
212212

213213
Func<Expression, MemberInfo, Expression> getExpression = MakeMemberAccess;
214214

@@ -235,7 +235,7 @@ private Expression GetConvertedSubMemberCall(MemberExpression node)
235235
if (propertyMap == null)
236236
return node;
237237
var sourceType = GetSourceType(propertyMap);
238-
var destType = propertyMap.DestinationPropertyType;
238+
var destType = propertyMap.DestinationType;
239239
if (sourceType == destType)
240240
return MakeMemberAccess(baseExpression, node.Member);
241241
var typeMap = _configurationProvider.ResolveTypeMap(sourceType, destType);
@@ -250,7 +250,7 @@ private Type GetSourceType(PropertyMap propertyMap) =>
250250
throw new AutoMapperMappingException(
251251
"Could not determine source property type. Make sure the property is mapped.",
252252
null,
253-
new TypePair(null, propertyMap.DestinationPropertyType),
253+
new TypePair(null, propertyMap.DestinationType),
254254
propertyMap.TypeMap,
255255
propertyMap);
256256

src/AutoMapper.Extensions.ExpressionMapping/MapperExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private static void FindChildPropertyTypeMaps(this Dictionary<Type, Type> typeMa
299299
if (typeMap == null)
300300
return;
301301

302-
FindMaps(typeMap.GetPropertyMaps().ToList());
302+
FindMaps(typeMap.PropertyMaps.ToList());
303303
void FindMaps(List<PropertyMap> maps)
304304
{
305305
foreach (PropertyMap pm in maps)
@@ -309,7 +309,7 @@ void FindMaps(List<PropertyMap> maps)
309309

310310
AddChildMappings
311311
(
312-
source.GetFieldOrProperty(pm.DestinationProperty.Name).GetMemberType(),
312+
source.GetFieldOrProperty(pm.DestinationMember.Name).GetMemberType(),
313313
pm.SourceMember.GetMemberType()
314314
);
315315
void AddChildMappings(Type sourcePropertyType, Type destPropertyType)

src/AutoMapper.Extensions.ExpressionMapping/XpressionMapperVisitor.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public XpressionMapperVisitor(IMapper mapper, IConfigurationProvider configurati
3131

3232
protected IMapper Mapper { get; }
3333

34-
protected override Expression VisitParameter(ParameterExpression parameterExpression)
34+
protected override Expression VisitParameter(ParameterExpression node)
3535
{
36-
InfoDictionary.Add(parameterExpression, TypeMappings);
37-
var pair = InfoDictionary.SingleOrDefault(a => a.Key.Equals(parameterExpression));
38-
return !pair.Equals(default(KeyValuePair<Type, MapperInfo>)) ? pair.Value.NewParameter : base.VisitParameter(parameterExpression);
36+
InfoDictionary.Add(node, TypeMappings);
37+
var pair = InfoDictionary.SingleOrDefault(a => a.Key.Equals(node));
38+
return !pair.Equals(default(KeyValuePair<Type, MapperInfo>)) ? pair.Value.NewParameter : base.VisitParameter(node);
3939
}
4040

4141
protected override Expression VisitMember(MemberExpression node)
@@ -328,54 +328,54 @@ protected void FindDestinationFullName(Type typeSource, Type typeDestination, st
328328
PathMap pathMap = typeMap.FindPathMapByDestinationPath(destinationFullPath: sourceFullName);
329329
if (pathMap != null)
330330
{
331-
propertyMapInfoList.Add(new PropertyMapInfo(pathMap.SourceExpression, new List<MemberInfo>()));
331+
propertyMapInfoList.Add(new PropertyMapInfo(pathMap.CustomMapExpression, new List<MemberInfo>()));
332332
return;
333333
}
334334

335335

336336
if (sourceFullName.IndexOf(period, StringComparison.OrdinalIgnoreCase) < 0)
337337
{
338338
var propertyMap = typeMap.GetPropertyMapByDestinationProperty(sourceFullName);
339-
var sourceMemberInfo = typeSource.GetFieldOrProperty(propertyMap.DestinationProperty.Name);
339+
var sourceMemberInfo = typeSource.GetFieldOrProperty(propertyMap.DestinationMember.Name);
340340
if (propertyMap.ValueResolverConfig != null)
341341
{
342342
throw new InvalidOperationException(Resource.customResolversNotSupported);
343343
}
344344

345-
if (propertyMap.CustomExpression == null && !propertyMap.SourceMembers.Any())
345+
if (propertyMap.CustomMapExpression == null && !propertyMap.SourceMembers.Any())
346346
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.srcMemberCannotBeNullFormat, typeSource.Name, typeDestination.Name, sourceFullName));
347347

348348
CompareSourceAndDestLiterals
349349
(
350-
propertyMap.CustomExpression != null ? propertyMap.CustomExpression.ReturnType : propertyMap.SourceMember.GetMemberType(),
351-
propertyMap.CustomExpression != null ? propertyMap.CustomExpression.ToString() : propertyMap.SourceMember.Name,
350+
propertyMap.CustomMapExpression != null ? propertyMap.CustomMapExpression.ReturnType : propertyMap.SourceMember.GetMemberType(),
351+
propertyMap.CustomMapExpression != null ? propertyMap.CustomMapExpression.ToString() : propertyMap.SourceMember.Name,
352352
sourceMemberInfo.GetMemberType()
353353
);
354354

355355
void CompareSourceAndDestLiterals(Type mappedPropertyType, string mappedPropertyDescription, Type sourceMemberType)
356356
{
357357
//switch from IsValueType to IsLiteralType because we do not want to throw an exception for all structs
358358
if ((mappedPropertyType.IsLiteralType() || sourceMemberType.IsLiteralType()) && sourceMemberType != mappedPropertyType)
359-
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.expressionMapValueTypeMustMatchFormat, mappedPropertyType.Name, mappedPropertyDescription, sourceMemberType.Name, propertyMap.DestinationProperty.Name));
359+
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.expressionMapValueTypeMustMatchFormat, mappedPropertyType.Name, mappedPropertyDescription, sourceMemberType.Name, propertyMap.DestinationMember.Name));
360360
}
361361

362-
propertyMapInfoList.Add(new PropertyMapInfo(propertyMap.CustomExpression, propertyMap.SourceMembers.ToList()));
362+
propertyMapInfoList.Add(new PropertyMapInfo(propertyMap.CustomMapExpression, propertyMap.SourceMembers.ToList()));
363363
}
364364
else
365365
{
366366
var propertyName = sourceFullName.Substring(0, sourceFullName.IndexOf(period, StringComparison.OrdinalIgnoreCase));
367367
var propertyMap = typeMap.GetPropertyMapByDestinationProperty(propertyName);
368368

369-
var sourceMemberInfo = typeSource.GetFieldOrProperty(propertyMap.DestinationProperty.Name);
370-
if (propertyMap.CustomExpression == null && !propertyMap.SourceMembers.Any())//If sourceFullName has a period then the SourceMember cannot be null. The SourceMember is required to find the ProertyMap of its child object.
369+
var sourceMemberInfo = typeSource.GetFieldOrProperty(propertyMap.DestinationMember.Name);
370+
if (propertyMap.CustomMapExpression == null && !propertyMap.SourceMembers.Any())//If sourceFullName has a period then the SourceMember cannot be null. The SourceMember is required to find the ProertyMap of its child object.
371371
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.srcMemberCannotBeNullFormat, typeSource.Name, typeDestination.Name, propertyName));
372372

373-
propertyMapInfoList.Add(new PropertyMapInfo(propertyMap.CustomExpression, propertyMap.SourceMembers.ToList()));
373+
propertyMapInfoList.Add(new PropertyMapInfo(propertyMap.CustomMapExpression, propertyMap.SourceMembers.ToList()));
374374
var childFullName = sourceFullName.Substring(sourceFullName.IndexOf(period, StringComparison.OrdinalIgnoreCase) + 1);
375375

376-
FindDestinationFullName(sourceMemberInfo.GetMemberType(), propertyMap.CustomExpression == null
376+
FindDestinationFullName(sourceMemberInfo.GetMemberType(), propertyMap.CustomMapExpression == null
377377
? propertyMap.SourceMember.GetMemberType()
378-
: propertyMap.CustomExpression.ReturnType, childFullName, propertyMapInfoList);
378+
: propertyMap.CustomMapExpression.ReturnType, childFullName, propertyMapInfoList);
379379
}
380380
}
381381
}

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/AutoMapper.Extensions.ExpressionMapping.UnitTests.csproj

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
@@ -7,11 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
11-
<PackageReference Include="Shouldly" Version="3.0.0" />
12-
<PackageReference Include="xunit" Version="2.3.1" />
13-
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
14-
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
11+
<PackageReference Include="Shouldly" Version="3.0.2" />
12+
<PackageReference Include="xunit" Version="2.4.1" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
1514
</ItemGroup>
1615

1716
<ItemGroup>

0 commit comments

Comments
 (0)