CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

NHibernate XmlType

498 Views
Copy Code Show/Hide Line Numbers
   1:  public class XmlType : IUserType
   2:  {
   3:      public new bool Equals(object x, object y)
   4:      {
   5:          if (x == null || y == null)
   6:              return false;
   7:   
   8:          var xdoc_x = (XmlDocument)x;
   9:          var xdoc_y = (XmlDocument)y;
  10:          return xdoc_y.OuterXml == xdoc_x.OuterXml;
  11:      }
  12:   
  13:      public int GetHashCode(object x)
  14:      {
  15:          return x.GetHashCode();
  16:      }
  17:   
  18:   
  19:      public object NullSafeGet(IDataReader rs, string[] names, object owner)
  20:      {
  21:          if (names.Length != 1)
  22:              throw new InvalidOperationException("names array has more than one element. can't handle this!");
  23:   
  24:          var document = new XmlDocument();
  25:   
  26:          var val = rs[names[0]] as string;
  27:   
  28:          if (val != null)
  29:          {
  30:              document.LoadXml(val);
  31:              return document;
  32:          }
  33:   
  34:          return null;
  35:      }
  36:   
  37:      public void NullSafeSet(IDbCommand cmd, object value, int index)
  38:      {
  39:          var parameter = (DbParameter)cmd.Parameters[index];
  40:   
  41:          if (value == null)
  42:          {
  43:              parameter.Value = DBNull.Value;
  44:              return;
  45:          }
  46:   
  47:          parameter.Value = ((XmlDocument)value).OuterXml;
  48:      }
  49:   
  50:      public object DeepCopy(object value)
  51:      {
  52:   
  53:          var toCopy = value as XmlDocument;
  54:   
  55:          if (toCopy == null)
  56:              return null;
  57:   
  58:          var copy = new XmlDocument();
  59:          copy.LoadXml(toCopy.OuterXml);
  60:          return copy;
  61:      }
  62:   
  63:      public object Replace(object original, object target, object owner)
  64:      {
  65:          throw new NotImplementedException();
  66:      }
  67:   
  68:      public object Assemble(object cached, object owner)
  69:      {
  70:          var str = cached as string;
  71:          if (str != null)
  72:          {
  73:              var doc = new XmlDocument();
  74:              doc.LoadXml(str);
  75:              return doc;
  76:          }
  77:          else
  78:          {
  79:              return null;
  80:          }
  81:   
  82:      }
  83:   
  84:      public object Disassemble(object value)
  85:      {
  86:          var val = value as XmlDocument;
  87:          if (val != null)
  88:          {
  89:              return val.OuterXml;
  90:          }
  91:          else
  92:          {
  93:              return null;
  94:          }
  95:      }
  96:   
  97:      public SqlType[] SqlTypes
  98:      {
  99:          get
 100:          {
 101:              return new SqlType[] { new SqlXmlType() };
 102:          }
 103:      }
 104:   
 105:      public Type ReturnedType
 106:      {
 107:          get { return typeof(XmlDocument); }
 108:      }
 109:   
 110:      public bool IsMutable
 111:      {
 112:          get { return true; }
 113:      }
 114:  }
 115:   
 116:  public class SqlXmlType : SqlType
 117:  {
 118:      public SqlXmlType()
 119:          : base(DbType.Xml)
 120:      {
 121:      }
 122:  }
by Mikael Henriksson
  September 23, 2009 @ 1:17pm
Tags:
Description:
Use with the patch from http://nhjira.koah.net/secure/attachment/12904/SqlXmlType.patch

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate