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

时间:2012-03-13 13:36:48

标签: nhibernate transform alias

我有两个像这样的DTO课程:

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

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

相应的实体是相同的多对一关系。

现在我想通过一个查询阅读Mailing列表,防止使用foreach手动构建DTO。像这样:

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

哪个不起作用。 是否有任何方法可以获得这样的嵌套DTO,或者我是否需要分别手动创建DTO来使用多个rouds?

1 个答案:

答案 0 :(得分:1)

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