Request level singleton object

  • Thread starter Thread starter deostroll
  • Start date Start date
deostroll said:
Hi,

I have written a singleton class that is theoretically supposed to
have a lifetime as that of its request. I plan to store user info into
it. Please refer to the following thread for code:

http://forums.asp.net/t/1365333.aspx

Is the class reliable?

--deostroll

It looks like it should work.

The lock in the GetCurrent method is totally useless, though. As you are
locking using an object that you created on the previous line, it has no
effect what so ever.
 
It looks like it should work.

The lock in the GetCurrent method is totally useless, though. As you are
locking using an object that you created on the previous line, it has no
effect what so ever.

I plan to use the object in different code files. For e.g.

In my default.aspx page's code behind (which is actually a derived
page) I plan to write code like this:

LabelUserID.Text = UserInfo.GetCurrent().UserID.ToString(); //Line A

And, then in my master page's (say master.master, the default.aspx
derives from this) code behind I plan to do something like this:

LabelLoggedIn.Text = "Logged in as " + UserInfo.GetCurrent
().Username; //Line B

Now consider one thread processing default.aspx. It will process
routines in this page first; so when line A gets executed, the
singleton is called for the first time, and the instance get stored to
the httpcontext, and that is returned to us. Processing goes on. The
master page's routines are processed next. So when line B is executed
we get the stored instance to use.

Now, what if two or more users are concurrently requesting this page?
Will there be any conflict?

--deostroll
 
deostroll said:
I plan to use the object in different code files. For e.g.

In my default.aspx page's code behind (which is actually a derived
page) I plan to write code like this:

LabelUserID.Text = UserInfo.GetCurrent().UserID.ToString(); //Line A

And, then in my master page's (say master.master, the default.aspx
derives from this) code behind I plan to do something like this:

LabelLoggedIn.Text = "Logged in as " + UserInfo.GetCurrent
().Username; //Line B

Now consider one thread processing default.aspx. It will process
routines in this page first; so when line A gets executed, the
singleton is called for the first time, and the instance get stored to
the httpcontext, and that is returned to us. Processing goes on. The
master page's routines are processed next. So when line B is executed
we get the stored instance to use.

Yes, except that the pages are not processed as separate units. The
master page is a control in the content page, so it's events will be
executed along with the events of the page. First the PreInit event of
one is executed, then the PreInit event of the other, then the Load
events, then the PreRender events... There are a lot of events, but you
get the picture.
Now, what if two or more users are concurrently requesting this page?
Will there be any conflict?

--deostroll

No, the requests are handled by separate threads, so they have separate
http contexts.
 
Back
Top