组成复合DTO的表达式树

编程入门 行业动态 更新时间:2024-10-23 05:49:04
本文介绍了组成复合DTO的表达式树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有以下3个DTO

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

我有3个表达式树来管理DAO到DTO的转换:

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

如何从上面的3中构建给定表达式?

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

推荐答案

简短的答案是使用 AutoMapper ,或将编译后的表达式转换为函数。在C#中,函数的编写很容易,而表达式的编写则更加困难。

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.

更长的答案是可能的,但这并不容易。您的代码使用友好的 Expression语法,但实际上要混合使用&匹配表达式,您将需要使用不友好的版本,该版本更难看且更难维护:

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

更多推荐

组成复合DTO的表达式树

本文发布于:2023-11-29 23:16:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   DTO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!