diff --git a/src/DbSyncKit.Core.csproj b/src/DbSyncKit.Core.csproj index f3ba60a..a4ce117 100644 --- a/src/DbSyncKit.Core.csproj +++ b/src/DbSyncKit.Core.csproj @@ -6,9 +6,9 @@ enable True True - 1.2.1.0 - 1.2.1.0 - 1.2.1.0 + 1.3.0.0 + 1.3.0.0 + 1.3.0.0 https://dbsynckit.rohit-mahajan.in/ README.md https://github.com/RohitM-IN/DbSyncKit diff --git a/src/Fetcher/DataContractFetcher.cs b/src/Fetcher/DataContractFetcher.cs index 04ac405..43294e5 100644 --- a/src/Fetcher/DataContractFetcher.cs +++ b/src/Fetcher/DataContractFetcher.cs @@ -5,6 +5,18 @@ namespace DbSyncKit.Core.Fetcher { + /// + /// Represents a callback function for filtering a list of data entities of type T. + /// + /// The type of data entities to filter. + /// The list of data entities to filter. + /// A filtered list of data entities. + /// + /// This delegate defines a callback function that takes a list of data entities as input, + /// performs filtering operations on the data, and returns the filtered list. + /// + public delegate List FilterCallback(List data); + /// /// Utility class for fetching data from a database using data contracts. /// @@ -21,7 +33,7 @@ public class DataContractFetcher /// Gets or sets the QueryGenerationManager instance for generating queries for the destination database. /// /// - /// This property is set by the method. + /// This property is set by the method. /// It allows the reuse of the QueryGenerationManager instance when the source and destination databases share the same provider. /// public IQueryGenerator? DestinationQueryGenerationManager { get; private set; } @@ -46,28 +58,32 @@ public DataContractFetcher(QueryGeneratorFactory factory) /// /// Retrieves data from source and destination databases for a specified table and column list, using a specified data contract type. /// + /// The type of data entities to retrieve. /// The source database. /// The destination database. /// The name of the table from which to retrieve data. /// A list of column names to retrieve from the table. /// An equality comparer for identifying properties used in data comparison. + /// A callback function for filtering the retrieved data. /// An output parameter that receives the retrieved data from the source database. /// An output parameter that receives the retrieved data from the destination database. /// /// The method uses a QueryGenerationManager to generate queries for retrieving data based on the specified table and columns. /// If the providers of the source and destination databases differ, a new QueryGenerationManager is created for the destination database. + /// The retrieved data can be optionally filtered using the provided filter callback function. /// public void RetrieveDataFromDatabases( IDatabase source, IDatabase destination, string tableName, List ColumnList, - PropertyEqualityComparer ComparablePropertyEqualityComparer, + PropertyEqualityComparer ComparablePropertyEqualityComparer, + FilterCallback? filterCallback, out HashSet sourceList, out HashSet destinationList) { var sourceQueryGenerationManager = new QueryGenerationManager(Factory.GetQueryGenerator(source.Provider)); - sourceList = GetDataFromDatabase(tableName, source, sourceQueryGenerationManager, ColumnList, ComparablePropertyEqualityComparer); + sourceList = GetDataFromDatabase(tableName, source, sourceQueryGenerationManager, ColumnList, ComparablePropertyEqualityComparer, filterCallback); if (source.Provider != destination.Provider) { @@ -79,35 +95,47 @@ public void RetrieveDataFromDatabases( DestinationQueryGenerationManager = sourceQueryGenerationManager; } - destinationList = GetDataFromDatabase(tableName, destination, DestinationQueryGenerationManager, ColumnList, ComparablePropertyEqualityComparer); + destinationList = GetDataFromDatabase(tableName, destination, DestinationQueryGenerationManager, ColumnList, ComparablePropertyEqualityComparer, filterCallback); } /// /// Retrieves data from a database for a specified table, columns, and data contract type. /// + /// The type of data entities to retrieve. /// The name of the table from which to retrieve data. /// The database connection. /// The query generation manager for creating the SELECT query. /// A list of column names to retrieve from the table. /// An equality comparer for identifying properties used in data comparison. + /// A callback function for filtering the retrieved data. /// /// A HashSet of data entities of type T retrieved from the specified table and columns in the database. /// /// /// The method generates a SELECT query using the provided query generation manager and executes it using a DatabaseManager. /// The resulting data is converted to a HashSet using the specified property equality comparer for data comparison. + /// The retrieved data can be optionally filtered using the provided filter callback function. /// public HashSet GetDataFromDatabase( string tableName, IDatabase connection, IQueryGenerator manager, List columns, - PropertyEqualityComparer ComparablePropertyEqualityComparer) + PropertyEqualityComparer ComparablePropertyEqualityComparer, + FilterCallback? filterCallback) { var query = manager.GenerateSelectQuery(tableName, columns, string.Empty); using var DBManager = new DatabaseManager(connection); - return DBManager.ExecuteQuery(query, tableName).ToHashSet(ComparablePropertyEqualityComparer); + + var data = DBManager.ExecuteQuery(query, tableName); + + if(filterCallback != null) + { + data = filterCallback(data); + } + + return data.ToHashSet(ComparablePropertyEqualityComparer); } #endregion diff --git a/src/Synchronization.cs b/src/Synchronization.cs index c430dfd..bf9595a 100644 --- a/src/Synchronization.cs +++ b/src/Synchronization.cs @@ -90,11 +90,19 @@ public Synchronization(DataContractFetcher fetcher, QueryBuilder queryBuilder, D /// /// Synchronizes data of a specific type between source and destination databases. /// + /// The type of data entities to synchronize. /// The source database. /// The destination database. - /// Represents Which Direction to compare db + /// A callback function for filtering data before synchronization. + /// Specifies the direction of synchronization. Default is SourceToDestination. /// A result object containing the differences between source and destination data. - public Result SyncData(IDatabase source, IDatabase destination, Direction direction = Direction.SourceToDestination) + /// + /// This method synchronizes data between source and destination databases of a specific type. + /// It retrieves data from both databases, applies optional filtering using the provided callback function, + /// and then compares the data to identify differences. The direction of synchronization determines + /// the comparison direction between source and destination databases. + /// + public Result SyncData(IDatabase source, IDatabase destination, FilterCallback? filterDataCallback, Direction direction = Direction.SourceToDestination) { #region Properties string tableName = GetTableName(); @@ -114,7 +122,7 @@ public Result SyncData(IDatabase source, IDatabase destination, Direction SwapDatabasesIfNeeded(ref source, ref destination, direction); - ContractFetcher.RetrieveDataFromDatabases(source, destination, tableName, ColumnList, ComparablePropertyEqualityComparer, out sourceList, out destinationList); + ContractFetcher.RetrieveDataFromDatabases(source, destination, tableName, ColumnList, ComparablePropertyEqualityComparer, filterDataCallback, out sourceList, out destinationList); return MismatchIdentifier.GetDifferences(sourceList, destinationList, keyEqualityComparer, ComparablePropertyEqualityComparer); }