您的位置:首页 > 其它

数据同步框架MS Sync Framework-不同场景使用例子和简要分析

2011-02-23 16:24 579 查看
上一篇/article/4762257.html对这个框架一个总体介绍,这篇通过SDK内带的例子和一个综合的例子描述一下这个框架的使用

[例子基于SDK2.1]

SDK的例子

C:\ProgramFiles(x86)\MicrosoftSDKs\MicrosoftSyncFramework\2.1\Samples

WebSharingAppDemo-CEProviderEndToEnd



运行效果如上图,可以进行SQL、SQLCE数据库的相互同步

Sync101RCA_Cached和其他例子展示了Provider等的实现,可参考其中的说明

基本使用

使用这个框架进行数据库同步涉及的几个方面:

ØProvisioning:对数据库架构和内容进行修改以记录修改的内容,为同步做准备

ØSynchronizing:同步过程,利用以上的信息进行信息的传递

ØSnapshot:在一个同步好的数据基础上建立快照,这样其他的客户端可以快速的部署

几个场景的例子

详细代码参考:http://cid-56b433ad3d1871e3.office.live.com/self.aspx/.Public/SyncTest.rar

具体代码的使用如下:

Database

N-Tier的同,SQlServer和SQLCE

Contracts:接口定义

SqlWebSvc:WCF服务

SyncClient:客户端端

1.建立SyncDB数据库,执行Data.edmx.sqldemoData.sql建立表和数据

2.启动SqlWebSvc服务

3.启动SyncClient

a)先Provison服务端数据库,定义出同步的表和条件[只要一次即可,需要修改可以DeProvision或使用1的步骤重建]

b)此时可远程同步[界面的中其他本地同步、架构建立只是说明]

c)快照:只要有一个同步好的数据库,这个文件就可以拷贝到其他的客户端,对于大数据量的初始化很有用





总结:

Ø数据的Provision和Deprovision简单

Ø代码稍微多一些

Database2

类似CustomerProvider的同步

数据库Provision在Custome_SyncSamplesDb.sql建立

WCFService:服务端

SyncClient:客户端

1.使用Custome_SyncSamplesDb.sql建立数据库和表

2.运行WCFService

3.运行SyncClient,可以看到同步的信息

总结:

Ø数据的Provision和Deprovision需要通过脚本编写,采用SQLServerTracking或自定义的形式

DBCache-IDE

VSIDE提供的支持,编程模型更简单

DataService:服务端

PresentationTier:客户端

1.使用Northwind.sql建立数据库和表

2.运行DataService服务

3.运行PresentationTier

总结

Ø提供了一系列的设置简化同步操作

Ø只能针对SQLServer系列的数据库

Offline

SDK中SynchronizingDatabases/Offline-OnlyScenarios的例子

TrackingChangesintheServerDatabase

UseaCustomChangeTrackingSystem

展示了以上两种跟踪变化的形式

Custome_SyncSamplesDb.sqlCustomeChangeTrack.cs

SqlServer_ChangeTracking.sqlSqlserverChangeTrack.cs

以上的脚本和代码配合可以完成同步的过程,通过这个可以了解更多的实现细节,对自定义Provider提供一些帮助

CodeSpecificToN-Tier

在SDK文档Howto:DeliverChangesinBatches(SQLServer)“Howto:DeliverChangesinBatches(SQLServer)”这个主题下有详细的说明,注记如下:

Theremainderofthecodeexamplesapplyonlytothen-tierscenarioinWebSharingAppDemo.Therelevantn-tiercodeiscontainedinthreefiles:

·Theservicecontract:IRelationalSyncContract

·TheWebservice:RelationalWebSyncService

·Theproxy:RelationalProviderProxy

ThetwoprovidersSqlSyncProviderandSqlCeSyncProviderbothinheritfromRelationalSyncProvider,sothiscodeappliestobothproviders.Additionalstore-specificfunctionalityisseparatedintoproxyandservicefilesforeachtypeofprovider.

Tounderstandhowbatchingworksinann-tierscenario,considerasynchronizationsessioninwhichtheserveristhesourceandtheclientisthedestination.Afterchangeshavebeenwrittentothelocaldirectoryontheserver,thefollowingprocessoccursfordownloadedchanges:

1.TheGetChangeBatchmethodiscalledontheclientproxy.Asdemonstratedlaterinthesamplecode,thismethodshouldincludespecificcodetohandlebatching.

2.TheservicegetsabatchfilefromSqlSyncProvider.Theserviceremovesthecompletepathinformationandsendsonlythefilenameoverthenetwork.Thispreventsexposingthedirectorystructureoftheservertotheclients.

3.TheproxycalltoGetChangeBatchreturns.

1.TheproxydetectsthatchangesarebatchedsoitcallsDownloadBatchFilebypassingthebatchfilenameasanargument.

2.Theproxycreatesauniquedirectory(ifonedoesn’texistforthesession)underRelationalProviderProxy.BatchingDirectorytoholdthesebatchfileslocally.ThedirectorynameisthereplicaIDofthepeerthatisenumeratingchanges.Thisensuresthattheproxyandservicehaveoneuniquedirectoryforeachenumeratingpeer.

4.Theproxydownloadsthefileandstoresitlocally.Theproxyreplacesthefilenameinthecontextwiththenewfullpathtothebatchfileonthelocaldisk.

5.Theproxyreturnsthecontextbacktotheorchestrator.

6.Repeatsteps1through6untilthelastbatchisreceivedbyproxy.

Thefollowingprocessoccursforuploadedchanges

1.TheorchestratorcallsProcessChangeBatchontheproxy.

2.Theproxydeterminesthatitisabatchfile,soitperformsthefollowingsteps:

1.Removesthecompletepathinformationandsendsonlythefilenameoverthenetwork.

2.CallsHasUploadedBatchFiletodetermineifthefilehasalreadybeenuploaded.Ifithas,stepCisnotnecessary.

3.IfHasUploadedBatchFilereturnsfalse,callsUploadBatchFileontheservice,anduploadsthebatchfilecontents.

TheservicewillreceivethecalltoUploadBatchFileandstorethebatchlocally.Directorycreationissimilartostep4above.

4.CallsApplyChangesontheservice.

3.TheserverreceivestheApplyChangescallanddeterminesthatitisabatchfile.Itreplacesthefilenameinthecontextwiththenewfullpathtothebatchfileonthelocaldisk.

4.TheserverpassestheDbSyncContexttolocalSqlSyncProvider.

5.Repeatsteps1through6untilthelastbatchissent.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