我有一个数据服务,它包含一个字符串列表。
列表应该快速返回,所以我将它保存在内存中的字符串列表中。我正在使用 GetList 和 SetList 处理内存。
列表应该可以抵抗应用程序关闭/逻辑删除,所以我也将其保存在文件中。我正在使用 ReadList 和 WriteList 来处理 IsoStorage。
List 应该与服务器同步,所以我有一些异步调用。使用 PushList 和 PullList 与服务器同步。
我觉得我正在发明一辆自行车。是否有平滑同步的模式?
编辑:到目前为止我得到了什么。其实需要的是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/