Create DTO with NHibernate

My previous blog is about using DTO in UI/Presenter layers. Just studying Nhibernate source code now, I find a very interesting approach to populate DTO object directly from Nhibernate with Criteria and Projections.
Here is the code:

IList resultWithAliasedBean = s.CreateCriteria(typeof(Enrolment)) .CreateAlias("Student", "st") .CreateAlias("Course", "co") .SetProjection(Projections.ProjectionList() .Add(Projections.Property("st.Name"), "studentName") .Add(Projections.Property("co.Description"), "courseDescription")) .AddOrder(Order.Desc("studentName")) .SetResultTransformer(NHibernate.Transform.Transformers .AliasToBean(typeof(StudentDTO))) .List();

public class Enrolment
   private Student student;
   public virtual Student Student
        get { return student; }
        set { student = value; }
   private Course course;

   public virtual Course Course
       get { return course; }
       set { course = value; }
   private long studentNumber;

   public virtual long StudentNumber
     get { return studentNumber; }
     set { studentNumber = value; }
    private string courseCode = string.Empty;

    public virtual string CourseCode
      get { return courseCode; }
      set { courseCode = value; }

  private short year;

  public virtual short Year
   get { return year; }
   set { year = value; }

  private short semester;
  public virtual short Semester
   get { return semester; }
   set { semester = value; }

 public override bool Equals(object obj)
   Enrolment that = obj as Enrolment;
   if (that == null)
          return false;
   return studentNumber == that.StudentNumber && courseCode.Equals(that.CourseCode);
 public override int GetHashCode()
     return courseCode.GetHashCode();

public class StudentDTO
    private string studentName;
    private string courseDescription;
    public StudentDTO() { }
    public string Name
        get { return studentName; }
    public string Description
        get { return courseDescription; }

4 thoughts on “Create DTO with NHibernate

  1. Wow, this is awesome. We are using NHibernate with Dtos. We had created factory methods to turn object arrays into Dtos. Talk about some ugly code!!! We will follow this pattern henceforth. Yes it is indeed beautiful. Thanks!!!

  2. I have a question about using DTOs in this fashion. If I wanted to modify the description property on your StudentDTO and send it back to nhibernate, how would I do that?

  3. Still new to NH, but i already see i need DTO given i have sepration where would the classes above live? IList would be in the repository? What about the others?
    BTW i like what you did here. I still need to understand some NH to use it correctly, but thanks!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s