Skip to content

Commit

Permalink
Merge pull request #31 from BlaiseD/master
Browse files Browse the repository at this point in the history
Fix breaking changes from AutoMapper v8.1.0
  • Loading branch information
BlaiseD authored Apr 28, 2019
2 parents d0c9f9d + 68e2adb commit 5826a45
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Authors>Jimmy Bogard</Authors>
<LangVersion>latest</LangVersion>
<VersionPrefix>2.0.1</VersionPrefix>
<VersionPrefix>2.0.2</VersionPrefix>
<WarningsAsErrors>true</WarningsAsErrors>
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="8.0.0" />
<PackageReference Include="AutoMapper" Version="8.1.0" />
</ItemGroup>

</Project>
10 changes: 9 additions & 1 deletion src/AutoMapper.Extensions.ExpressionMapping/ExpressionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,15 @@ private PropertyMap PropertyMap(MemberExpression node)
? null
: (!node.Member.DeclaringType.IsAssignableFrom(_typeMap.DestinationType)
? null
: _typeMap.GetExistingPropertyMapFor(node.Member)));
: GetExistingPropertyMapFor(node.Member, _typeMap)));

private PropertyMap GetExistingPropertyMapFor(MemberInfo destinationProperty, TypeMap typeMap)
{
if (!destinationProperty.DeclaringType.IsAssignableFrom(typeMap.DestinationType))
return null;

return typeMap.PropertyMaps.FirstOrDefault(pm => pm.DestinationName == destinationProperty.Name);
}

private void SetSorceSubTypes(PropertyMap propertyMap)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,20 @@ public QueryDataSourceInjection(IQueryable<TSource> dataSource, IMapper mapper)
public ISourceInjectedQueryable<TDestination> For<TDestination>(object parameters, params Expression<Func<TDestination, object>>[] membersToExpand)
{
_parameters = GetParameters(parameters);
_membersToExpand = ProjectionExpression.GetMemberPaths(membersToExpand);
_membersToExpand = ReflectionExtensions.GetMemberPaths(membersToExpand);
return CreateQueryable<TDestination>();
}

public ISourceInjectedQueryable<TDestination> For<TDestination>(params Expression<Func<TDestination, object>>[] membersToExpand)
{
_membersToExpand = ProjectionExpression.GetMemberPaths(membersToExpand);
_membersToExpand = ReflectionExtensions.GetMemberPaths(membersToExpand);
return CreateQueryable<TDestination>();
}

public ISourceInjectedQueryable<TDestination> For<TDestination>(IObjectDictionary parameters, params string[] membersToExpand)
{
_parameters = parameters;
_membersToExpand = ProjectionExpression.GetMemberPaths(typeof(TDestination), membersToExpand);
_membersToExpand = ReflectionExtensions.GetMemberPaths(typeof(TDestination), membersToExpand);
return CreateQueryable<TDestination>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ public TResult Execute<TResult>(Expression expression)
var sourceResult = _dataSource.Provider.CreateQuery(sourceExpression);
Inspector.SourceResult(sourceExpression, sourceResult);

destResult = new ProjectionExpression((IQueryable<TSource>)sourceResult, _mapper.ConfigurationProvider.ExpressionBuilder).To<TDestination>(_parameters, _membersToExpand);
var membersToExpand = _membersToExpand.SelectMany(m => m).Distinct().ToArray();

var parameters = _parameters ?? new Dictionary<string, object>();
var mapExpressions = _mapper.ConfigurationProvider.ExpressionBuilder.GetMapExpression(sourceResult.ElementType, typeof(TDestination), parameters, membersToExpand);

destResult = (IQueryable<TDestination>)mapExpressions.Aggregate(sourceResult, Select);
}
// case #2: query is arbitrary ("manual") projection
// exaple: users.UseAsDataSource().For<UserDto>().Select(user => user.Age).ToList()
Expand Down Expand Up @@ -228,6 +233,14 @@ private static Expression Select(Expression source, LambdaExpression lambda)
);
}

private static IQueryable Select(IQueryable source, LambdaExpression lambda) => source.Provider.CreateQuery(
Call(
null,
QueryableSelectMethod.MakeGenericMethod(source.ElementType, lambda.ReturnType),
new[] { source.Expression, Expression.Quote(lambda) }
)
);

private object InvokeSourceQuery(Type sourceResultType, Expression sourceExpression)
{
var result = IsProjection<TSource>(sourceResultType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using AutoMapper.Internal;

namespace AutoMapper
{
using MemberPaths = IEnumerable<IEnumerable<MemberInfo>>;

internal static class ReflectionExtensions
{
public static object GetDefaultValue(this ParameterInfo parameter)
Expand All @@ -29,6 +32,12 @@ public static object GetMemberValue(this MemberInfo propertyOrField, object targ
public static IEnumerable<MemberInfo> GetMemberPath(Type type, string fullMemberName)
=> ReflectionHelper.GetMemberPath(type, fullMemberName);

public static MemberPaths GetMemberPaths(Type type, string[] membersToExpand) =>
membersToExpand.Select(m => ReflectionHelper.GetMemberPath(type, m));

public static MemberPaths GetMemberPaths<TResult>(Expression<Func<TResult, object>>[] membersToExpand) =>
membersToExpand.Select(expr => MemberVisitor.GetMemberPath(expr));

public static MemberInfo GetFieldOrProperty(this LambdaExpression expression)
=> ReflectionHelper.GetFieldOrProperty(expression);

Expand Down

0 comments on commit 5826a45

Please sign in to comment.