开发者

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

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


1st, are you sure you want a module? Normally, you'd use a class.

Public Class MySiteController
    Private _pages as PageCollection
    ReadOnly Property Pages As PageCollection 
        Get
          If _pages Is Nothing Then 
            _pages = New PageCollection
            _pages.FindAll
          End If 
          Return _pages 
        End Get 
    End Function 

    ... other code here...

    '' use it as:  Pages.FindById(1)
End Class
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