Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 13th, 2012  |  syntax: None  |  size: 2.00 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Data;
  3. using NHibernate;
  4. using NHibernate.SqlTypes;
  5. using NHibernate.UserTypes;
  6.  
  7. public class BinaryGuidType : IUserType
  8. {
  9.     private static readonly int[] ByteOrder = new[] { 10,11,12,13,14,15,8,9,6,7,4,5,0,1,2,3 };
  10.     private static readonly SqlType[] Types = new[] { new SqlType(DbType.Binary) };
  11.  
  12.     public SqlType[] SqlTypes
  13.     {
  14.         get { return Types; }
  15.     }
  16.  
  17.     public Type ReturnedType
  18.     {
  19.         get { return typeof(Guid); }
  20.     }
  21.  
  22.     public new bool Equals(object x, object y)
  23.     {
  24.         return (x != null && x.Equals(y));
  25.     }
  26.  
  27.     public int GetHashCode(object x)
  28.     {
  29.         return x.GetHashCode();
  30.     }
  31.  
  32.     public object NullSafeGet(IDataReader rs, string[] names, object owner)
  33.     {
  34.         var bytes = (byte[]) NHibernateUtil.Binary.NullSafeGet(rs, names[0]);
  35.         if (bytes == null || bytes.Length == 0) return null;
  36.  
  37.         var reorderedBytes = new byte[16];
  38.  
  39.         for (var i = 0 ; i < 16; i++)
  40.         {
  41.             reorderedBytes[ByteOrder[i]] = bytes[i];
  42.         }
  43.  
  44.         return new Guid(reorderedBytes);
  45.     }
  46.  
  47.     public void NullSafeSet(IDbCommand cmd, object value, int index)
  48.     {
  49.         if (null != value)
  50.         {
  51.             var bytes = ((Guid) value).ToByteArray();
  52.             var reorderedBytes = new byte[16];
  53.  
  54.             for (var i = 0; i < 16; i++)
  55.             {
  56.                 reorderedBytes[i] = bytes[ByteOrder[i]];
  57.             }
  58.  
  59.             NHibernateUtil.Binary.NullSafeSet(cmd, reorderedBytes, index);
  60.         }
  61.         else
  62.         {
  63.             NHibernateUtil.Binary.NullSafeSet(cmd, null, index);
  64.         }
  65.     }
  66.  
  67.     public object DeepCopy(object value)
  68.     {
  69.         return value;
  70.     }
  71.  
  72.     public bool IsMutable
  73.     {
  74.         get { return false; }
  75.     }
  76.  
  77.     public object Replace(object original, object target, object owner)
  78.     {
  79.         return original;
  80.     }
  81.  
  82.     public object Assemble(object cached, object owner)
  83.     {
  84.         return cached;
  85.     }
  86.  
  87.     public object Disassemble(object value)
  88.     {
  89.         return value;
  90.     }
  91. }