Skip to content

Commit 5901745

Browse files
authored
Merge pull request #1 from DbSyncKit/optimization/code_optimization
Code optimization
2 parents 09f65bb + a7063a2 commit 5901745

4 files changed

Lines changed: 99 additions & 103 deletions

File tree

src/Enum/CachePropertyType.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using DbSyncKit.DB.Manager;
62

73
namespace DbSyncKit.DB.Enum
84
{
@@ -54,7 +50,17 @@ public enum CachePropertyType
5450
/// <summary>
5551
/// Represents properties that are comparable (neither key nor excluded).
5652
/// </summary>
57-
ComparableProperties
53+
ComparableProperties,
54+
55+
/// <summary>
56+
/// Represents properties that are unique.
57+
/// </summary>
58+
KeyProperties,
59+
60+
/// <summary>
61+
/// Represents properties that are excluded.
62+
/// </summary>
63+
ExcludedProperties
5864
}
5965

6066
}

src/Helper/QueryHelper.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public List<string> GetKeyColumns<T>() where T : IDataContractComparer
6868
/// <typeparam name="T">The type for which to get the excluded properties. Must implement <see cref="IDataContractComparer"/>.</typeparam>
6969
/// <returns>A list of excluded property names.</returns>
7070
/// <seealso cref="IDataContractComparer"/>
71-
public List<string> GetExcludedProperties<T>() where T : IDataContractComparer
71+
public List<string> GetExcludedColumns<T>() where T : IDataContractComparer
7272
{
73-
return CacheManager.GetExcludedProperties(typeof(T));
73+
return CacheManager.GetExcludedColumns(typeof(T));
7474
}
7575

7676
/// <summary>
@@ -98,10 +98,29 @@ public List<string> GetIdentityColumns<T>() where T : IDataContractComparer
9898
return CacheManager.GetIdentityColumns(typeof(T));
9999
}
100100

101+
/// <summary>
102+
/// Retrieves an array of <see cref="PropertyInfo"/> objects representing the properties that are used for data comparison
103+
/// in objects of type <typeparamref name="T"/>. These properties are determined based on the implementation of the
104+
/// <see cref="IDataContractComparer"/> interface.
105+
/// </summary>
106+
/// <typeparam name="T">The type of objects for which to retrieve comparable properties.</typeparam>
107+
/// <returns>An array of <see cref="PropertyInfo"/> objects representing the comparable properties of type <typeparamref name="T"/>.</returns>
101108
public PropertyInfo[] GetComparableProperties<T>() where T: IDataContractComparer
102109
{
103110
return CacheManager.GetComparableProperties(typeof(T));
104111
}
105112

113+
/// <summary>
114+
/// Retrieves an array of <see cref="PropertyInfo"/> objects representing the properties that are used as key properties
115+
/// for uniquely identifying objects of type <typeparamref name="T"/>. These key properties are determined based on the
116+
/// implementation of the <see cref="IDataContractComparer"/> interface.
117+
/// </summary>
118+
/// <typeparam name="T">The type of objects for which to retrieve key properties.</typeparam>
119+
/// <returns>An array of <see cref="PropertyInfo"/> objects representing the key properties of type <typeparamref name="T"/>.</returns>
120+
public PropertyInfo[] GetKeyProperties<T>() where T : IDataContractComparer
121+
{
122+
return CacheManager.GetComparableProperties(typeof(T));
123+
}
124+
106125
}
107126
}

src/Manager/CacheManager.cs

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static List<string> GetIdentityColumns(Type type)
6666
identityColumns = CacheManager.GetTypeProperties(type)
6767
.Where(prop =>
6868
Attribute.IsDefined(prop, typeof(KeyPropertyAttribute)) &&
69-
((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))).IsPrimaryKey
69+
((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))!).IsPrimaryKey
7070
).Select(prop => prop.Name).ToList();
7171

7272

@@ -92,7 +92,7 @@ public static List<string> GetKeyColumns(Type type)
9292
keyColumns = GetTypeProperties(type)
9393
.Where(prop =>
9494
Attribute.IsDefined(prop, typeof(KeyPropertyAttribute)) &&
95-
((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))).KeyProperty
95+
((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))!).KeyProperty
9696
).Select(prop => prop.Name).ToList();
9797

