@@ -21,14 +21,15 @@ public class DataMetadataComparisonHelper<T> where T : IDataContractComparer
21
21
/// </summary>
22
22
/// <param name="sourceList">The source set of data contracts.</param>
23
23
/// <param name="destinationList">The destination set of data contracts.</param>
24
- /// <param name="keyComparer">An instance of <see cref="KeyEqualityComparer {T}"/> used for key comparison.</param>
24
+ /// <param name="keyComparer">An instance of <see cref="PropertyEqualityComparer {T}"/> used for key comparison.</param>
25
25
/// <returns>A <see cref="Result{T}"/> object containing added, deleted, and edited data contracts, as well as data counts.</returns>
26
- public static Result < T > GetDifferences ( HashSet < T > sourceList , HashSet < T > destinationList , KeyEqualityComparer < T > keyComparer , PropertyInfo [ ] CompariableProperties )
26
+ public static Result < T > GetDifferences ( HashSet < T > sourceList , HashSet < T > destinationList , PropertyEqualityComparer < T > keyComparer , PropertyInfo [ ] CompariableProperties )
27
27
{
28
28
29
29
List < T > added = new List < T > ( ) ;
30
30
List < T > deleted = new List < T > ( ) ;
31
31
ConcurrentBag < ( T edit , Dictionary < string , object > updatedProperties ) > edited = new ConcurrentBag < ( T , Dictionary < string , object > ) > ( ) ;
32
+ PropertyEqualityComparer < T > CompariablePropertyComparer = new PropertyEqualityComparer < T > ( CompariableProperties ) ;
32
33
33
34
// Identify added entries
34
35
added . AddRange ( sourceList . Except ( destinationList , keyComparer ) ) ;
@@ -39,20 +40,20 @@ public static Result<T> GetDifferences(HashSet<T> sourceList, HashSet<T> destina
39
40
// Identify edited entries
40
41
var sourceKeyDictionary = sourceList
41
42
. Except ( added )
42
- . ToDictionary ( row => GenerateCompositeKey ( row , keyComparer . keyProperties ) , row => row ) ;
43
+ . ToDictionary ( row => GenerateCompositeKey ( row , keyComparer . properties ) , row => row ) ;
43
44
44
45
var destinationKeyDictionary = destinationList
45
46
. Except ( deleted )
46
- . ToDictionary ( row => GenerateCompositeKey ( row , keyComparer . keyProperties ) , row => row ) ;
47
+ . ToDictionary ( row => GenerateCompositeKey ( row , keyComparer . properties ) , row => row ) ;
47
48
48
49
Parallel . ForEach ( sourceKeyDictionary , kvp =>
49
50
{
50
51
var sourceContract = kvp . Value ;
51
52
52
53
T ? destinationContract ;
53
- if ( destinationKeyDictionary . TryGetValue ( GenerateCompositeKey ( sourceContract , keyComparer . keyProperties ) , out destinationContract ) )
54
+ if ( destinationKeyDictionary . TryGetValue ( GenerateCompositeKey ( sourceContract , keyComparer . properties ) , out destinationContract ) )
54
55
{
55
- var ( isEdited , updatedProperties ) = GetEdited ( sourceContract , destinationContract , CompariableProperties ) ;
56
+ var ( isEdited , updatedProperties ) = GetEdited ( sourceContract , destinationContract , CompariablePropertyComparer ) ;
56
57
57
58
if ( isEdited )
58
59
{
@@ -77,40 +78,36 @@ public static Result<T> GetDifferences(HashSet<T> sourceList, HashSet<T> destina
77
78
#region Private Methods
78
79
79
80
/// <summary>
80
- /// Compares two entities of type <typeparamref name="T"/> to identify if any properties
81
- /// have been edited during synchronization.
81
+ /// Compares two instances of the data contract and identifies the properties that have been edited.
82
82
/// </summary>
83
- /// <param name="source">The original entity before synchronization .</param>
84
- /// <param name="destination">The entity in the destination after synchronization .</param>
85
- /// <param name="compariableProperties ">The properties to be compared for edits .</param>
83
+ /// <param name="source">The source instance to compare .</param>
84
+ /// <param name="destination">The destination instance to compare against .</param>
85
+ /// <param name="comparablePropertyComparer ">The comparer used to determine which properties are comparable .</param>
86
86
/// <returns>
87
- /// A tuple where:
88
- /// - <see cref="ValueTuple{T1,T2}.Item1"/> is a boolean indicating if any properties were edited.
89
- /// - <see cref="ValueTuple{T1,T2}.Item2"/> is a dictionary of updated properties for the edited entity.
87
+ /// A tuple indicating whether the instances are edited and a dictionary containing the names and values of the updated properties.
88
+ /// If the instances are not edited, returns (false, null).
90
89
/// </returns>
91
- private static ( bool isEdited , Dictionary < string , object > ? updatedProperties ) GetEdited ( T source , T destination , PropertyInfo [ ] compariableProperties )
90
+ private static ( bool isEdited , Dictionary < string , object > ? updatedProperties ) GetEdited ( T source , T destination , PropertyEqualityComparer < T > comparablePropertyComparer )
92
91
{
93
- if ( source . Equals ( destination ) )
92
+ if ( comparablePropertyComparer . Equals ( source , destination ) )
94
93
{
95
94
return ( false , null ) ;
96
95
}
97
96
98
97
Dictionary < string , object > updatedProperties = new ( ) ;
99
- bool isEdited = false ;
100
- foreach ( PropertyInfo prop in compariableProperties )
98
+ foreach ( PropertyInfo prop in comparablePropertyComparer . properties )
101
99
{
102
100
object sourceValue = prop . GetValue ( source ) ! ;
103
101
object destinationValue = prop . GetValue ( destination ) ! ;
104
102
105
103
// Compare values
106
- if ( ! EqualityComparer < object > . Default . Equals ( sourceValue , destinationValue ) )
104
+ if ( ! System . Collections . Generic . EqualityComparer < object > . Default . Equals ( sourceValue , destinationValue ) )
107
105
{
108
- isEdited = true ;
109
106
updatedProperties [ prop . Name ] = sourceValue ;
110
107
}
111
108
}
112
109
113
- return ( isEdited , updatedProperties ) ;
110
+ return ( true , updatedProperties ) ;
114
111
}
115
112
116
113
/// <summary>
0 commit comments