NHibernate child collection Projection to DTO

倖福魔咒の 提交于 2019-12-07 22:30:35

问题


I have had a good look around for answers on this, and several questions suggest this cannot be done.

Nhibernate projection with child collection

NHibernate QueryOver projections - projecting collections to DTO

NHibernate Projections - how to project collections

So I was wondering what would be a good work around to project a child collection to my DTO. Will I need to run two seperate projections and manually add the children to the parent?

I am using NH 3.3.1 I have the following DTO data structure

 public class ProviderDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IList<EmployerDTO> Employers { get; set; }
}


 public class EmployerDTO
{
    public int Id { get; set; }
    public string Name { get; set; }

}

I would like to map my Provider and employer domain entities to this structure with a projection. (In this simple scenario my domain entities are very similar to my dto's)

Employer employerAlias = null;
Provider providerAlias = null;
ProviderDto providerDto = null;

var dto = session.QueryOver<Provider>(() => providerAlias)
            .JoinAlias(x => x.Employers, () => employerAlias)
            .Where(()=> providerAlias.Id == 1)
            .Select(Projections.ProjectionList()
            .Add(Projections.Property(() => providerAlias.Id).WithAlias(() => providerDto.Id))
            .Add(Projections.Property(() => providerAlias.Name).WithAlias(() => providerDto.Name))

              //This is where I cannot project my child employer collection
            .TransformUsing(Transformers.AliasToBean<ProviderDto>()).List<ProviderDto>();

How could I map across the child collection? Thanks very much.


回答1:


You should batch queries using futures and construct the relationship yourself.

Employer employerAlias = null;
Provider providerAlias = null;
ProviderDto providerDto = null;
EmployerDto employerDto = null;

var providers = session.QueryOver<Provider>(() => providerAlias)
        .JoinAlias(x => x.Employers, () => employerAlias)
        .Where(()=> providerAlias.Id == 1)
        .Select(Projections.ProjectionList()
        .Add(Projections.Property(() => providerAlias.Id).WithAlias(() =>    providerDto.Id))
        .Add(Projections.Property(() => providerAlias.Name).WithAlias(() => providerDto.Name))
        .TransformUsing(Transformers.AliasToBean<ProviderDto>()).Future<ProviderDto>();

var employers = session.QueryOver<Employer>(() => employerAlias)
//Etc 
.TransformUsing(Transformers.AliasToBean<EmployerDto>()).Future<EmployerDto>();

Then it's a matter of mapping the correct employers to providers. You might need more information in the DTO for that, but using Future queries, you will only make one database roundtrip to get all the information.

EDIT:

In order to get which employers map to which providers.

session.QueryOver(() => provider)
       .JoinAlias(() => provider.Employers, () => employer)
       .SelectList(list => list.Select(() => provider.Id).WithAlias(() =>     peDTO.ProviderId)
                               .Select(() => employer.Id).WithAlias(() =>     peDTO.EmployerId))
       .TransformUsing(Transformers.AliasToBean<ProviderEmployerMapDTO>()).Future<ProviderEmployerMapDTO>();


来源:https://stackoverflow.com/questions/17279307/nhibernate-child-collection-projection-to-dto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!