Skip to content

Commit 2e69a39

Browse files
authored
Merge pull request #1032 from mikependon/repodb-fixes-1031
#1031 - hashcode generation refactoring.
2 parents 014752b + d0da5b0 commit 2e69a39

40 files changed

+149
-163
lines changed

RepoDb.Core/RepoDb/Attributes/Parameter/PropertyValueAttribute.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,30 +177,30 @@ public override int GetHashCode()
177177
// FullName: This is to ensure that even the user has created an identical formatting
178178
// on the derived class with the existing classes, the Type.FullName could still
179179
// differentiate the instances
180-
var hashCode = GetType().FullName.GetHashCode();
180+
var hashCode = HashCode.Combine(GetType().FullName);
181181

182182
// Base
183-
hashCode += base.GetHashCode();
183+
hashCode = HashCode.Combine(hashCode, base.GetHashCode());
184184

185185
// PropertyName
186186
if (PropertyName != null)
187187
{
188-
hashCode += PropertyName.GetHashCode();
188+
hashCode = HashCode.Combine(hashCode, PropertyName);
189189
}
190190

191191
// ParameterType
192192
if (ParameterType != null)
193193
{
194-
hashCode += ParameterType.GetHashCode();
194+
hashCode = HashCode.Combine(hashCode, ParameterType);
195195
}
196196

197197
// IncludedInCompilation
198-
hashCode += IncludedInCompilation.GetHashCode();
198+
hashCode = HashCode.Combine(hashCode, IncludedInCompilation);
199199

200200
// Value
201201
if (Value != null)
202202
{
203-
hashCode += Value.GetHashCode();
203+
hashCode = HashCode.Combine(hashCode, Value);
204204
}
205205

206206
// Return

RepoDb.Core/RepoDb/ClassProperty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ public IEnumerable<PropertyValueAttribute> GetPropertyValueAttributes()
329329
/// </summary>
330330
/// <returns>The hash code value.</returns>
331331
public override int GetHashCode() =>
332-
GetDeclaringType().GetHashCode() ^ PropertyInfo.GenerateCustomizedHashCode(GetDeclaringType());
332+
HashCode.Combine(GetDeclaringType(), PropertyInfo.GenerateCustomizedHashCode(GetDeclaringType()));
333333

334334
/// <summary>
335335
/// Compare the current instance to the other object instance.

RepoDb.Core/RepoDb/DbField.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,33 +137,32 @@ public override int GetHashCode()
137137
return this.hashCode.Value;
138138
}
139139

140-
var hashCode = 0;
141-
142140
// Set the hashcode
143-
hashCode = Name.GetHashCode() + IsPrimary.GetHashCode() + IsIdentity.GetHashCode() + IsNullable.GetHashCode();
141+
var hashCode = HashCode.Combine(Name, IsPrimary, IsIdentity, IsNullable);
142+
144143
if (Type != null)
145144
{
146-
hashCode += Type.GetHashCode();
145+
hashCode = HashCode.Combine(hashCode, Type);
147146
}
148147
if (Size != null)
149148
{
150-
hashCode += Size.GetHashCode();
149+
hashCode = HashCode.Combine(hashCode, Size);
151150
}
152151
if (Precision != null)
153152
{
154-
hashCode += Precision.GetHashCode();
153+
hashCode = HashCode.Combine(hashCode, Precision);
155154
}
156155
if (Scale != null)
157156
{
158-
hashCode += Scale.GetHashCode();
157+
hashCode = HashCode.Combine(hashCode, Scale);
159158
}
160159
if (DatabaseType != null)
161160
{
162-
hashCode += DatabaseType.GetHashCode();
161+
hashCode = HashCode.Combine(hashCode, DatabaseType);
163162
}
164163
if (Provider != null)
165164
{
166-
hashCode += Provider.GetHashCode();
165+
hashCode = HashCode.Combine(hashCode, Provider);
167166
}
168167

169168
// Set and return the hashcode

RepoDb.Core/RepoDb/DbSettings/BaseDbSetting.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,51 +119,51 @@ public override int GetHashCode()
119119
var hashCode = 0;
120120

121121
// AreTableHintsSupported
122-
hashCode += AreTableHintsSupported.GetHashCode();
122+
hashCode = HashCode.Combine(hashCode, AreTableHintsSupported);
123123

124124
// ClosingQuote
125125
if (!string.IsNullOrWhiteSpace(ClosingQuote))
126126
{
127-
hashCode += ClosingQuote.GetHashCode();
127+
hashCode = HashCode.Combine(hashCode, ClosingQuote);
128128
}
129129

130130
// DefaultAverageableType
131131
if (AverageableType != null)
132132
{
133-
hashCode += AverageableType.GetHashCode();
133+
hashCode = HashCode.Combine(hashCode, AverageableType);
134134
}
135135

136136
// DefaultSchema
137137
if (!string.IsNullOrWhiteSpace(DefaultSchema))
138138
{
139-
hashCode += DefaultSchema.GetHashCode();
139+
hashCode = HashCode.Combine(hashCode, DefaultSchema);
140140
}
141141

142-
// IsDbParameterDirectionSettingSupported
143-
hashCode += IsDirectionSupported.GetHashCode();
142+
// IsDirectionSupported
143+
hashCode = HashCode.Combine(hashCode, IsDirectionSupported);
144144

145-
// IsDisposeDbCommandAfterExecuteReader
146-
hashCode += IsExecuteReaderDisposable.GetHashCode();
145+
// IsExecuteReaderDisposable
146+
hashCode = HashCode.Combine(hashCode, IsExecuteReaderDisposable);
147147

