2017-06-07 132 views
2

我有這些功能如何重構這個C#代碼

public async Task<List<Machine>> GetMachines() 
    { 
     await Initialize(); 
     await SyncMachines(); 
     return await machineTable.ToListAsync(); 
    } 

    public async Task InsertMachines(List<Machine> machines) 
    { 
     await Initialize(); 
     await Task.WhenAll(machines.Select(m => machineTable.InsertAsync(m))); 
     await SyncMachines(); 
    } 

我寫一個父類把這些功能,使得功能getMachines()InsertMachines()成爲getObjectInsertObject其中List<Machine>可以是一個列表的任何對象,所以他們可以返回並接受任何類型的列表

如何聲明這樣的功能?

+0

應該如何處理對'machineTable'和'SyncMachines()'的引用? – devio

回答

1

根據你的描述,我創建了蔚藍色的同步表類,如下所示,你可以參考一下吧:

public class AzureCloudSyncTable<TModel> where TModel : class 
{ 
    public static MobileServiceClient MobileService = new MobileServiceClient(
     "https://{your-mobile-app-name}.azurewebsites.net" 
    ); 
    private IMobileServiceSyncTable<TModel> syncTable = MobileService.GetSyncTable<TModel>(); 

    #region Offline sync 
    private async Task InitLocalStoreAsync() 
    { 
     if (!MobileService.SyncContext.IsInitialized) 
     { 
      var store = new MobileServiceSQLiteStore("localstore.db"); 
      store.DefineTable<TModel>(); 
      await MobileService.SyncContext.InitializeAsync(store); 
     } 
     await SyncAsync(); 
    } 

    private async Task SyncAsync() 
    { 
     await PushAsync(); 
     await syncTable.PullAsync(typeof(TModel).Name, syncTable.CreateQuery()); 
    } 

    private async Task PushAsync() 
    { 
     try 
     { 
      await MobileService.SyncContext.PushAsync(); 
     } 
     catch (MobileServicePushFailedException ex) 
     { 
      if (ex.PushResult != null) 
      { 
       foreach (var error in ex.PushResult.Errors) 
       { 
        await ResolveConflictAsync(error); 
       } 
      } 
     } 
    } 

    private async Task ResolveConflictAsync(MobileServiceTableOperationError error) 
    { 
     //var serverItem = error.Result.ToObject<TModel>(); 
     //var localItem = error.Item.ToObject<TModel>(); 

     //// Note that you need to implement the public override Equals(TModel item) 
     //// method in the Model for this to work 
     //if (serverItem.Equals(localItem)) 
     //{ 
     // // Items are the same, so ignore the conflict 
     // await error.CancelAndDiscardItemAsync(); 
     // return; 
     //} 

     //// Client Always Wins 
     //localItem.Version = serverItem.Version; 
     //await error.UpdateOperationAsync(JObject.FromObject(localItem)); 

     // Server Always Wins 
     //await error.CancelAndDiscardItemAsync(); 
    } 
    #endregion 

    #region public methods 
    public async Task<List<TModel>> GetAll() 
    { 
     await InitLocalStoreAsync(); 
     await SyncAsync(); 
     return await syncTable.ToListAsync(); 
    } 

    public async Task Insert(List<TModel> items) 
    { 
     await InitLocalStoreAsync(); 
     await Task.WhenAll(items.Select(item => syncTable.InsertAsync(item))); 
     await PushAsync(); 
    } 
    #endregion 
} 

此外,有關處理衝突解決方案的詳細信息,你可以參考阿德里安·霍爾的書here