2009-07-13 82 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] 
      }; 
     } 
    } 
相關問題