我正在开发一个 ASP.NET MVC 项目。通过具有简单缓存功能的存储库访问数据。它包含几个功能,如以下两个:
Public Function SelectAllCurrencies() As List(Of store_Currency)
Dim AllCurrencies As List(Of store_Currency)
If UseCaching Then
AllCurrencies = HttpContext.Current.Cache("AllCurrencies")
If AllCurrencies Is Nothing Then
AllCurrencies = (From Currencies In db.Currencies Order By Currencies.Title Ascending).ToList
Cache.AddToCache("AllCurrencies", AllCurrencies)
End If
Else
AllCurrencies = (From Currencies In db.Currencies Order By Currencies.Title Ascending).ToList
End If
Return AllCurrencies
End Function
Public Function SelectAllCountries() As List(Of store_Country)
Dim AllCountries As List(Of store_Country)
If UseCaching Then
AllCountries = HttpContext.Current.Cache("AllCountries")
If AllCountries Is Nothing Then
AllCountries = (From Countries In db.Countries Order By Countries.Title Ascending).ToList
Cache.AddToCache("AllCountries", AllCountries)
End If
Else
AllCountries = (From Countries In db.Countries Order By Countries.Title Ascending).ToList
End If
Return AllCountries
End Function
如您所见,他们一遍又一遍地使用相同的工作流程。我想删除这种冗余。我认为泛型应该提供一个解决方案,但我一生都无法弄清楚如何处理泛型SelectAllEntities(Of T)
函数中的 LINQ 语句。有没有办法“概括”查询,也许是动态 LINQ?