From a7063a2421d5b75545ce984643661ee9a76bce03 Mon Sep 17 00:00:00 2001 From: RohitM-IN Date: Sun, 31 Dec 2023 13:02:18 +0530 Subject: [PATCH] Code optimizate --- src/Enum/CachePropertyType.cs | 18 ++++--- src/Helper/QueryHelper.cs | 23 ++++++++- src/Manager/CacheManager.cs | 75 +++++++++++++++++++++++----- src/Utils/DataContractUtility.cs | 86 ++------------------------------ 4 files changed, 99 insertions(+), 103 deletions(-) diff --git a/src/Enum/CachePropertyType.cs b/src/Enum/CachePropertyType.cs index 8b12e41..dd488c1 100644 --- a/src/Enum/CachePropertyType.cs +++ b/src/Enum/CachePropertyType.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using DbSyncKit.DB.Manager; namespace DbSyncKit.DB.Enum { @@ -54,7 +50,17 @@ public enum CachePropertyType /// /// Represents properties that are comparable (neither key nor excluded). /// - ComparableProperties + ComparableProperties, + + /// + /// Represents properties that are unique. + /// + KeyProperties, + + /// + /// Represents properties that are excluded. + /// + ExcludedProperties } } diff --git a/src/Helper/QueryHelper.cs b/src/Helper/QueryHelper.cs index 04d07b6..202dc48 100644 --- a/src/Helper/QueryHelper.cs +++ b/src/Helper/QueryHelper.cs @@ -68,9 +68,9 @@ public List GetKeyColumns() where T : IDataContractComparer /// The type for which to get the excluded properties. Must implement . /// A list of excluded property names. /// - public List GetExcludedProperties() where T : IDataContractComparer + public List GetExcludedColumns() where T : IDataContractComparer { - return CacheManager.GetExcludedProperties(typeof(T)); + return CacheManager.GetExcludedColumns(typeof(T)); } /// @@ -98,10 +98,29 @@ public List GetIdentityColumns() where T : IDataContractComparer return CacheManager.GetIdentityColumns(typeof(T)); } + /// + /// Retrieves an array of objects representing the properties that are used for data comparison + /// in objects of type . These properties are determined based on the implementation of the + /// interface. + /// + /// The type of objects for which to retrieve comparable properties. + /// An array of objects representing the comparable properties of type . public PropertyInfo[] GetComparableProperties() where T: IDataContractComparer { return CacheManager.GetComparableProperties(typeof(T)); } + /// + /// Retrieves an array of objects representing the properties that are used as key properties + /// for uniquely identifying objects of type . These key properties are determined based on the + /// implementation of the interface. + /// + /// The type of objects for which to retrieve key properties. + /// An array of objects representing the key properties of type . + public PropertyInfo[] GetKeyProperties() where T : IDataContractComparer + { + return CacheManager.GetComparableProperties(typeof(T)); + } + } } diff --git a/src/Manager/CacheManager.cs b/src/Manager/CacheManager.cs index 8dbf0ac..e3a93fe 100644 --- a/src/Manager/CacheManager.cs +++ b/src/Manager/CacheManager.cs @@ -66,7 +66,7 @@ public static List GetIdentityColumns(Type type) identityColumns = CacheManager.GetTypeProperties(type) .Where(prop => Attribute.IsDefined(prop, typeof(KeyPropertyAttribute)) && - ((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))).IsPrimaryKey + ((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))!).IsPrimaryKey ).Select(prop => prop.Name).ToList(); @@ -92,7 +92,7 @@ public static List GetKeyColumns(Type type) keyColumns = GetTypeProperties(type) .Where(prop => Attribute.IsDefined(prop, typeof(KeyPropertyAttribute)) && - ((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))).KeyProperty + ((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))!).KeyProperty ).Select(prop => prop.Name).ToList(); typeCaches.Add(nameof(CachePropertyType.Key), keyColumns); @@ -103,9 +103,9 @@ public static List GetKeyColumns(Type type) /// /// Gets the names of properties marked as excluded properties for a specified type. /// - /// The type for which to retrieve excluded properties. - /// A list of excluded property names. - public static List GetExcludedProperties(Type type) + /// The type for which to retrieve excluded columns. + /// A list of excluded column names. + public static List GetExcludedColumns(Type type) { var typeCaches = GetOrCreateDictionary(type); @@ -114,11 +114,7 @@ public static List GetExcludedProperties(Type type) return (List)excludedProperties; } - excludedProperties = GetTypeProperties(type) - .Where(prop => - Attribute.IsDefined(prop, typeof(ExcludedPropertyAttribute)) && - ((ExcludedPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(ExcludedPropertyAttribute))).Excluded - ).Select(prop => prop.Name).ToList(); + excludedProperties = GetExcludedProperties(type).Select(prop => prop.Name).ToList(); typeCaches.Add(nameof(CachePropertyType.Excluded), excludedProperties); @@ -194,9 +190,9 @@ public static string GetTableName(Type type) else TableSchema = null; - typeCaches.Add(nameof(CachePropertyType.TableSchema), TableSchema); + typeCaches.Add(nameof(CachePropertyType.TableSchema), TableSchema!); - return (string)TableSchema; + return (string?)TableSchema; } /// @@ -266,7 +262,7 @@ public static PropertyInfo[] GetComparableProperties(Type type) } var keyProps = GetKeyColumns(type); - var excludeProps = GetExcludedProperties(type); + var excludeProps = GetExcludedColumns(type); _properties = GetTypeProperties(type).Where(prop => !keyProps.Contains(prop.Name) && !excludeProps.Contains(prop.Name)).ToArray(); @@ -274,6 +270,59 @@ public static PropertyInfo[] GetComparableProperties(Type type) } + /// + /// Gets the properties that are unique for a specified type. + /// + /// The type for which to retrieve unique properties. + /// An array of objects representing unique properties of the specified type. + public static PropertyInfo[] GetKeyProperties(Type type) + { + var typeCaches = GetOrCreateDictionary(type); + + if (typeCaches.TryGetValue(nameof(CachePropertyType.KeyProperties), out var _properties)) + { + return (PropertyInfo[])_properties; + } + + var keyProps = GetKeyColumns(type); + var excludeProps = GetExcludedColumns(type); + + _properties = GetTypeProperties(type) + .Where(prop => + Attribute.IsDefined(prop, typeof(KeyPropertyAttribute)) && + ((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))!).KeyProperty + ).ToArray(); + + return (PropertyInfo[])_properties; + + } + + /// + /// Gets the excluded properties for a specified type. + /// + /// The type for which to retrieve the excluded properties. + /// An array of representing the excluded properties for the specified type. + public static PropertyInfo[] GetExcludedProperties(Type type) + { + var typeCaches = GetOrCreateDictionary(type); + + if (typeCaches.TryGetValue(nameof(CachePropertyType.ExcludedProperties), out var _properties)) + { + return (PropertyInfo[])_properties; + } + + var keyProps = GetKeyColumns(type); + + _properties = GetTypeProperties(type) + .Where(prop => + Attribute.IsDefined(prop, typeof(ExcludedPropertyAttribute)) && + ((ExcludedPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(ExcludedPropertyAttribute))!).Excluded + ).ToArray(); + + return (PropertyInfo[])_properties; + + } + /// /// Disposes of cached data associated with the specified type. /// diff --git a/src/Utils/DataContractUtility.cs b/src/Utils/DataContractUtility.cs index 8fb6477..0395040 100644 --- a/src/Utils/DataContractUtility.cs +++ b/src/Utils/DataContractUtility.cs @@ -12,87 +12,12 @@ namespace DbSyncKit.DB.Utils public class DataContractUtility : IDataContractComparer where T : IDataContractComparer { /// - /// Calculates the hash code for the object, excluding specified properties. + /// Overrides the default GetHashCode method to provide a custom hash code for the object. /// - /// List of property names to exclude from hash code calculation. - /// The calculated hash code. - public int GetHashCode(List excludedProperties) + /// The hash code of the object. + public override int GetHashCode() { - // Get all properties of the Entity class - var properties = typeof(T).GetProperties(); - - // Filter out properties that are in the exclusion list - var filteredProperties = properties - .Where(prop => !excludedProperties.Contains(prop.Name)); - - // Calculate the hash code using XOR - int hashCode = base.GetHashCode(); - foreach (var prop in filteredProperties) - { - var value = prop.GetValue(this); - if (value != null) - { - hashCode ^= value.GetHashCode(); - } - } - - return hashCode; - } - - /// - /// Generates a string representation of the object, excluding specified properties. - /// - /// List of property names to exclude from the string representation. - /// The string representation of the object. - public string ToString(List excludedProperties) - { - // Get all properties of the Entity class - var properties = typeof(T).GetProperties(); - - // Filter out properties that are in the exclusion list - var filteredProperties = properties - .Where(prop => !excludedProperties.Contains(prop.Name)); - - // Create a string representation - StringBuilder stringBuilder = new StringBuilder(base.ToString()); - foreach (var prop in filteredProperties) - { - var value = prop.GetValue(this); - stringBuilder.Append($",\t{prop.Name}: {value}"); - } - - return stringBuilder.ToString(); - } - - /// - /// Checks if the current object is equal to another object, considering specified properties. - /// - /// The object to compare. - /// List of properties to consider for equality check. - /// If true, specified properties are excluded; otherwise, they are included. - /// True if the objects are equal; otherwise, false. - public bool Equals(T obj, List listOfProperties, bool isExcluded = true) - { - if (obj == null || GetType() != obj.GetType()) - return false; - - var props = GetType().GetProperties(); - - if (isExcluded) - props = props.Where(prop => !listOfProperties.Contains(prop.Name)).ToArray(); - else - props = props.Where(prop => listOfProperties.Contains(prop.Name)).ToArray(); - - - foreach (var prop in props) - { - if (!EqualityComparer.Default.Equals(prop.GetValue(this), prop.GetValue(obj))) - { - return false; - } - } - - return true; + return base.GetHashCode(); } /// @@ -105,9 +30,6 @@ public override bool Equals(object? obj) if (obj == null || GetType() != obj.GetType()) return false; - var listOfExcluded = CacheManager.GetExcludedProperties(GetType()); - var listOfKey = CacheManager.GetKeyColumns(GetType()); - var props = CacheManager.GetComparableProperties(GetType()); foreach (var prop in props)