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:

   1:  IList resultWithAliasedBean = s.CreateCriteria(typeof(Enrolment))
   2:   .CreateAlias("Student", "st")
   3:   .CreateAlias("Course", "co")
   4:   .SetProjection(Projections.ProjectionList()
   5:   .Add(Projections.Property("st.Name"), "studentName")
   6:   .Add(Projections.Property("co.Description"), "courseDescription")
   7:  )
   8:  .AddOrder(Order.Desc("studentName"))
   9:  .SetResultTransformer(NHibernate.Transform.Transformers
  10.  .AliasToBean(typeof(StudentDTO)))
  11:  .List();

The Element and StudentDTO is simple enough.

   1:          [Serializable]
   2:      public class Enrolment
   3:  
   4:          private Student student;
   5:          public virtual Student Student
   6:          {
   7:              get { return student; }
   8:              set { student = value; }
   9:          }
  10:  
  11:          private Course course;
  12:          public virtual Course Course
  13:          {
  14:              get { return course; }
  15:              set { course = value; }
  16:          }
  17:  
  18:          private long studentNumber;
  19:          public virtual long StudentNumber
  20:          {
  21:              get { return studentNumber; }
  22:              set { studentNumber = value; }
  23:          }
  24:  
  25:          private string courseCode = string.Empty;
  26:          public virtual string CourseCode
  27:          {
  28:              get { return courseCode; }
  29:              set { courseCode = value; }
  30:          }
  31:  
  32:          private short year;
  33:          public virtual short Year
  34:          {
  35:              get { return year; }
  36:              set { year = value; }
  37:          }
  38:  
  39:          private short semester;
  40:          public virtual short Semester
  41:          {
  42:              get { return semester; }
  43:              set { semester = value; }
  44:          }
  45:  
  46:          public override bool Equals(object obj)
  47:          {
  48:              Enrolment that = obj as Enrolment;
  49:              if (that == null)
  50:                  return false;
  51:              return studentNumber == that.StudentNumber && courseCode.Equals(that.CourseCode);
  52:          }
  53:  
  54:          public override int GetHashCode()
  55:          {
  56:              return courseCode.GetHashCode();
  57:          }
  58:      }
  59:  public class StudentDTO
  60:      {
  61:          private string studentName;
  62:          private string courseDescription;
  63:  
  64:          public StudentDTO() { }
  65:  
  66:          public string Name
  67:          {
  68:              get { return studentName; }
  69:          }
  70:  
  71:          public string Description
  72:          {
  73:              get { return courseDescription; }
  74:          }
  75:      }

What we can do is to retrieve StudentDTO from Presenter by querying from Repository. Code is elegant and beautiful. How sexy NHibernate is!!

2 Comments

  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?


One Trackback/Pingback

Post a Comment

*
*