Composing expression trees for composite DTO

血红的双手。 提交于 2019-12-11 13:12:20

问题


Let's say I have the 3 followings DTOs

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; }
    //...
}

And I have 3 expression trees to manage DAO-to-DTO conversion :

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,
                            // ...
                        };

How can I build the given expression from the 3 above ?

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;,
                                // ...
                            }
                        };

The goal being, obviousely, to avoid rewriting each individual conversion in the composite one


回答1:


The short answer is use AutoMapper, or the compiled expressions into functions. It is easy in C# to compose functions, much harder to compose expressions.

The longer answer is that this is possible, but not easy. Your code uses the 'friendly' Expression syntax, but to really mix & match the expressions, you would need to use the unfriendly version, which is much uglier and harder to maintain:

    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]
        );


来源:https://stackoverflow.com/questions/27741659/composing-expression-trees-for-composite-dto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!