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.
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