2016-04-06 24 views
0

什麼是最好的方式我正在開發一個MVC項目,雖然我是新的MVC中,我有一個函數加載數據庫值。我在同一個控制器的不同地方使用這個功能。當我檢查了SQL分析器時,我發現每次運行我使用該函數的過程時。什麼是加載和重用列表<DataRow>對象在MVC控制器

我的問題是什麼是使用函數的最佳方法,它將在第一次調用該過程,並且從下次開始,它將使用相同的值。

在此先感謝

更新時間:

public List<DataRow> SalesCount() 
{   
    List<DataRow> lstSales = new List<DataRow>(); 
    string str_salesClass = adding session value here;  
    return lstSales = SalesRepository.SalesCount(SessionValue);   
} 
+0

顯示你的代碼,所以我們可以SE你做了什麼至今 – Theunis

+0

@VadimMartynov,謝謝瓦迪姆,我可能會想法有關緩存數據,但在我的情況,我怎麼能實現高速數據 – ronibd

回答

1

有幾種方法可以做到這裏面Controller。起初,您應該知道,每次向控制器發送請求時,您都會使用不同的相同控制器的實例。

第一種方式:使用單身。將值存儲在公共靜態實例中,每次請求時加載一次並獲取相同的列表。但是,因爲它是數據庫值 - 您需要定期更新數據庫中存儲的值。

public class LoaderSingleton 
{ 
    private static readonly LoaderSingleton _instance = new LoaderSingleton(); 
    private List<DataRow> _items; 
    static LoaderSingleton() 
    { 

    } 

    private LoaderSingleton() 
    { 

    } 

    public static LoaderSingleton Instance 
    { 
     get 
     { 
      return _instance; 
     } 
    } 

    public List<DataRow> Items 
    { 
     get 
     { 
      if (this._items == null) 
      { 
       this.Load(); 
      } 

      return this._items; 
     } 
    } 

    public void Load() 
    { 
     this._items = //perform load from database 
    } 
} 

方式二:使用Session變量。將值存儲在用戶會話中而不是靜態類中。你不應該忘記更新值和清理會話。

//inside controller 
this.Session[str_salesClass] = SalesRepository.SalesCount(); 

第三條道路:使用ViewData變量。值將僅存儲在請求之間,您應該在上次請求完成後使用新值更新值。

//inside controller 
this.ViewData[str_salesClass] = SalesRepository.SalesCount(); 

第四種方式:執行從您的視圖async請求。例如,您正在將所需的值加載到基本視圖,它可以異步加載另一個視圖,並且當您發送加載子視圖的請求時 - 您應該將加載的數據作爲參數傳遞。

public ActionResult BaseView() 
{ 
    ViewBag.Data = SalesRepository.SalesCount(); 
    return View(); 
} 

[ChildActionOnly] 
public ActionResult ChildView1(List<DataRow> sales) 
{ 

} 

我不推薦使用Cache,導致它有不可預知的行爲。當你不期望的時候,你會得到null

+0

謝謝Mikhail,我將使用ViewData/ViewBag選項,非常感謝 – ronibd

0

您可以使用MemoryCache保存SQL查詢結果,並有只有一些參數的變化或高速緩存值需要被無效的新請求。

對於配置緩存參數,您可以使用CacheItemPolicy類。您可以配置一個值,該值指示緩存條目是否應該在指定的持續時間後被逐出,額外的監視器通過觸發器和其他參數使緩存狀態無效。

// your controller field 
private readonly MemoryCache _dbDataCache = MemoryCache.Default; 

// string key for your cached data 
private const string DbDataCacheSalesCountKey = "DbDataCacheSalesCountKey"; 

public List<DataRow> SalesCount() 
{ 
    if(_dbDataCache.Contans(DbDataCacheSalesCountKey)) 
     return _dbDataCache[DbDataCacheSalesCountKey] as List<DataRow>; 

    string str_salesClass = adding session value here; 
    var lstSales = SalesRepository.SalesCount(SessionValue); 

    // save cache only for 10 minutes 
    var cip = new CacheItemPolicy { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(10)) }; 
    _dbDataCache.Set(DbDataCacheSalesCountKey, lstSales, cip); 
    return lstSales; 
} 

替代組織代碼看起來像下一個和作品一樣具有下面的代碼:

public List<DataRow> SalesCount() 
{ 
    var salesCount = _dbDataCache[DbDataCacheSalesCountKey] as List<DataRow>; 
    if(salesCount == null) 
    { 
     string str_salesClass = adding session value here; 
     salesCount = SalesRepository.SalesCount(SessionValue); 

     // save cache only for 10 minutes 
     var cip = new CacheItemPolicy { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(10)) }; 
     _dbDataCache.Set(DbDataCacheSalesCountKey, salesCount, cip); 
    } 

    return salesCount; 
} 
+0

謝謝瓦迪姆您經過測試,我認爲使用ViewData/ViewBag會更好,非常感謝 – ronibd