Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code optimization #1

Merged
merged 1 commit into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/Enum/CachePropertyType.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -54,7 +50,17 @@ public enum CachePropertyType
/// <summary>
/// Represents properties that are comparable (neither key nor excluded).
/// </summary>
ComparableProperties
ComparableProperties,

/// <summary>
/// Represents properties that are unique.
/// </summary>
KeyProperties,

/// <summary>
/// Represents properties that are excluded.
/// </summary>
ExcludedProperties
}

}
23 changes: 21 additions & 2 deletions src/Helper/QueryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public List<string> GetKeyColumns<T>() where T : IDataContractComparer
/// <typeparam name="T">The type for which to get the excluded properties. Must implement <see cref="IDataContractComparer"/>.</typeparam>
/// <returns>A list of excluded property names.</returns>
/// <seealso cref="IDataContractComparer"/>
public List<string> GetExcludedProperties<T>() where T : IDataContractComparer
public List<string> GetExcludedColumns<T>() where T : IDataContractComparer
{
return CacheManager.GetExcludedProperties(typeof(T));
return CacheManager.GetExcludedColumns(typeof(T));
}

/// <summary>
Expand Down Expand Up @@ -98,10 +98,29 @@ public List<string> GetIdentityColumns<T>() where T : IDataContractComparer
return CacheManager.GetIdentityColumns(typeof(T));
}

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

/// <summary>
/// Retrieves an array of <see cref="PropertyInfo"/> objects representing the properties that are used as key properties
/// for uniquely identifying objects of type <typeparamref name="T"/>. These key properties are determined based on the
/// implementation of the <see cref="IDataContractComparer"/> interface.
/// </summary>
/// <typeparam name="T">The type of objects for which to retrieve key properties.</typeparam>
/// <returns>An array of <see cref="PropertyInfo"/> objects representing the key properties of type <typeparamref name="T"/>.</returns>
public PropertyInfo[] GetKeyProperties<T>() where T : IDataContractComparer
{
return CacheManager.GetComparableProperties(typeof(T));
}

}
}
75 changes: 62 additions & 13 deletions src/Manager/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static List<string> 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();


Expand All @@ -92,7 +92,7 @@ public static List<string> 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);
Expand All @@ -103,9 +103,9 @@ public static List<string> GetKeyColumns(Type type)
/// <summary>
/// Gets the names of properties marked as excluded properties for a specified type.
/// </summary>
/// <param name="type">The type for which to retrieve excluded properties.</param>
/// <returns>A list of excluded property names.</returns>
public static List<string> GetExcludedProperties(Type type)
/// <param name="type">The type for which to retrieve excluded columns.</param>
/// <returns>A list of excluded column names.</returns>
public static List<string> GetExcludedColumns(Type type)
{
var typeCaches = GetOrCreateDictionary(type);

Expand All @@ -114,11 +114,7 @@ public static List<string> GetExcludedProperties(Type type)
return (List<string>)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);

Expand Down Expand Up @@ -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;
}

/// <summary>
Expand Down Expand Up @@ -266,14 +262,67 @@ 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();

return (PropertyInfo[])_properties;

}

/// <summary>
/// Gets the properties that are unique for a specified type.
/// </summary>
/// <param name="type">The type for which to retrieve unique properties.</param>
/// <returns>An array of <see cref="PropertyInfo"/> objects representing unique properties of the specified type.</returns>
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;

}

/// <summary>
/// Gets the excluded properties for a specified type.
/// </summary>
/// <param name="type">The type for which to retrieve the excluded properties.</param>
/// <returns>An array of <see cref="PropertyInfo"/> representing the excluded properties for the specified type.</returns>
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;

}

/// <summary>
/// Disposes of cached data associated with the specified type.
/// </summary>
Expand Down
86 changes: 4 additions & 82 deletions src/Utils/DataContractUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,87 +12,12 @@ namespace DbSyncKit.DB.Utils
public class DataContractUtility<T> : IDataContractComparer where T : IDataContractComparer
{
/// <summary>
/// Calculates the hash code for the object, excluding specified properties.
/// Overrides the default GetHashCode method to provide a custom hash code for the object.
/// </summary>
/// <param name="excludedProperties">List of property names to exclude from hash code calculation.</param>
/// <returns>The calculated hash code.</returns>
public int GetHashCode(List<string> excludedProperties)
/// <returns>The hash code of the object.</returns>
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;
}

/// <summary>
/// Generates a string representation of the object, excluding specified properties.
/// </summary>
/// <param name="excludedProperties">List of property names to exclude from the string representation.</param>
/// <returns>The string representation of the object.</returns>
public string ToString(List<string> 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();
}

/// <summary>
/// Checks if the current object is equal to another object, considering specified properties.
/// </summary>
/// <param name="obj">The object to compare.</param>
/// <param name="listOfProperties">List of properties to consider for equality check.</param>
/// <param name="isExcluded">If true, specified properties are excluded; otherwise, they are included.</param>
/// <returns>True if the objects are equal; otherwise, false.</returns>
public bool Equals(T obj, List<string> 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<object?>.Default.Equals(prop.GetValue(this), prop.GetValue(obj)))
{
return false;
}
}

return true;
return base.GetHashCode();
}

/// <summary>
Expand All @@ -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)
Expand Down