使用AutoMapper时Expression的转换

此文章为转载:http://www.bubuko.com/infodetail-699735.html

参考链接:

http://q.cnblogs.com/q/34480/   dudu有回复,其中一个链接http://*.com/questions/7424501/automapper-for-funcs-between-selector-types可以打开,并且本人主要参考此链接进行修改

下文链接为automapper相关源码

http://www.symbolsource.org/Public/Metadata/NuGet/Project/AutoMapper/3.3.0-ci1017/Release/Unsupported,Version%3Dv0.0/AutoMapper/AutoMapper/Internal/MappingExpression.cs?ImageName=AutoMapper

以下为转载内容:

问题描述

项目中使用AutoMapper进行VO&DTO&Entity的互相映射,但是默认Map方法不支持Expression的转换。如

Expression<Func<Entity,bool>> fun = _ => _.A == "A";

希望转换成

Expression<Func<Dto,bool>> fun = _ => _.A == "A";

似乎解决方案就是解析ExpressionTree并映射替换节点。正好找到了人家的提问和解决方案

http://*.com/questions/7424501/automapper-for-funcs-between-selector-types

改造一下支持泛型,代码如下:

 #region using namespace

 using System;
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;
using AutoMapper;
using AutoMapper.QueryableExtensions; #endregion namespace Core
{
public static class FunctionCompositionExtensions
{
private static readonly ConcurrentDictionary<Tuple<Type, Type>, Tuple<MethodInfo, Expression>> Dictionary =
new ConcurrentDictionary<Tuple<Type, Type>, Tuple<MethodInfo, Expression>>(); private static readonly MethodInfo Method = typeof (FunctionCompositionExtensions).GetMethod("Compose",
BindingFlags.NonPublic | BindingFlags.Static); public static Expression<Func<TDestination, bool>> MapExpression<TSource, TDestination>(
this Expression<Func<TSource, bool>> selector)
{
var bulidMethod = Dictionary.GetOrAdd(new Tuple<Type, Type>(typeof (TSource), typeof (TDestination)), _ =>
{
var expression = Mapper.Engine.CreateMapExpression<TDestination, TSource>();
return new Tuple<MethodInfo, Expression>(
Method.MakeGenericMethod(typeof (TDestination), typeof (bool), typeof (TSource)), expression);
});
return
bulidMethod.Item1.Invoke(null, new object[] {selector, bulidMethod.Item2}) as
Expression<Func<TDestination, bool>>;
} public static Expression<Func<TX, TY>> Compose<TX, TY, TZ>(this Expression<Func<TZ, TY>> outer,
Expression<Func<TX, TZ>> inner)
{
return Expression.Lambda<Func<TX, TY>>(
ParameterReplacer.Replace(outer.Body, outer.Parameters[], inner.Body),
inner.Parameters[]);
} public static Expression<Predicate<TX>> ComposePredicate<TX, TZ>(this Expression<Predicate<TZ>> outer,
Expression<Func<TX, TZ>> inner)
{
return Expression.Lambda<Predicate<TX>>(
ParameterReplacer.Replace(outer.Body, outer.Parameters[], inner.Body),
inner.Parameters[]);
}
} class ParameterReplacer : ExpressionVisitor
{
private readonly ParameterExpression _parameter;
private readonly Expression _replacement; private ParameterReplacer(ParameterExpression parameter, Expression replacement)
{
_parameter = parameter;
_replacement = replacement;
} public static Expression Replace(Expression expression, ParameterExpression parameter, Expression replacement)
{
return new ParameterReplacer(parameter, replacement).Visit(expression);
} protected override Expression VisitParameter(ParameterExpression parameter)
{
if (parameter == _parameter)
{
return _replacement;
}
return base.VisitParameter(parameter);
}
}
}

测试

 public class Entity
{
public string A { get; set; }
} public class Dto
{
public string A { get; set; }
} Mapper.CreateMap<Entity, Dto>();
Mapper.CreateMap<Dto, Entity>(); var list = new List<Dto>()
{
new Dto() {A = "A"},
new Dto() {A = "B"},
new Dto() {A = "C"},
new Dto() {A = "D"},
new Dto() {A = "E"},
}; //Predicate<Entity> fun = _ => _.A =="A";
Expression<Func<Entity,bool>> funEntity = _ => _.A == "A"; var query = list.Where(funEntity.MapExpression<Entity, Dto>().Compile());
Assert.True(query.Count() == ); Expression<Func<Entity, bool>> funEntity2 = _ => _.A == "F";
var query2 = list.Where(funEntity2.MapExpression<Entity, Dto>().Compile());
Assert.True(query2.Count() == );
上一篇:leetcode4


下一篇:MVC验证