2009-07-13 37 views
2

我需要获取DTO中填充的集合属性,并且无法找到任何关于此操作的信息。如何获得一个DTO成员与nhibernate的集合?

我试图做这样的:

ICriteria selectCriteria = Session.CreateCriteria<DataRun>() 
      .SetProjection(Projections.ProjectionList() 
       .Add(Projections.Property("SomeCollection"), "Collection")) 
      .SetResultTransformer(Transformers.AliasToBean<MyDto>()); 

但MyDto.Collection总是空。我做错了,这甚至有可能吗?

此外,我原本计划使用SubQuery做这件事,所以我可以用其他DTO填充我的DTO集合,但这不起作用,因为子查询的结果有超过1行(如它应该)和Sqlit不喜欢那样(引发异常)。在这里做什么是正确的事情?

+0

我认为你正在试图做一个一对多应该在你的映射做,但我真的不知道。你能发布你的映射文件吗? – 2009-07-13 19:48:16

+0

我正在做一对多和我的映射实体工作正常,但我不想让我的映射实体回来,我想要一个DTO的一堆属性,什么不是特定的他们将使用的观点。 – mockobject 2009-07-13 19:55:01

回答

0

我检索到的集合属性在我的项目的语法如下:

public IList<ChildCollectionType> GetChildObjectsByParentId(Int32 id) 
{ 
    ISession session = GetSession();//Get NHibernate session routine 
    return session.Load<ParentDTO>(id).ChildCollectionProperty; 
} 

其中ID是父对象的关键。

0

您可以使用自定义转换。

ICriteria selectCriteria = Session.CreateCriteria<DataRun>() 
      .SetProjection(Projections.ProjectionList() 
       .Add(Projections.Property("SomeCollection"), "Collection")) 
      .TransformUsing(new CustomTransformer()); 

这是您的自定义变压器实现

public class CustomTransformer : IResultTransformer 
    { 
     public System.Collections.IList TransformList(System.Collections.IList collection) 
     { 
      return collection; 
     } 

     public object TransformTuple(object[] tuple, string[] aliases) 
     { 
      return new MyDto 
      { 
       //map your data to dto and convert to data type if needed 
       YourProperty1 = tuple[0], 
       YourProperty12 = (int)tuple[1] 
      }; 
     } 
    } 
相关问题