如何重构这个C#代码

时间:2017-06-07 02:34:13

标签: c# class azure oop azure-mobile-services

我有这些功能

    Parameters {"authenticity_token"=>"xSeWlcW6Ix5kciNGi2/xAep2aRq0CmefI6ln‌​ieQutlolAsk/qXM+mGvW‌​ixvNfTMedHnstWDd29mb‌​MThGF1FCqg==", "commit"=>"Sign up", "user"=>"email=ja%40j.com&name=jasonj&password=jjjjjj&passwo‌​rd_confirmation=jjjj‌​jj", "utf8"=>"✓"}

我正在写一个父类来放置这些函数,这样函数 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()getObject InsertObject可以是任何对象的列表,因此他们可以返回并接受任何类型的列表

如何声明这样的函数?

1 个答案:

答案 0 :(得分:1)

根据你的描述,我创建了我的azure sync table类,如下所示,你可以参考它:

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
}

此外,有关处理冲突解决的更多详细信息,请参阅adrian hall的书here

相关问题