在Memory,IsoStorage和Server之间同步

时间:2013-10-04 10:19:14

标签: c# windows-phone-7 design-patterns memory synchronization

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

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

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

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

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


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

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? :)
}

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

1 个答案:

答案 0 :(得分: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; }
}