简体   繁体   中英

Using target DTO with nested DTO for Transformers.AliasToBean in NHibernate

I have two DTO classes like these:

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. 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?

if LINQ is an option

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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