5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
28 using System.Collections.Generic;
\r
31 namespace System.Data.Linq.Identity
\r
33 namespace DbLinq.Data.Linq.Identity
\r
37 /// Identifies an object in a unique way (think Primay Keys in a database table)
\r
42 /// Example: to store Product with ProductID=1, we use the following IdentityKey:
\r
43 /// IdentityKey{Type=Product, Keys={1}}
\r
50 sealed class IdentityKey
\r
55 public Type Type { get; private set; }
\r
59 public IList<object> Keys { get; private set; }
\r
61 private readonly int hashCode;
\r
64 /// Determines equality between two refs
\r
66 /// <param name="obj"></param>
\r
67 /// <returns></returns>
\r
68 public override bool Equals(object obj)
\r
70 var other = (IdentityKey)obj;
\r
71 if (Type != other.Type)
\r
73 if (Keys.Count != other.Keys.Count)
\r
75 for (int keyIndex = 0; keyIndex < Keys.Count; keyIndex++)
\r
77 if (!Equals(Keys[keyIndex], other.Keys[keyIndex]))
\r
84 /// Computes hash code
\r
86 /// <returns></returns>
\r
87 public override int GetHashCode()
\r
93 /// Initializes a new instance of the <see cref="IdentityKey"/> class.
\r
95 /// <param name="type">The type.</param>
\r
96 /// <param name="keys">The keys.</param>
\r
97 public IdentityKey(Type type, IEnumerable<object> keys)
\r
100 Keys = new List<object>(keys);
\r
102 // Done here becouse IdentityKeys exists to be keys in dictionaries...
\r
103 hashCode = type.GetHashCode();
\r
104 foreach (object key in keys)
\r
106 hashCode ^= key.GetHashCode();
\r
111 /// Initializes a new instance of the <see cref="IdentityKey"/> class.
\r
113 /// <param name="type">The type.</param>
\r
114 /// <param name="keys">The keys.</param>
\r
115 public IdentityKey(Type type, params object[] keys)
\r
118 Keys = new List<object>(keys);
\r
120 // Done here becouse IdentityKeys exists to be keys in dictionaries...
\r
121 hashCode = Type.GetHashCode();
\r
122 foreach (object key in Keys)
\r
124 hashCode ^= key.GetHashCode();
\r