Première étape : Créer le fournisseur de synchronisation client, classe héritée de
SqlCeClientSyncProvider.
Cette classe doit contenir la chaine de connexion à la source de donnée cliente, configurée dans notre fichier de configuration.
Note : SqlCeClientSyncProvider sait créer les tables à la volée, par contre il ne sait pas créer la base de donnée si celle-ci n'existe pas.
public class DemoClientSyncProvider : SqlCeClientSyncProvider
{
public DemoClientSyncProvider()
{
this.ConnectionString = ConfigurationManager.ConnectionStrings["ClientConnexionString"];
}
public DemoClientSyncProvider(string connectionString)
{
this.ConnectionString = connectionString;
}
}
Deuxième étape : Créer l'agent, classe héritée de
SyncAgent.
Cette classe va contenir les
SyncTables et le
SyncGroup.
Dans notre exemple, deux SyncTables : Client et ClientType.
Nous lierons ces deux SyncTables dans un SyncGroup, pour préserver la clé externe, et créer un contexte Transactionnel de synchronisation.
Notez les options de création des SyncTables : Une est créée avec l'option de Direction
BiDirectional, puisque nous voulons récupérer les enregistrements de façon incrémentale et l'autre avec l'option
Snapshot, la table ClientType devant être récupérée entièrement, créant une sorte de "cache client".
Note : l'agent doit connaître le fournisseur serveur, nous ne l'avons pas encore créé, c'est pourquoi nous le mettons, pour l'instant, en commentaire.
public partial class DemoSyncAgent : SyncAgent
{
private SyncGroup globalSyncGroup;
private SyncTable clientSyncTable;
private SyncTable clientTypeSyncTable;
public DemoSyncAgent()
{
this.InitializeSyncProviders();
this.InitializeSyncTables();
}
public SyncTable Client
{
get
{
return this.clientSyncTable;
}
set
{
this.Configuration.SyncTables.Remove(this.clientSyncTable);
this.clientSyncTable = value;
this.Configuration.SyncTables.Add(this.clientSyncTable);
}
}
public SyncTable ClientType
{
get
{
return this.clientTypeSyncTable;
}
set
{
this.Configuration.SyncTables.Remove(this.clientTypeSyncTable);
this.clientTypeSyncTable = value;
this.Configuration.SyncTables.Add(this.clientTypeSyncTable);
}
}
private void InitializeSyncProviders()
{
// this.RemoteProvider = new DemoServerSyncProvider();
this.LocalProvider = new DemoClientSyncProvider();
}
private void InitializeSyncTables()
{
// Creation du SyncGroup.
this.globalSyncGroup = new Microsoft.Synchronization.Data.SyncGroup("DemoGlobalSyncGroup");
// Creation des SyncTables.
this.clientSyncTable = new SyncTable();
this.clientSyncTable.TableName = "Client";
this.clientSyncTable.CreationOption = TableCreationOption.DropExistingOrCreateNewTable;
this.clientSyncTable.SyncDirection = SyncDirection.Bidirectional;
this.clientSyncTable.SyncGroup = globalSyncGroup;
this.Configuration.SyncTables.Add(this.clientSyncTable);
this.clientTypeSyncTable = new SyncTable();
this.clientTypeSyncTable.SyncGroup = globalSyncGroup;
this.clientTypeSyncTable.TableName = "ClientType";
this.clientTypeSyncTable.CreationOption = TableCreationOption.DropExistingOrCreateNewTable;
this.clientTypeSyncTable.SyncDirection = SyncDirection.Snapshot;
this.Configuration.SyncTables.Add(this.clientTypeSyncTable);
}
}
Nous avons terminé la partie Cliente, nous pouvons nous attaquer au gros morceau, la configuration du coté serveur.