2

我有一个 DataService,它包含一个字符串列表。

  • 列表应该快速返回,所以我将它保存在内存中的字符串列表中。我正在使用GetListSetList来处理内存。

  • 列表应该可以抵抗应用程序关闭/墓碑,所以我也将它保存在文件中。我正在使用ReadListWriteList来处理 IsoStorage。

  • 列表应该与服务器同步,所以我有一些异步调用。使用PushListPullList与服务器同步。

我有一种感觉,我正在发明一辆自行车。是否有平滑同步的模式?


编辑:到目前为止我得到了什么。实际上,需要的是一个吸气剂

async List<Items> GetList()
{
    if (list != null) return list; // get from memory

    var listFromIso = await IsoManager.ReadListAsync();
    if (listFromIso != null) return listFromIso; // get, well, from iso

    var answer = await NetworkManager.PullListAsync(SERVER_REQUEST);
    if (answer.Status = StatusOK) return answer.List; // get from.. guess where? :)
}

和二传手一样,只是反过来。请分享您的想法/经验。

4

1 回答 1

0

装修师傅能帮忙吗?

interface DataService
{
    IList<Items> GetList();
    void SetList(IList<Items> items);
}

class InMemoryDataService : DataService
{
    public InMemoryDataService(DataService other)
    {
        Other = other;
    }

    public IList<Items> GetList()
    {
        if (!Items.Any())
        {
            Items = Other.GetList();
        }

        return Items;
    }

    public void SetList(IList<Items> items)
    {
        Items = items;
        Other.SetList(items);
    }

    private IList<Items> Items { get; set; }
    private DataService Other { get; set; }
}

class IsoStorageDataService : DataService
{
    public IsoStorageDataService(DataService other)
    {
        Other = other;
    }

    public IList<Items> GetList()
    {
        ...
    }

    private DataService Other { get; set; }
}
于 2014-02-06T14:05:05.567 回答