比方说,我有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;,
// ...
}
};
其目标是,obviousely,以避免在复合材料一个重写每个单独的转换