9898
typeCaches.Add(nameof(CachePropertyType.Key), keyColumns);
@@ -103,9 +103,9 @@ public static List<string> GetKeyColumns(Type type)
103103
/// <summary>
104104
/// Gets the names of properties marked as excluded properties for a specified type.
105105
/// </summary>
106-
/// <param name="type">The type for which to retrieve excluded properties.</param>
107-
/// <returns>A list of excluded property names.</returns>
108-
public static List<string> GetExcludedProperties(Type type)
106+
/// <param name="type">The type for which to retrieve excluded columns.</param>
107+
/// <returns>A list of excluded column names.</returns>
108+
public static List<string> GetExcludedColumns(Type type)
109109
{
110110
var typeCaches = GetOrCreateDictionary(type);
111111

@@ -114,11 +114,7 @@ public static List<string> GetExcludedProperties(Type type)
114114
return (List<string>)excludedProperties;
115115
}
116116

117-
excludedProperties = GetTypeProperties(type)
118-
.Where(prop =>
119-
Attribute.IsDefined(prop, typeof(ExcludedPropertyAttribute)) &&
120-
((ExcludedPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(ExcludedPropertyAttribute))).Excluded
121-
).Select(prop => prop.Name).ToList();
117+
excludedProperties = GetExcludedProperties(type).Select(prop => prop.Name).ToList();
122118

123119
typeCaches.Add(nameof(CachePropertyType.Excluded), excludedProperties);
124120

@@ -194,9 +190,9 @@ public static string GetTableName(Type type)
194190
else
195191
TableSchema = null;
196192

197-
typeCaches.Add(nameof(CachePropertyType.TableSchema), TableSchema);
193+
typeCaches.Add(nameof(CachePropertyType.TableSchema), TableSchema!);
198194

199-
return (string)TableSchema;
195+
return (string?)TableSchema;
200196
}
201197

202198
/// <summary>
@@ -266,14 +262,67 @@ public static PropertyInfo[] GetComparableProperties(Type type)
266262
}
267263

268264
var keyProps = GetKeyColumns(type);
269-
var excludeProps = GetExcludedProperties(type);
265+
var excludeProps = GetExcludedColumns(type);
270266

271267
_properties = GetTypeProperties(type).Where(prop => !keyProps.Contains(prop.Name) && !excludeProps.Contains(prop.Name)).ToArray();
272268

273269
return (PropertyInfo[])_properties;
274270

275271
}
276272

273+
/// <summary>
274+
/// Gets the properties that are unique for a specified type.
275+
/// </summary>
276+
/// <param name="type">The type for which to retrieve unique properties.</param>
277+
/// <returns>An array of <see cref="PropertyInfo"/> objects representing unique properties of the specified type.</returns>
278+
public static PropertyInfo[] GetKeyProperties(Type type)
279+
{
280+
var typeCaches = GetOrCreateDictionary(type);
281+
282+
if (typeCaches.TryGetValue(nameof(CachePropertyType.KeyProperties), out var _properties))
283+
{
284+
return (PropertyInfo[])_properties;
285+
}
286+
287+
var keyProps = GetKeyColumns(type);
288+
var excludeProps = GetExcludedColumns(type);
289+
290+
_properties = GetTypeProperties(type)
291+
.Where(prop =>
292+
Attribute.IsDefined(prop, typeof(KeyPropertyAttribute)) &&
293+
((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))!).KeyProperty
294+
).ToArray();
295+
296+
return (PropertyInfo[])_properties;
297+
298+
}
299+
300+
/// <summary>
301+
/// Gets the excluded properties for a specified type.
302+
/// </summary>
303+
/// <param name="type">The type for which to retrieve the excluded properties.</param>
304+
/// <returns>An array of <see cref="PropertyInfo"/> representing the excluded properties for the specified type.</returns>
305+
public static PropertyInfo[] GetExcludedProperties(Type type)
306+
{
307+
var typeCaches = GetOrCreateDictionary(type);
308+
309+
if (typeCaches.TryGetValue(nameof(CachePropertyType.ExcludedProperties), out var _properties))
310+
{
311+
return (PropertyInfo[])_properties;
312+
}
313+
314+
var keyProps = GetKeyColumns(type);
315+
316+
_properties = GetTypeProperties(type)
317+
.Where(prop =>
318+
Attribute.IsDefined(prop, typeof(ExcludedPropertyAttribute)) &&
319+
((ExcludedPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(ExcludedPropertyAttribute))!).Excluded
320+
).ToArray();
321+
322+
return (PropertyInfo[])_properties;
323+
324+
}
325+
277326
/// <summary>
278327
/// Disposes of cached data associated with the specified type.
279328
/// </summary>

