I'm royally confused about ASP.NET now. I believed that everytime you retrieved a webpage business model referred to by that page would be created and that after the display had been sent to the user it would all be torn back down again.
A colleague at work showed me this simple web app which seems to be storing business layer code between page fetches, without using session, cache, application, viewstate, querystrings, or post (as far as I can tell).
The code is as follows:
<form id="form1" runat="server" enableviewstate="False">
<div>
<asp:Label ID="Label1" runat="server" Text="Label" EnableViewState="False"></asp:Label>
<asp:Button ID="Button1"
runat="server" Text="Button" EnableViewState="False"
CausesValidation="False" />
</div>
</form>
Page Load:
PersistanceTest.BLL.Singleton s = PersistanceTest.BLL.Singleton.GetInstance;
Label1.Text = "Counter: " + s.GetCounter;
A开发者_StackOverflow社区nd Singleton class:
public class Singleton
{
private int counter;
private Singleton()
{
counter = 0;
}
private static Singleton theSingleton;
static public Singleton GetInstance
{
get
{
if(theSingleton == null)
{
theSingleton = new Singleton();
}
return theSingleton;
}
}
public int GetCounter
{
get
{
int c = counter;
counter++;
return c;
}
}
}
Now when I load the page, the counter increments each time I press the button. But I have no idea where the Singleton instance is being persisted in the framework? I was sure it wouldn't exist between page loads. Can someone shed some light on this? It's driving me crazy.
I believe Singletons like that one are stored in the Application Domain (AppDomain). When your application restarts, all stored information will be reset as well.
You can read more about AppDomains here: http://odetocode.com/articles/305.aspx
精彩评论