Basic Synchronization

In this guide, we'll walk through the process of performing a basic data synchronization operation using DbSyncKit.

Prerequisites

Before we start, ensure that you have the following:

Setup

To begin, let's set up the necessary variables. For detailed setup instructions, refer to the Setup Guide.

Here's a brief summary of the variables:

  • IDatabase SourceDatabase: Represents the source database connection.
  • IDatabase DestinationDatabase: Represents the destination database connection.
  • Synchronization Sync: Represents the DbSyncKit synchronization instance.

For detailed instructions on setting up these variables, refer to the Setup Guide.

Perform Synchronization

Now that we have the variables set up, let's perform the synchronization. Replace YourEntity with the appropriate entity you want to synchronize.

// Perform synchronization
Result<YourEntity> syncResult = Sync.SyncData<YourEntity>(SourceDatabase, DestinationDatabase);

The synchronization result provides detailed information about added, edited, and deleted records, along with the change type and data counts.

For more details on interpreting the synchronization result, refer to the Synchronization Results Guide.

Manual Synchronization

To perform synchronization manually, follow these steps:

Certainly! Let's go through each step with detailed explanations and examples:

1) Setup

Explanation: The setup step involves initializing variables and configurations necessary for synchronization.

Code Example:

List<string> excludedProperty = Sync.GetExcludedColumns<YourEntity>();
List<string> columnList = Sync.GetAllColumns<YourEntity>().Except(excludedProperty).ToList();
PropertyInfo[] comparableProperties = Sync.GetComparableProperties<YourEntity>();
PropertyInfo[] keyProperties = Sync.GetKeyProperties<YourEntity>();
    
// Create equality comparers
PropertyEqualityComparer<YourEntity> keyEqualityComparer = new(keyProperties);
PropertyEqualityComparer<YourEntity> ComparablePropertiesComparer = new(comparableProperties);
    
// Get the table name
string tableName = Sync.GetTableName<YourEntity>();

// Result
Result<YourEntity> result;

2) Getting Data

In this step, data is retrieved from source and destination databases.

Code Example:

// A method to filter out the result data before creating a hashset (optional or you can pass null instead)
private List<YourEntity> FilterData(List<YourEntity> data)
{
    // filter your data here
    return data;
}

// Use the synchronization instance to retrieve data
Sync.ContractFetcher.RetrieveDataFromDatabases<YourEntity>(Source, Destination, tableName, columnList, ComparablePropertiesComparer, FilterData,out HashSet<YourEntity> SourceList, out HashSet<YourEntity> DestinationList);

3) Comparison

This step involves comparing the data from the source and destination databases.

Code Example:

// Use the synchronization instance to get differences
result = Sync.MismatchIdentifier.GetDifferences<YourEntity>(sourceList, destinationList, keyEqualityComparer, ComparablePropertiesComparer);

4) Generating SQL Queries

This step generates SQL queries for synchronization based on the identified differences.

Code Example:

Sync.QueryBuilder.GetSqlQueryForSyncData<YourEntity>(syncResult,Sync.ContractFetcher.DestinationQueryGenerationManager);

5) Cleanup

This step involves cleaning up resources or performing any necessary cleanup operations.

Code Example:

DbSyncKit.DB.Manager.CacheManager.DisposeType(typeof(YourEntity));

Continue exploring other topics in the Usage Guide.