移动设备数据访问技术三

1.向 N 层应用程序添加本地数据库缓存

  Visual Studio 上下文中的“本地数据库缓存”是 SQL Server Compact 数据库,
  该数据库配置为使用 Microsoft Synchronization Services for ADO.NET 与远程数据库进行数据同步。

2.向 RefactorNTierWalkthrough 添加本地数据库缓存
  由于本地数据库缓存是一个位于客户端上的 SQL Server Compact数据库,
  因此将本地数据库缓存添加到 RefactorNTierWalkthrough客户端项目上。
  本例将缓存 Customers 表,因此将本地数据库缓存命名为 CustomersCache。
  a)在“解决方案资源管理器”中右击“RefactorNTierWalkthrough”,再单击“添加新项”。
  b)单击“本地数据库缓存”模板。


  c)在“名称”中键入“CustomersCache”。
  d)单击“添加”。
  “配置数据同步”对话框随即打开。

  此时的项目结构:


3.在现有数据服务中启用同步
  生成的同步组件已添加到 DataService 项目中,但还需要通过服务来实现它们。
  生成的 SyncContract 包含服务所需的信息。此信息在文件中显示为注释。
  修改DataService项目的App.config:

View Code
      <service name="DataService.Service1">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/DataService/Service1/"   />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
        <endpoint address=""  binding="wsHttpBinding" contract="DataService.IService1">
          <!-- 
              部署时,应删除或替换下列标识元素,以反映
             用来运行所部署服务的标识。删除之后,WCF 将
              自动推断相应标识。
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 --> 
        <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <endpoint address ="SyncServer" binding="wsHttpBinding" contract="DataService.ICustomersCacheSyncContract"/>
      </service>

 


4.向现有的数据服务添加同步服务操作
修改DataService项目的CustomersCache.Server.SyncContract

View Code
    public partial class Service1 : object, ICustomersCacheSyncContract {
        
        private CustomersCacheServerSyncProvider _serverSyncProvider;
        
        public Service1() {
            this._serverSyncProvider = new CustomersCacheServerSyncProvider();
        }
        
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public virtual SyncContext ApplyChanges(SyncGroupMetadata groupMetadata, DataSet dataSet, SyncSession syncSession) {
            return this._serverSyncProvider.ApplyChanges(groupMetadata, dataSet, syncSession);
        }
        
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public virtual SyncContext GetChanges(SyncGroupMetadata groupMetadata, SyncSession syncSession) {
            return this._serverSyncProvider.GetChanges(groupMetadata, syncSession);
        }
        
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public virtual SyncSchema GetSchema(Collection<string> tableNames, SyncSession syncSession) {
            return this._serverSyncProvider.GetSchema(tableNames, syncSession);
        }
        
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public virtual SyncServerInfo GetServerInfo(SyncSession syncSession) {
            return this._serverSyncProvider.GetServerInfo(syncSession);
        }
    }
    
    [ServiceContractAttribute()]
    public interface ICustomersCacheSyncContract {
        
        [OperationContract()]
        SyncContext ApplyChanges(SyncGroupMetadata groupMetadata, DataSet dataSet, SyncSession syncSession);
        
        [OperationContract()]
        SyncContext GetChanges(SyncGroupMetadata groupMetadata, SyncSession syncSession);
        
        [OperationContract()]
        SyncSchema GetSchema(Collection<string> tableNames, SyncSession syncSession);
        
        [OperationContract()]
        SyncServerInfo GetServerInfo(SyncSession syncSession);
    }

 


5.发布服务
6.更新服务引用 
7.修改Form1.cs

  添加"加载数据"后台代码

View Code
        private void button1_Click(object sender, EventArgs e)
        {
            using (var client = new ServiceReference1.Service1Client())
            {
                //获取本地的customer表
                var customersTableAdapter
                   = new LocalNorthwindCustomersTableAdapters.CustomersTableAdapter();
                northwindDataSet.Customers.Merge(customersTableAdapter.GetData());
                //使用wcf获取远端的Order表
                northwindDataSet.Orders.Merge(client.GetOrders());
            }
        }

 

8.运行测试.

9.同步数据
  现在表示层已设置就绪,可以从正确的源显示表,下一步是添加代码来启动同步。
  将在窗体中添加一个按钮来启动同步进程。

  修改Form1.cs

  添加"同步数据"后台代码:

View Code
        private void button2_Click(object sender, EventArgs e)
        {
            CustomersCacheSyncAgent syncAgent = new CustomersCacheSyncAgent();

            using (ServiceReference1.CustomersCacheSyncContractClient syncClient = new ServiceReference1.CustomersCacheSyncContractClient())
            {
                syncAgent.RemoteProvider = new Microsoft.Synchronization.Data.ServerSyncProviderProxy(syncClient);
                Microsoft.Synchronization.Data.SyncStatistics syncStats = syncAgent.Synchronize();
                northwindDataSet.Customers.Merge(new LocalNorthwindCustomersTableAdapters.CustomersTableAdapter().GetData());

                string syncSummary = "Total changes downloaded: " +
                syncStats.TotalChangesDownloaded.ToString() + Environment.NewLine +
                "Last successful synchronization: " +
                 syncStats.SyncCompleteTime.ToString();
                MessageBox.Show(syncSummary);
            }
        }

 

10.运行测试

11.完成

 

转载请注明出处:http://www.cnblogs.com/refactor 

posted on 2012-07-04 11:59  refactor  阅读(1492)  评论(1编辑  收藏  举报

导航