There is a growing trend to access applications that are not just confined to just the PC but also extend to the other usually connected devices such as mobile phones and Point-of-Sale terminals. Keeping data synchronized across different platforms is a difficult problem to solve and the Microsoft Sync Framework is Microsoft’s solution to the problem.
In today’s world — where data is shared between various devices, ranging from your personal computer to your notebook, as well as your smartphones — data synchronization becomes a comprehensive problem that applications need to work with. Peripherals are no longer only “always connected;” they can also be “occasionally connected” like in smartphones or point-of-sale (POS) terminals.
The answer to this problem from Microsoft is the Sync Framework which enables collaboration for applications, services as well as devices.
The Problem
Consider the case of a salesman who needs to look up the current inventory to ensure that a possible order placed by the client can be handled by the current inventory. If he/she does not have the accurate data, he/she is not in a position to accurately ascertain where the order being place can be fulfilled by the expected date. If his/her POS device is not an “always connected” device, he/she will not have the accurate data to make that determination.
The Data Source
A data source can be defined as a repository of information which needs to be synchronized. The common data sources are relational databases like SQL Server and Oracle, and XML configuration files. Some applications even have flat files as data sources. As long as the data source offers programmatic way to access and update it, it can participate in data synchronization.
For data synchronization to work with a particular type of data store, the sync framework would have to implement a protocol to communicate with the data source.
Sync Framework
Microsoft has come up with a comprehensive synchronization platform that allows collaboration for applications, services and devices. The Sync framework is composed of:
- Core Component — They are used for creating synchronization providers.
- Synchronization Services for ADO.NET, File Systems and Feedsync, and
- Metadata Storage ServiceThe Sync Framework uses metadata to perform synchronization.
The core components are responsible for managing the synchronization session, and communicates updates to the clients.
The Sync Framework provides both managed as well as unmanaged interfaces for use.
The different participants in the synchronization process are:
- Full Participant — It can host the synchronization runtime as well as stores metadata. When 2 full participants are involved, either of them can start the synchronization process.
- Proxy participant — It enables synchronization for remote providers by handling calls locally and forwarding them to the remote provider.
- Partial Participant — It depends on full participants to start the synchronization. They can store metadata but cannot process it.
- Simple participant — It depends on full participant to do everything – manager synchronization as well as store metadata. It can be considered equivalent of a dumb terminal.
Synchronization Knowledge
How do the elements involved know what to add/update/delete? This is based on
the synchronization knowledge. The process of propagating synchronization knowledge
involves the following types of actions:- Change enumeration — The destination provider provides its current state to the source provider. The source provider then checks whether destination knowledge contains all the elements of the source replica and includes all missing changes in a batch.
- Change sending — Changes are sent from the source provider to the destination provider in batch. Each batch contains metadata which describes the changes.
- Conflict detection — When destination provider receives the changes from the source provider, a determination is made whether there is any conflict or if the data is outdated.
- Change application — Once conflicts (if any) are resolved, the changes are applied and the destination knowledge is now updated.
Hands On
To get started, download the Microsoft Sync Framework v2.1 from http://www.microsoft.com/downloads/details.aspx?FamilyID=ee6af141-79d0-4351-a4a0-ea89bb29dcf5&displaylang=en#filelist
You can choose to download either the x86 version or the x64 version. For this example, I have chosen the x64 version. After it is installed, start Visual Studio and create a new C# project called SampleSyncFramework.
Here is a mock skeleton of source and destination Synchronization Providers.
To create a synchronization provider, we need to inherit from KnowledgeSyncProvider (which is an abstract class) as well as implement the IChangeDataRetriever as well as INotifyingChangeApplierTarget interfaces.
In the skeleton below, the following methods need to be implemented by the source Sync Provider — BeginSession, GetChangeBatch, LoadChangeData, EndSession.
Here is the API skeleton for the source synchronization provider.
class SourceSyncProvider : KnowledgeSyncProvider, IChangeDataReceiver, INotifyingChangeApplierTarget { public override BeginSession(SyncProviderPosition position, SyncSessionContext syncSessionContext) { ... } public override ChangeBatch GetChangeBatch(uint batchSize, SyncKnowledge destinationKnowledge, out Object changeDataRetriever) { ... } public override Object LoadChangeData(LoadChangeContext loadChangeContext) { ... } public override void EndSession(SyncSessionContext syncSessionContext) { ... } }
For the destination synchronization provider, the following APIs needs to be overridden – BeginSession, GetSyncBatchParameters, ProcessChangeBatch, SaveItemChange, StoreKnowledgeForScope and EndSession. Here is the destination sync provider skeleton.
class DestinationSyncProvider : KnowledgeSyncProvider, IChangeDataReceiver, INotifyingChangeApplierTarget { public override BeginSession(SyncProviderPosition position, SyncSessionContext syncSessionContext) { ... } public override GetSyncBatchParameters (out uint batchSize, out SyncKnowledge knowledge) { ... } public override void ProcessChangeBatch( ConflictResolutionPolicy resolutionPolicy, ChangeBatch sourceChanges, Object changeDataRetriever, SyncCallbacks syncCallbacks, SyncSessionStatistics sessionStatistics ) { ... } public override void SaveItemChange (SaveChangeAction saveChangeAction, ItemChange change, SaveChangeContext context) { ... } public override void StoreKnowledgeForScope (SyncKnowledge knowledge, ForgottenKnowledge forgottenKnowledge) { ... } public override void EndSession(SyncSessionContext syncSessionContext) { ... } }
An example of Sync Provider implementation is available in the Code Samples section (the installation path of the samples would be “C:Program Files (x86)Microsoft SDKsMicrosoft Sync Framework2.1Samples”). Look at the Sync101 solution which contains an illustration of how a simple sync provider can be implemented.
Conclusion
Microsoft Sync Framework provides a framework to keep data concurrent across devices by providing infrastructure to maintain synchronization using a variety of formats. Use the format that meets your need and your data will always be up-to-date.
Bio: Vipul Patel
Vipul Patel is a Software Engineer currently working at Microsoft Corporation. He is currently working in the Office Communications Group and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.comRelated Articles
Free Microsoft Azure SQL Tools For Cloud Application Development
Handling Data Conflicts in the Microsoft Sync Framework