c# - 在内存、IsoStorage 和服务器之间同步

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

我有一个数据服务,它包含一个字符串列表。

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

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

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

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


编辑:到目前为止我得到了什么。其实需要的是getter

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

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

最佳答案

装饰器能帮忙吗?

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

关于c# - 在内存、IsoStorage 和服务器之间同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19178837/

相关文章:

c# - 正则表达式 选择 [A-Z ] 除了两个连续空格

windows-phone-7 - 我们可以使用 Blackberry 和 Windows 7 SDK 以编程方式截取屏幕截图吗?

c# - WP7 InvokeScript 错误

go - 重构Golang避免相似结构之间的手动字段更新

java - 是否有任何工具可以检测代码中的架构和设计模式?

c# - 在 C# 中提取 XML 内部节点元素

c# - 类的 in 修饰符有什么意义

c# - Windows Phone 上的状态大小

c++ - 组织基于任务的科学计算

c# - 访问 C# WPF 应用程序中的资源(同一程序集)