148-
// IsMultipleStatementExecutionSupported
149-
hashCode += IsMultiStatementExecutable.GetHashCode();
148+
// IsMultiStatementExecutable
149+
hashCode = HashCode.Combine(hashCode, IsMultiStatementExecutable);
150150

151151
// IsPreparable
152-
hashCode += IsPreparable.GetHashCode();
152+
hashCode = HashCode.Combine(hashCode, IsPreparable);
153153

154-
// IsUseUpsertForMergeOperation
155-
hashCode += IsUseUpsert.GetHashCode();
154+
// IsUseUpsert
155+
hashCode = HashCode.Combine(hashCode, IsUseUpsert);
156156

157157
// OpeningQuote
158158
if (!string.IsNullOrWhiteSpace(OpeningQuote))
159159
{
160-
hashCode += OpeningQuote.GetHashCode();
160+
hashCode = HashCode.Combine(hashCode, OpeningQuote);
161161
}
162162

163163
// ParameterPrefix
164164
if (!string.IsNullOrWhiteSpace(ParameterPrefix))
165165
{
166-
hashCode += ParameterPrefix.GetHashCode();
166+
hashCode = HashCode.Combine(hashCode, ParameterPrefix);
167167
}
168168

169169
// Set and return the hashcode

RepoDb.Core/RepoDb/DirectionalQueryField.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,21 +283,21 @@ public override int GetHashCode()
283283
var hashCode = 0;
284284

285285
// Get the hashcode of the base query field
286-
hashCode += base.GetHashCode();
286+
hashCode = HashCode.Combine(hashCode, base.GetHashCode());
287287

288288
// Add the parameter direction
289-
hashCode += Direction.GetHashCode();
289+
hashCode = HashCode.Combine(hashCode, Direction);
290290

291291
// Add the type
292292
if (Type != null)
293293
{
294-
hashCode += Type.GetHashCode();
294+
hashCode = HashCode.Combine(hashCode, Type);
295295
}
296296

297297
// Add the size
298298
if (Size.HasValue)
299299
{
300-
hashCode += Size.Value.GetHashCode();
300+
hashCode = HashCode.Combine(hashCode, Size.Value);
301301
}
302302

303303
// Set and return the hashcode

RepoDb.Core/RepoDb/Extensions/PropertyInfoExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ internal static string AsFieldAndAliasField(this PropertyInfo property,
148148
/// <returns>The generated hashcode.</returns>
149149
internal static int GenerateCustomizedHashCode(this PropertyInfo property,
150150
Type declaringType) =>
151-
(declaringType ?? property.DeclaringType).GetHashCode() ^ property.Name.GetHashCode() ^ property.PropertyType.GetHashCode();
151+
HashCode.Combine((declaringType ?? property.DeclaringType), property.Name, property.PropertyType);
152152

153153
/// <summary>
154154
/// Converts an instance of <see cref="PropertyInfo"/> object into <see cref="Field"/> object.

RepoDb.Core/RepoDb/Extensions/QueryFields/FunctionalQueryField.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ public override int GetHashCode()
110110
var hashCode = GetType().FullName.GetHashCode();
111111

112112
// Base
113-
hashCode += base.GetHashCode();
113+
hashCode = HashCode.Combine(hashCode, base.GetHashCode());
114114

115115
// Format
116116
if (Format != null)
117117
{
118-
hashCode += Format.GetHashCode();
118+
hashCode = HashCode.Combine(hashCode, Format);
119119
}
120120

121121
// Return

RepoDb.Core/RepoDb/Extensions/QueryFields/LeftQueryField.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using RepoDb.Enumerations;
2+
using System;
23

34
namespace RepoDb.Extensions.QueryFields
45
{
@@ -96,7 +97,7 @@ public override int GetHashCode()
9697
var hashCode = base.GetHashCode();
9798

9899
// CharCount
99-
hashCode += CharCount.GetHashCode();
100+
hashCode = HashCode.Combine(hashCode, CharCount);
100101

101102
// Return
102103
return (this.hashCode = hashCode).Value;

RepoDb.Core/RepoDb/Extensions/QueryFields/RightQueryField.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using RepoDb.Enumerations;
2+
using System;
23

34
namespace RepoDb.Extensions.QueryFields
45
{
@@ -96,7 +97,7 @@ public override int GetHashCode()
9697
var hashCode = base.GetHashCode();
9798

9899
// CharCount
99-
hashCode += CharCount.GetHashCode();
100+
hashCode = HashCode.Combine(hashCode, CharCount);
100101

101102
// Return
102103
return (this.hashCode = hashCode).Value;

RepoDb.Core/RepoDb/Field.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,12 @@ public override int GetHashCode()
276276
return this.hashCode.Value;
277277
}
278278

279-
var hashCode = 0;
279+
var hashCode = Name.GetHashCode();
280280

281281
// Set the hash code
282-
hashCode = Name.GetHashCode();
283282
if (Type != null)
284283
{
285-
hashCode += Type.GetHashCode();
284+
hashCode = HashCode.Combine(hashCode, Type);
286285
}
287286

288287
// Set and return the hashcode
@@ -297,7 +296,7 @@ public override int GetHashCode()
297296
public override bool Equals(object obj)
298297
{
299298
if (obj is null) return false;
300-
299+
301300
return obj.GetHashCode() == GetHashCode();
302301
}
303302

@@ -309,7 +308,7 @@ public override bool Equals(object obj)
309308
public bool Equals(Field other)
310309
{
311310
if (other is null) return false;
312-
311+
313312
return other.GetHashCode() == GetHashCode();
314313
}
315314

0 commit comments

Comments
 (0)