It is possible to enable a cache that start ad the beginning of the request and ends at the end of the request ??
For some tables it will useful to enable a cache that not select the same record more that one time.
For example when I render a a partial more that one time the SELECT inside the partial are unnecessary.
Assume this snippet:
@foreach(var row in orders)
{
@{Html.RenderPartial("Order");}
}
And the partial Order is:
<div>
@session.Query<Langs>.SingleOrDefault(el => el.Id == "EN').Description
</div>
<div>
@Model.OrderID
</div>
It is possible to enable a cache that cache the “Langs” table only in the current session ?
Without cache I have N (Count of Orders) SELECT per request, otherwise with request cache I will have 1 SELECT per request.
Thank You!
You should not be executing any database queries from within any view such as the partial you mentioned. This will almost always lead to a SELECT N+1 scenario.
Follow an MVC pattern and do not mix the concerns. Any database queries should be initiated from a repository layer, then populate a model object that represents what data that the view needs, then pass that model to the view.
With programming, anything is possible and there are always a million ways to do the same thing but following best practices and separation of concerns will save you from yourself and help you to build efficient, extendable and maintainable applications.
To reiterate, whatever data your view needs access to, do all of that querying on the Controller-side of the MVC pattern. Doing it any other way is mixing concerns and leads to situations such as the one you are encountering.