- Is it OK to create Mappings for DTO objects and then query them
instead of domain? If it is not explain why? - What If i need couple of those dtos?
- DTos are readonly
- ID is autogenerated by NH
- In future these dtos will have set mappings to linked dtos.
-
I use DTO to reduce query size
<class name="Person" table="`APP_Person`"> <property name="FirstName" type="string" length="512" /> <property name="Age" type="int" /> <property name="SocialNumber" type="int" /> <property name="PassportId" type="int" /> <property name="Salary" type="int" /> </class> <class name="PersonDTO" table="`APP_Person`"> <property name="FirstName" type="string" length="512" /> <property name="Age" type="int" /> </class>
You don’t need to map/persist a
DTOobject. It’s normaly to readonly data and send to other layer of your application (web services, views, etc…).You can create a query on the
Personentity that returns aPersonDTOlist. Take a look atSetResultTransformermethod. Try somethin like this:And your DTO:
The result of the column on the
hqlquery should have the same name of your DTO’s properties to NHibernate do the right reflection when construct the DTO and hydrate the object.Linq
You also can use
linqto have aDTO(or a list of DTOs) as a result. For sample:Look this article: http://gustavoringel.blogspot.com.br/2009/02/creating-dto-in-nhibernate-hql-using.html