.NET MVC - Storing database result during single page result?

Posted by ropstah on Stack Overflow See other posts from Stack Overflow or by ropstah
Published on 2010-03-26T20:35:48Z Indexed on 2010/03/26 21:23 UTC
Read the original article Hit count: 332

Filed under:
|

Fairly simple issue which is solved in PHP by using a static variable.

private static $pages;
public function Pages() {
    if($pages == null) {
        $pages = new PageCollection();
        $pages->findAll();
    }
}

Everywhere in my code I use Pages()::someFindFunction() to make sure the results are fetched only once, and I use that same collection.

I want the same in my .NET MVC application: use something like:

<%=MySite.Pages.findById(1).Title%>

In the code below, if I use a private variable, or if I use a public class with shared variables (doesn't matter) they are both persisted during the entire application.

I want them to load the same way PHP does, once per request. Now where do I store the .NET equivalent of private static $pages, so that the code below works?

            //what to do with $pages??

Public Module MySite
    Public Function Pages() As PageCollection
        If $pages Is Nothing Then
            $pages.loadAll()
        End If
        Return $pages
    End Function
End Module

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about variables