140
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Data;
|
|
4 using System.Linq;
|
|
5 using NHibernate.SqlTypes;
|
|
6 using NHibernate.UserTypes;
|
|
7
|
|
8 namespace Agendas.NHibernate
|
|
9 {
|
|
10 [Serializable]
|
|
11 public abstract class GenericWellKnownInstanceType<T, TId> : IUserType where T : class
|
|
12 {
|
|
13 //private static readonly SqlType[] ReturnSqlTypes = { SqlTypeFactory.Int32 };
|
|
14 private Func<T, TId, bool> findPredicate;
|
|
15 private Func<T, TId> idGetter;
|
|
16 private IEnumerable<T> repository;
|
|
17
|
|
18 /// <summary>
|
|
19 /// Base constructor
|
|
20 /// </summary>
|
|
21 /// <param name="repository">The collection that represent a in-memory repository.</param>
|
|
22 /// <param name="findPredicate">The predicate an instance by the persisted value.</param>
|
|
23 /// <param name="idGetter">The getter of the persisted value.</param>
|
|
24 protected GenericWellKnownInstanceType(IEnumerable<T> repository, Func<T, TId, bool> findPredicate, Func<T, TId> idGetter)
|
|
25 {
|
|
26 this.repository = repository;
|
|
27 this.findPredicate = findPredicate;
|
|
28 this.idGetter = idGetter;
|
|
29 }
|
|
30
|
|
31 //public SqlType[] SqlTypes
|
|
32 //{
|
|
33 // get { return ReturnSqlTypes; }
|
|
34 //}
|
|
35
|
|
36 public Type ReturnedType
|
|
37 {
|
|
38 get { return typeof(T); }
|
|
39 }
|
|
40
|
|
41 public bool IsMutable
|
|
42 {
|
|
43 get { return false; }
|
|
44 }
|
|
45
|
|
46 public new bool Equals(object x, object y)
|
|
47 {
|
|
48 if (ReferenceEquals(x, y))
|
|
49 {
|
|
50 return true;
|
|
51 }
|
|
52 if (ReferenceEquals(null, x) || ReferenceEquals(null, y))
|
|
53 {
|
|
54 return false;
|
|
55 }
|
|
56
|
|
57 return x.Equals(y);
|
|
58 }
|
|
59
|
|
60 public int GetHashCode(object x)
|
|
61 {
|
|
62 return (x == null) ? 0 : x.GetHashCode();
|
|
63 }
|
|
64
|
|
65 public object NullSafeGet(IDataReader rs, string[] names, object owner)
|
|
66 {
|
|
67 int index0 = rs.GetOrdinal(names[0]);
|
|
68 if (rs.IsDBNull(index0))
|
|
69 {
|
|
70 return null;
|
|
71 }
|
|
72
|
|
73 var value = (TId)rs.GetValue(index0);
|
|
74 return repository.FirstOrDefault(x => findPredicate(x, value));
|
|
75 }
|
|
76
|
|
77 public void NullSafeSet(IDbCommand cmd, object value, int index)
|
|
78 {
|
|
79 if (value == null)
|
|
80 {
|
|
81 ((IDbDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
|
|
82 }
|
|
83 else
|
|
84 {
|
|
85 ((IDbDataParameter)cmd.Parameters[index]).Value = idGetter((T)value);
|
|
86 }
|
|
87 }
|
|
88
|
|
89 public object DeepCopy(object value)
|
|
90 {
|
|
91 return value;
|
|
92 }
|
|
93
|
|
94 public object Replace(object original, object target, object owner)
|
|
95 {
|
|
96 return original;
|
|
97 }
|
|
98
|
|
99 public object Assemble(object cached, object owner)
|
|
100 {
|
|
101 return cached;
|
|
102 }
|
|
103
|
|
104 public object Disassemble(object value)
|
|
105 {
|
|
106 return value;
|
|
107 }
|
|
108
|
|
109 /// <summary>
|
|
110 /// The SQL types for the columns mapped by this type.
|
|
111 /// </summary>
|
|
112 public abstract SqlType[] SqlTypes { get; }
|
|
113 }
|
|
114 } |