Please refer to following code:
using(ISession session = SessionFactory.OpenSession())
{
//Case 1: using Query = works fine.
IList<MasterDto> listMaster = session.Query<MasterEntity>()
.ProjectTo<MasterDto>(autoMapperConfig)
.ToList();
//Case 2: using QueryOver = how to achieve same result as above?
IQueryable<MasterDto> masterDtos = session.Query<MasterEntity>()
.ProjectTo<MasterDto>(autoMapperConfig);
IList<MasterEntity> list = session.QueryOver<MasterEntity>()
.Select(masterDtos)//This of-course does not work
.List();
}
Both cases above return same result if I comment Select(masterDtos) in Case 2. Other difference is the generated SQL query.
In Case 1 above, the generated SQL only contains the columns those present in Dto. It skips the additional columns present in Entity but not present in Dto. This is necessary to improve SQL query performance.
The autoMapperConfig parameter passed to ProjectTo method there is instance of AutoMapper.MapperConfiguration.
Case 1 works fine.
But I have huge legacy code that uses QueryOver.
It is hard and error prone to migrate this code from QueryOver to Query API of NHibernate.
So, I was playing around how can I achieve this (SELECTing selected columns) with QueryOver.
One way that I already know is to use Select and/or SelectList methods of QueryOver and pass proper parameters to it.
This will work fine; but there are few problems with this approach:
- I have to type in each projection/expression as input to those methods.
- If I change the Dto in future, I should remember to change the projection list as well.
So, I was thinking something mentioned in above code. Is this possible? Is there any other similar/simpler solution to this?