You do not need to display / save the DTO object. This is normal for reading only data and sending it to another level of your application (web services, views, etc.).
You can create a query for a Person object that returns a PersonDTO list. Take a look at the SetResultTransformer method. Try something like this:
var query = session.CreateQuery("select p.FirstName, p.Age from Person p order by p.FirstName") .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(PersonDTO))) .List<PersonDTO>();
And your DTO:
public class PersonDTO { public string FirstName { get; set; } public int Age { get; set; } }
The result of the hql query hql must have the same name for your DTO properties so that NHibernate will correctly reflect when building the DTO and hydrate the object.
Linq
You can also use linq to get a DTO (or list of DTOs). For sample:
var query = Session.Query<Person>().OrderBy(x => x.FirstName) .Select(x = new PersonDTO() { FirstName = x.FirstName, Age = x.Age }) .ToList();
Check out this article: http://gustavoringel.blogspot.com.br/2009/02/creating-dto-in-nhibernate-hql-using.html
source share