数据同步框架MS Sync Framework-不同场景使用例子和简要分析
2011-02-23 16:24
579 查看
上一篇/article/4762257.html对这个框架一个总体介绍,这篇通过SDK内带的例子和一个综合的例子描述一下这个框架的使用
[例子基于SDK2.1]
WebSharingAppDemo-CEProviderEndToEnd
运行效果如上图,可以进行SQL、SQLCE数据库的相互同步
Sync101RCA_Cached和其他例子展示了Provider等的实现,可参考其中的说明
ØProvisioning:对数据库架构和内容进行修改以记录修改的内容,为同步做准备
ØSynchronizing:同步过程,利用以上的信息进行信息的传递
ØSnapshot:在一个同步好的数据基础上建立快照,这样其他的客户端可以快速的部署
http://cid-56b433ad3d1871e3.office.live.com/self.aspx/.Public/SyncTest.rar
具体代码的使用如下:
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.
[例子基于SDK2.1]
SDK的例子
C:\ProgramFiles(x86)\MicrosoftSDKs\MicrosoftSyncFramework\2.1\SamplesWebSharingAppDemo-CEProviderEndToEnd
运行效果如上图,可以进行SQL、SQLCE数据库的相互同步
Sync101RCA_Cached和其他例子展示了Provider等的实现,可参考其中的说明
基本使用
使用这个框架进行数据库同步涉及的几个方面:ØProvisioning:对数据库架构和内容进行修改以记录修改的内容,为同步做准备
ØSynchronizing:同步过程,利用以上的信息进行信息的传递
ØSnapshot:在一个同步好的数据基础上建立快照,这样其他的客户端可以快速的部署
几个场景的例子
详细代码参考:具体代码的使用如下:
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
Thetwoproviders
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.
相关文章推荐
- 数据同步框架MS Sync Framework [术语、例子、参考资料、Tips]
- 数据同步框架MS Sync Framework - IDE快速开发支持Local Database Cache
- 数据同步框架MS Sync Framework - IDE快速开发支持Local Database Cache
- Microsoft Sync Framework 系列(八):微软同步框架中的自定义数据同步基础
- 在Windows Mobile和Wince(Windows Embedded CE)下使用.NET Compact Framework进行GPS NMEA data数据分析的开发
- 【转贴】pentaho不同的数据呈现形式使用场景
- net.sz.framework 框架 ORM 消消乐超过亿条数据排行榜分析 天王盖地虎
- Microsoft Sync Framework 系列(九):微软同步框架中的提供者(Provider)
- Redis和MySQL数据同步及Redis使用场景
- 【MySQL】MySQL数据模型、字符编码介绍与使用场景分析
- 在Windows Mobile和Wince(Windows Embedded CE)下使用.NET Compact Framework进行GPS NMEA data数据分析的开发
- [置顶] 解决项目中不同进程中使用SharedPreferences存取数据不同步的问题
- 微软同步框架-Microsoft Sync Framework 系列
- net.sz.framework 框架 ORM 消消乐超过亿条数据排行榜分析 天王盖地虎
- Microsoft Sync Framework 系列(四):微软同步框架中的基本概念
- Unity(六) 使用XML/JSON/Binary数据文件封装场景以及StreamingAssets目录的安卓平台同步
- Redis不同数据类型命令使用及应用场景
- Microsoft Sync Framework 系列(一):微软同步框架概述
- Microsoft Sync Framework 系列(六):微软同步框架中的知识(Knowledge)
- net.sz.framework 框架 ORM 消消乐超过亿条数据排行榜分析 天王盖地虎