为复合DTO组合表达式树

时间:2015-01-02 11:44:13

标签: c# expression-trees composition

我们说我有以下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;,
                                // ...
                            }
                        };

显然,目标是避免在复合转换中重写每个单独的转换

1 个答案:

答案 0 :(得分:2)

简短的回答是使用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]
        );