I am unsure about the scope of one static inner class of a non-static class.
In the lines below, would the variable DataContextCreator.Instance (part of a Singleton pattern) point to the same PDataContext instance for all Page objects/page requests or would each object have its own instance?
public class Page : System.Web.UI.Page
{
private class DataContextCreator
{
static DataContextCreator() { }
public static readonly PDataContext Instance = new PDataContext(TUtil.GetConnectionString());
}
public PDataContext DataContext
{
get
{
return DataContextCreator.Instance;
}
}
}
Do I have to work with HttpContext.Current for this to work in the desired way, i.e. no shared DataContext? If so, is there another way?
I am not sure whether modifying the static class to private (as I have done) does the trick.
Thanks!
EDIT: This is my solution where I now have the same lazy-loading capability but without static variables. I don't think I need to put a mutex lock around the holding variable in this case.
private PDataContext _DataContext;
private PDataContext DataContext
{
get
{
if (this._DataContext == null)
{
this._DataContext = new PDataContext(TUtil.GetConnectionString());
}
return this._DataContext;
}
}