src/Utils/DataContractUtility.cs

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -12,87 +12,12 @@ namespace DbSyncKit.DB.Utils
1212
public class DataContractUtility<T> : IDataContractComparer where T : IDataContractComparer
1313
{
1414
/// <summary>
15-
/// Calculates the hash code for the object, excluding specified properties.
15+
/// Overrides the default GetHashCode method to provide a custom hash code for the object.
1616
/// </summary>
17-
/// <param name="excludedProperties">List of property names to exclude from hash code calculation.</param>
18-
/// <returns>The calculated hash code.</returns>
19-
public int GetHashCode(List<string> excludedProperties)
17+
/// <returns>The hash code of the object.</returns>
18+
public override int GetHashCode()
2019
{
21-
// Get all properties of the Entity class
22-
var properties = typeof(T).GetProperties();
23-
24-
// Filter out properties that are in the exclusion list
25-
var filteredProperties = properties
26-
.Where(prop => !excludedProperties.Contains(prop.Name));
27-
28-
// Calculate the hash code using XOR
29-
int hashCode = base.GetHashCode();
30-
foreach (var prop in filteredProperties)
31-
{
32-
var value = prop.GetValue(this);
33-
if (value != null)
34-
{
35-
hashCode ^= value.GetHashCode();
36-
}
37-
}
38-
39-
return hashCode;
40-
}
41-
42-
/// <summary>
43-
/// Generates a string representation of the object, excluding specified properties.
44-
/// </summary>
45-
/// <param name="excludedProperties">List of property names to exclude from the string representation.</param>
46-
/// <returns>The string representation of the object.</returns>
47-
public string ToString(List<string> excludedProperties)
48-
{
49-
// Get all properties of the Entity class
50-
var properties = typeof(T).GetProperties();
51-
52-
// Filter out properties that are in the exclusion list
53-
var filteredProperties = properties
54-
.Where(prop => !excludedProperties.Contains(prop.Name));
55-
56-
// Create a string representation
57-
StringBuilder stringBuilder = new StringBuilder(base.ToString());
58-
foreach (var prop in filteredProperties)
59-
{
60-
var value = prop.GetValue(this);
61-
stringBuilder.Append($",\t{prop.Name}: {value}");
62-
}
63-
64-
return stringBuilder.ToString();
65-
}
66-
67-
/// <summary>
68-
/// Checks if the current object is equal to another object, considering specified properties.
69-
/// </summary>
70-
/// <param name="obj">The object to compare.</param>
71-
/// <param name="listOfProperties">List of properties to consider for equality check.</param>
72-
/// <param name="isExcluded">If true, specified properties are excluded; otherwise, they are included.</param>
73-
/// <returns>True if the objects are equal; otherwise, false.</returns>
74-
public bool Equals(T obj, List<string> listOfProperties, bool isExcluded = true)
75-
{
76-
if (obj == null || GetType() != obj.GetType())
77-
return false;
78-
79-
var props = GetType().GetProperties();
80-
81-
if (isExcluded)
82-
props = props.Where(prop => !listOfProperties.Contains(prop.Name)).ToArray();
83-
else
84-
props = props.Where(prop => listOfProperties.Contains(prop.Name)).ToArray();
85-
86-
87-
foreach (var prop in props)
88-
{
89-
if (!EqualityComparer<object?>.Default.Equals(prop.GetValue(this), prop.GetValue(obj)))
90-
{
91-
return false;
92-
}
93-
}
94-
95-
return true;
20+
return base.GetHashCode();
9621
}
9722

9823
/// <summary>
@@ -105,9 +30,6 @@ public override bool Equals(object? obj)
10530
if (obj == null || GetType() != obj.GetType())
10631
return false;
10732

108-
var listOfExcluded = CacheManager.GetExcludedProperties(GetType());
109-
var listOfKey = CacheManager.GetKeyColumns(GetType());
110-
11133
var props = CacheManager.GetComparableProperties(GetType());
11234

11335
foreach (var prop in props)

0 commit comments

Comments
 (0)