如何重构此C#代码 [英] How to refactor this C# code

查看:93
本文介绍了如何重构此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>可以是任何对象的列表,因此它们可以返回并接受任何类型的列表

I am writing a parent class to put these functions in, such that the functions getMachines() and InsertMachines() become getObject and InsertObject where the List<Machine> can be a list of any objects, so they can return and accept any type of list

如何声明这样的函数?

推荐答案

根据您的描述,我按如下方式创建了我的azure同步表类,您可以参考它:

According to your description, I created my azure sync table class as follows, you could refer to it:

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的书

Additionally, for more details about Handling Conflict Resolution, you could refer to adrian hall's book here.

这篇关于如何重构此C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