1: using System;
2: using System.Collections.Generic;
3: using System.Data;
4: using System.Text;
5: using NHibernate;
6: using NHibernate.SqlTypes;
7: using NHibernate.UserTypes;
8:
9: namespace MySolution.Data.Types
10: {
11: public class RawType : IUserType
12: {
13: public static string GenerateNewId()
14: {
15: Guid nextVal = Guid.NewGuid();
16: return nextVal.ToString("N");
17: }
18: private static SqlType[] types = new SqlType[]
19: { new SqlType(DbType.Binary) };
20:
21: public object Assemble(object cached, object owner)
22: {
23: return DeepCopy(cached);
24: }
25:
26: public new bool Equals(object x, object y)
27: {
28: return (x == null ? false : x.Equals(y));
29: }
30:
31: public object DeepCopy(object value)
32: {
33: return value;
34: }
35:
36: public object Disassemble(object value)
37: {
38: return DeepCopy(value);
39: }
40:
41: public int GetHashCode(object x)
42: {
43: return x.GetHashCode();
44: }
45:
46: public bool IsMutable
47: {
48: get { return true; }
49: }
50:
51: public object NullSafeGet(IDataReader rs, string[] names, object owner)
52: {
53: string result = null;
54: byte[] buffer = (byte[])NHibernateUtil.Binary.NullSafeGet(rs, names[0]);
55: if (null != buffer)
56: {
57: result = new Guid(buffer).ToString("N");
58: Array.Clear(buffer, 0, buffer.Length);
59: }
60: return result;
61: }
62:
63: public void NullSafeSet(IDbCommand cmd, object value, int index)
64: {
65: if (null != value)
66: {
67: byte[] buffer = new Guid(((string)value)).ToByteArray();
68: NHibernateUtil.Binary.NullSafeSet(cmd, buffer, index);
69: Array.Clear(buffer, 0, buffer.Length);
70: }
71: }
72:
73: public object Replace(object original, object target, object owner)
74: {
75: return original;
76: }
77:
78: public Type ReturnedType
79: {
80: get { return typeof(string); }
81: }
82:
83: public SqlType[] SqlTypes
84: {
85: get { return types; }
86: }
87: }
88: }