简体   繁体   English

将目标DTO与嵌套DTO一起用于NHibernate中的Transformers.AliasToBean

[英]Using target DTO with nested DTO for Transformers.AliasToBean in NHibernate

I have two DTO classes like these: 我有两个像这样的DTO类:

public class Mailing
{
  public string MailingType {get;set;}
  public DateTime? ValidUntil {get;set;}
  public Address MailAddress {get;set;}
}

and

public class Address
{
  public string Street {get;set;}
  public string StreetNumber {get;set;}
  public string ZIP {get;set;}
}

The according entities are simliar with identical many-to-one relation. 相应的实体具有相同的多对一关系。

Now I want to read a list of Mailing with one query, preventing a manual construction of DTOs with foreach. 现在,我想阅读一个查询的Mailing列表,以防止使用foreach手动构造DTO。 Something like this: 像这样:

return this.session.QueryOver<MailingEntity>()
    .JoinAlias(p => p.Address, () => addressAlias)
    .Where(...)
    .SelectList(list => list
      .Select(p => p.Type).WithAlias(() => mailingDTO.MailingType)
      .Select(p => p.ValidTo).WithAlias(() => mailingDTO.ValidUntil)
      .Select(() => addressAlias.Street).WithAlias(() => addressDTO.Street)
      .Select(() => addressAlias.ZIP).WithAlias(() => addressDTO.ZIP)
      .Select(() => addressAlias.StreetNumber).WithAlias(() => addressDTO.StreetNumber)
      .Select(() => addressDTO).WithAlias(() => mailingDTO.MailAddress)
    .TransformUsing(Transformers.AliasToBean<Mailing>())
    .List<Mailing>(); 

which does not work. 这不起作用。 Is there any way getting nested DTOs like this or do I have to user several roudtrips respectively manually create the DTOs? 有什么办法可以像这样嵌套DTO,还是我必须分别使用几个roodtrip来手动创建DTO?

if LINQ is an option 如果可以选择LINQ

var query = from e in session.Query<MailingEntity>()
            let a = e.Address
            select new Mailing
            {
                MailingType = e.Type,
                ValidUntil = e.ValidTo,
                MailAddress = new Address
                {
                    Street = a.Street,
                    ZIP = a.ZIP,
                    StreetNumber = a.StreetNumber
                }
            };

 return query.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM