2011-05-27 114 views
2

使用NHibernate 2.1,我试图将实体及其子集合投影到DTO中。我的实体看起来是这样的..Nhibernate投影与子集合

public class Application 
{ 
    public int Id {get;set;} 
    public string Name {get;set;} 
    public List<ApplicationSetting> Settings {get;set;} 
    // A bunch of other properties that I don't want in the DTO 
} 

public class ApplicationSetting 
{ 
    public int Id {get;set;} 
    public string Name {get;set;} 
    public string Code {get;set;} 
    // A bunch of other properties that I don't want in the DTO 
} 

我的DTO是这样的..

public ApplicationDto 
{ 
     public int Id {get;set;} 
     public string Name {get;set;} 
     public List<ApplicationSettingDto> Settings {get;set;} 
} 

public class ApplicationSettingDto 
{ 
    public int Id {get;set;} 
    public string Name {get;set;} 
    public string Code {get;set;} 
} 

我的代码只选择应用程序,它是这个(使用NHibernate 2.1和nhLambdaExtensions)

项目
var applicationAlias = new Application(); 

    var criteria = Session 
    .Add<Application>(a => a.Id == id); 

     int? Id = null; 
    string Name = null; 

    criteria 
    .SetProjection 
    (
     Projections.Distinct(
     Projections.ProjectionList() 
      .Add(LambdaProjection.Property<Application>(a => a.Id).As(() => Id)) 
      .Add(LambdaProjection.Property<Application>(a => a.Name).As(() => Name)) 
     ) 
    ); 

    criteria.SetResultTransformer(Transformers.AliasToBean(typeof(ApplicationDto))); 

    var contract = criteria.UniqueResult<ApplicationDto>(); 

我的问题是,我如何将一些属性从ApplicationSettings实体投影到ApplicationSettingsDto子集合?

+2

你不会做你想做的事。投影是数据而不是实体的扁平表示。所以在投影中你不能有一对多的表示。 – Vadim 2011-05-27 16:19:32

回答

2

我想你可能需要做一个MutiQuery,并把你自己的DTO父母和孩子聚集在一起。