如何重构此 C# 代码

How to refactor this C# code

我有这些功能

    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();
    }

我正在写一个父类 class 来放入这些函数,这样函数 getMachines()InsertMachines() 变成 getObjectInsertObject 其中List<Machine> 可以是任何对象的列表,因此它们可以 return 并接受任何类型的列表

如何声明这样的函数?

根据你的描述,我创建的azure sync table class如下,你可以参考一下:

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
}

另外,关于Handling Conflict Resolution的更多细节,你可以参考adrian hall的书here