Architecture and Classes for Database Synchronization
In offline and collaboration scenarios, Sync Framework can be used in 2-tier architectures and in N-tier architectures. In both architectures, activity occurs during a synchronization session: a synchronization orchestrator communicates with two synchronization providers to retrieve and apply changes to each database. The providers use a synchronization adapter for each table with the specific SQL commands that are required for that table. Some providers expose adapters to enable you to customize these commands. Other providers generate the adapters automatically to reduce the complexity associated with synchronizing a particular store.
The following illustration shows a two-tier architecture: all components for a synchronization session reside on the local computer; and there is a direct connection from the local computer to the database on the remote computer. During a synchronization session, the local computer is the computer from which synchronization is initiated. If you want to initiate synchronization from more than one computer, each computer must contain all the components that are shown in the illustration. These components are described in more detail after the illustrations.
The following illustration shows an N-tier architecture: additional components are required, including components on the remote computer; and the connection to the remote database is now handled by a proxy on the local computer and a service on the remote computer. Both the proxy and the service must be implemented by the application developer. Samples are provided to reduce the complexity associated with this task. As with the 2-tier architecture, if you want to initiate synchronization from more than one computer, each computer must contain all the components.
Databases can be any edition of SQL Server 2005 SP2 or later, including SQL Server Compact 3.5 SP2 or later (32-bit and 64 bit versions), , or other databases for which an ADO.NET provider is available. Sync Framework configures change-tracking and metadata storage automatically for SQL Server and SQL Server Compact databases. For more information, see How to: Execute Database Synchronization (SQL Server). For non-SQL Server databases, you must provision the database. For more information, see How to: Provision a Server Database for Collaborative Synchronization (Non-SQL Server).
The synchronization orchestrator drives synchronization in the following ways:
Determines the order and direction in which changes should be applied, based on its Direction property.
Calls the remote synchronization provider to retrieve and apply changes at the remote database.
Calls the local synchronization provider to retrieve and apply changes at the local database.
The synchronization orchestrator also maintains session-level information for the synchronization session and provides success messages, errors, and statistics to the application that initiates synchronization. For more information, see SyncOrchestrator.
SyncProvider (SqlSyncProvider, SqlCeSyncProvider, and DbSyncProvider)
A synchronization provider communicates with each database and shields the synchronization orchestrator from the specific implementation of the database. For SQL Server Compact databases, use SqlCeSyncProvider; and for other editions of SQL Server, including , use SqlSyncProvider. For other databases, use DbSyncProvider. All three providers inherit from RelationalSyncProvider. The principal activities of the synchronization provider are as follows:
Retrieves changes that occurred in the database since the last synchronization session.
Applies incremental changes to the database.
Detects conflicting changes.
If you are synchronizing two SQL Server Compact databases by using two instances of SqlCeSyncProvider, you must use an n-tier architecture. Two-tier architectures are not supported for this scenario.
SyncAdapter (DbSyncAdapter and SqlCeSyncAdapter)
Modeled after the data adapter in ADO.NET, the synchronization adapter is defined for each table that is synchronized. The synchronization adapter supplies the synchronization provider with the specific SQL commands that are required to interact with the database.
For the synchronization adapters that are used by SqlSyncProvider and SqlCeSyncProvider, Sync Framework generates commands automatically (SqlCeSyncAdapter is private to reduce the complexity of synchronizing with this store.). For DbSyncAdapter, you create the adapter and specify the commands, such as the InsertCommand that applies inserts from another server or client database to the server database. Because synchronization adapters use the ADO.NET DbCommand object, you can use any command structure that is supported by ADO.NET. This includes inline SQL, stored procedures, views against a single table, functions, and so on. The commands only require a single result that defines the structure and data to be transferred and applied. For more information, see DbSyncAdapter. For more information about commands, see "Synchronization Adapter" in How to: Synchronize Other ADO.NET Compatable Databases.
Additional Classes in the API
The illustrations in this topic show the major classes in the API. However, there are many classes that are not shown. To obtain information about all the available classes, see Microsoft.Synchronization, Microsoft.Synchronization.Data, Microsoft.Synchronization.Data.SqlServer , and Microsoft.Synchronization.Data.SqlServerCe. The following sections provide introductions to other important classes that you should be familiar with.
A synchronization scope is a logical grouping of objects that must be synchronized as a unit. In the case of database synchronization, a scope is typically a set of tables; and tables can be filtered. Tables can be included in one or more scopes. For more information, see "Define Scopes to Synchronize" in How to: Provision a Server Database for Collaborative Synchronization (Non-SQL Server).
Database Provisioning Objects
For SQL Server, , and SQL Server Compact databases, Sync Framework provides a set of classes that are used to describe scopes and the tables that are included in each scope. After the tables and scopes are described, you use Sync Framework objects to apply provisioning scripts to each node. The scripts create a change-tracking and change-application infrastructure that consists of metadata tables, triggers, and stored procedures. The following table lists the classes that are used to set up databases and providers. For more information, see How to: Execute Database Synchronization (SQL Server).
SQL Server and
SQL Server Compact
Represents a synchronization scope, which is a logical grouping of tables (optionally filtered) that are synchronized as a unit.
Represents the provisioning of a SQL Server, , or SQL Server Compact database for a particular scope that is represented by a DbSyncScopeDescription object.
Represents the schema of a table that is included in a synchronization scope.
Represents the schema of a column that is part of a table that is included in a synchronization scope.
Represents scope and table information for a SQL Server, , or SQL Server Compact database that is involved in synchronization.
Represents the provisioning of a SQL Server, , or SQL Server Compact database that is represented by a DbSyncTableDescription object.
Represents synchronization adapter configuration information for a table in a SQL Server, , or SQL Server Compact database.
Session statistics are a set of statistics that the synchronization orchestrator provides for each synchronization session. The statistics include information about synchronization times, the number of changes processed, and any conflicts or exceptions that occurred. For more information, see SyncOperationStatistics.
The DbSyncSession object provides access to synchronization session variables. Session variables are variables that are provided for a developer to use as parameters for the select, insert, update, and delete commands executed at servers that use DbSyncProvider and DbSyncAdapter. For more information, see How to: Use Session Variables for Collaborative Synchronization (Non-SQL Server).
Sync Framework DLLs
Sync Framework database synchronization classes are contained in the following DLLs:
Microsoft.Synchronization.dll, which includes SyncOrchestrator.
Microsoft.Synchronization.SqlServer.dll, which includes SqlSyncProvider.
Microsoft.Synchronization.SqlServerCe.dll, which includes SqlCeSyncProvider.
The DLLs depend on System.dll and System.Data.dll from .NET Framework 2.0 or later versions.