Tuesday, May 6, 2014

IHydratable is baked into DotNetNuke and it provides a way to read data into an object collection in sort of an ORM way.

using System;
using DotNetNuke.Entities.Modules;
namespace Whatver
{
   public class Foo : IHydratable
   {
      public int FooId { get; set; }
      public string Bar { get; set; }
      public bool IsBaz { get; set; }
      public string Qux { get; set; }
      
      public void Fill(System.Data.IDataReader dr)
      {
         FooId = Convert.ToInt32(dr[DALColumnNames.Foo_Id]);
         Bar = dr[DALColumnNames.Bar].ToString();
         IsBaz = !(dr[DALColumnNames.Is_Baz] is DBNull) &&
               Convert.ToBoolean(dr[DALColumnNames.Is_Baz]);
         Qux = dr[DALColumnNames.Qux] is DBNull ?
               null : dr[DALColumnNames.Qux].ToString();
      }
      
      public int KeyID
      {
         get
         {
            return DataMapID;
         }
         set
         {
            DataMapID = value;
         }
      }
   }
}

 
 

You must implement a Fill and KeyID. We do nothing with the KeyID where I work, but the Fill denotes what columns coming back from a sproc should be mapped to what fields on an implementation. If you don't use the Fill, there is another way to map with Hydratable via reflection, but it...

  1. comes with the performance hit you'd expect from reflection
  2. requires that there be no discrepancies between column names and field names

No comments:

Post a Comment