Reputation: 2023
If I want to create a DTO from properties on classes, all of which are mapped, can I do this without writing a regular SQL statement?
I've seen a lot of documentation on making the DTO class, as well as using it, but not much about how it gets created. I've seen SQL queries used with the Transformer, but that requires the use of magic strings.
Edit:
Thanks for the comments. I am aware there are multiple methods to retrieve DTOs, but I have been unable to find examples of methods that don't use SQL/HQL. Is there a reference somewhere, or is this one of those sparsely documented areas of NHibernate?
Upvotes: 0
Views: 1094
Reputation: 22424
From your latest comment here are some examples of projecting a DTO using QueryOver
and Linq
Using QueryOver:-
var schoolList = Session.QueryOver<lmschool>()
.SelectList(i => i
.Select(p => p.Name).WithAlias(() => dto.Name)
.Select(p => p.Lat).WithAlias(() => dto.Lat)
.Select(p => p.Lng).WithAlias(() => dto.Lng)
)
.Where(w => w.Lat != null && w.Lng != null)
.TransformUsing(Transformers.AliasToBean<MarkerDto>())
.List<MarkerDto>();
Using the NH Linq provider:-
var schoolList = (from school in Session.Query<lmschool>()
.Where(w => w.Lat != null && w.Lng != null)
select new LmSchoolMarkerDto {
Name = school.Name,
Lat = school.Lat,
Lng = school.Lng,
}).ToList();
Source is from my blog Also if you need to see how multiple joins are used the see this blog post.
There are also lots of S.O. posts that should help you, see this search.
Upvotes: 5
Reputation: 8201
You could load all necessary entities and use AutoMapper to map them to DTOs more easily ?
Upvotes: 0