为复合 DTO 编写表达式树
本文关键字:表达式 复合 DTO | 更新日期: 2023-09-27 18:36:00
假设我有以下 3 个 DTO
。public class Mailing
{
public long Id { get; set; }
//...
public long IdSender { get; set; }
public Sender Sender { get; set; }
public long IdTemplate { get; set; }
public Template Template { get; set; }
}
public class Sender
{
public long Id { get; set; }
public string Email { get; set; }
//...
}
public class Template
{
public long Id { get; set; }
public string Name { get; set; }
//...
}
我有 3 个表达式树来管理 DAO 到 DTO 的转换:
private static readonly Expression<Func<DaoMailing, Mailing>> ToMailingShort =
input => new Mailing
{
Id = input.Id,
IdSender = input.IdSender,
IdTemplate = input.IdTemplate,
// ...
};
private static readonly Expression<Func<DaoTemplate, Template>> ToTemplate =
input => new Template
{
Id = input.Id,
Name = input.Name,
// ...
};
private static readonly Expression<Func<DaoSender, Sender>> ToSender =
input => new Sender
{
Id = input.Id,
Email = input.Email,
// ...
};
如何从上面的 3 个构建给定的表达式?
private static readonly Expression<Func<DaoMailing, DaoTemplate, DaoSender, MailingFull>> ToMailingFull =
(input, template, sender) => new Mailing
{
Id = input.Id,
IdSender = input.IdSender,
IdTemplate = input.IdTemplate,
// ...
Template = new Template
{
Id = template.Id,
Name = template.Name,
// ...
},
new Sender
{
Id = sender.Id,
Email = sender.Emai;,
// ...
}
};
目标显然是避免重写复合转换中的每个单独转换
简短的回答是使用AutoMapper,或将编译的表达式转换为函数。在 C# 中,编写函数很容易,而编写表达式则困难得多。
更长的答案是,这是可能的,但并不容易。你的代码使用"友好"的表达式语法,但要真正混合和匹配表达式,你需要使用不友好的版本,它更丑陋,更难维护:
private static readonly Expression<Func<DaoMailing, DaoTemplate, DaoSender, Mailing>> ToMailingFull =
(Expression<Func<DaoMailing, DaoTemplate, DaoSender, Mailing>>)Expression.Lambda(
Expression.MemberInit(
Expression.New(typeof(Mailing).GetConstructor(Type.EmptyTypes)),
(ToMailingShort.Body as MemberInitExpression).Bindings
.Concat(new List<MemberBinding>{
Expression.MemberBind(typeof(Mailing).GetProperty("Sender"), (ToSender.Body as MemberInitExpression).Bindings),
Expression.MemberBind(typeof(Mailing).GetProperty("Template"), (ToTemplate.Body as MemberInitExpression).Bindings)
})
),
ToMailingShort.Parameters[0],
ToTemplate.Parameters[0],
ToSender.Parameters[0]
);