我的框架中有DTO模型。 BLL和View根本看不到EntiyModel。我想在层之间传输和转换复杂表达式。
这是BLL中的一种方法......
//// BLL(Service)
Public PersonDTO getAll(Expression<Func<PersonDTO, bool>> whereCondition)
{
return _repository.getAll(whereCondition);
}
// DLL(Repository)
Public PersonDTO getAll(Expression<Func<PersonDTO, bool>> whereCondition)
{
Expression<Func<Person, bool>> NewCondition = ?/ How Convert DTOwhereCondition ???
return DataContext.Persons(NewCondition);
}
/////我想在PersonDTO上创建复杂的表达式,如下所示:
var persons = serive.getPersons(i => i.PersonDetailsDTO.Count == 3);
/////我的班级
public class Person
{
public Int32 Id { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
public List<PersonDetail> PersonDetails { get; set; }
}
public class PersonDTO
{
public Int32 Id { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
public List<PersonDetailDTO> PersonDetailsDTO { get; set; }
}
答案 0 :(得分:0)
这是最基本的解决方案。如果您需要更多功能,也许您可以从那里开始工作。到目前为止,它允许可选地提供一个委托来解析映射的属性名称,但属性类型必须相同。
用法:
var mappedExpression =
ExpressionMapper<Person>.MapFrom(originalDtoExpression, null);
// or:
var mappedExpression =
ExpressionMapper<Person>.MapFrom(originalDtoExpression,
entityPropertyName => someMapper.ResolveDtoPropertyName(entityPropertyName));
实现:
public class ExpressionMapper<TTarget> : ExpressionVisitor
{
protected ExpressionMapper(Type sourceType, Func<string, string> resolveTargetMemberNameFunc)
{
this.SourceType = sourceType;
this.ResolveTargetMemberNameFunc = resolveTargetMemberNameFunc ?? (cur => cur);
}
public Func<string, string> ResolveTargetMemberNameFunc { get; private set; }
public Type SourceType { get; private set; }
public static Expression MapFrom<TSource>(Expression sourceExpression, Func<string, string> resolveTargetMemberNameFunc)
{
return new ExpressionMapper<TTarget>(typeof(TSource), resolveTargetMemberNameFunc).Visit(sourceExpression);
}
public static Expression<Func<TTarget, bool>> MapFrom<TSource>(Expression<Func<TSource, bool>> sourcePredicateExpression, Func<string, string> resolveTargetMemberNameFunc)
{
return Expression.Lambda<Func<TTarget, bool>>(
MapFrom<TSource>(sourcePredicateExpression.Body, resolveTargetMemberNameFunc),
Expression.Parameter(typeof(TTarget), sourcePredicateExpression.Parameters[0].Name));
}
protected override Expression VisitMember(MemberExpression node)
{
var sourceMemberName = node.Member.Name;
var targetNode = this.Visit(node.Expression);
var targetMemberName = this.ResolveTargetMemberNameFunc(sourceMemberName);
var targetMember = targetNode.Type.GetMember(targetMemberName).FirstOrDefault();
if (targetMember == null)
{
throw new NotSupportedException(String.Format("The source type '{0}' cannot be mapped to the target type '{1}', because that target type has no member '{2}'.", node.Expression.Type.Name, targetNode.Type.Name, targetMemberName));
}
return Expression.MakeMemberAccess(targetNode, targetMember);
}
protected override Expression VisitParameter(ParameterExpression node)
{
if (node.Type == this.SourceType)
{
return Expression.Parameter(typeof(TTarget), node.Name);
}
return node;
}
}