NHibernate child collection Projection to DTO

自古美人都是妖i 提交于 2019-12-06 08:12:02

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