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.