2013-10-04 57 views
2

我有一個DataService,它包含一個字符串列表。內存,IsoStorage和服務器之間的同步

  • 列表應該快速返回,所以我把它保存在內存中,在一個字符串列表中。我正在使用GetListSetList用於處理內存。

  • 列表應該是抵制應用程序關閉/墓碑,所以我也把它保存在文件中。我正在使用讀取列表WriteList與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? :) 
} 

和setter,同樣剛好相反。請分享你的想法/經驗。

回答

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; } 
} 
相關問題