How to: one database call per request in ASP.NET MVC using L2S
How to fetch all data required for processing a request in one call to DB. Also I want to know how will I do this if my master page also requires some data from database? I know that its clarified in Passing data to Master Page in ASP.NET MVC. But I think that will make multiple calls to database. Please let me know i开发者_开发问答f it's not like that.
UPDATE
+1 and thanks to all answerers/commenter s for their views and advice.I think you might be confusing one session per request
and SELECT N + 1
scenarios. It is perfectly fine to have multiple calls to the database, however you want to watch out for when you have to do one database call per item in a list. This is most commonly encountered when using lazy loaded lists. The solution is to use eager fetching.
ps. I think the popular solution for getting data to the master page is to use Html.RenderAction
in the master page. That way you don't have to worry about master page concerns (like navigation) all over the place.
I have to agree with the other answers, what is your justification for this?
NHibernate can batch SQL statements using FUTURES
, have you read this blog post?
However said I am not sure how you would combine all possibilities for all of your views on your site. If you do come up a solution I would love to see it.
This is not going to be easy and showing a small code example is not possible.
It's a bit of a tall order to limit yourself to only one database hit per request. What are your reasons for doing this? Is it just a fun challenge you've set yourself? If it's for optimisation reasons, there are better ways of achieving this: optimising queries, caching etc.
精彩评论