Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > how did a singleton object achieve application scope in asp.net when it was declared a page variable?

Reply
Thread Tools

how did a singleton object achieve application scope in asp.net when it was declared a page variable?

 
 
Rich
Guest
Posts: n/a
 
      02-21-2006
The following code produced a singleton object with application scope
when it should have had page scope:

public class Singleton
{
private static Singleton uniqueInstance = null;
private Singleton()
{
}
public static Singleton getInstance()
{
if (uniqueInstance == null)
uniqueInstance = new Singleton();
return uniqueInstance;
}
}
public partial class Default1age
{
Singleton s = Singleton.getInstance();
void Page_Load(...)
{...}
void Button1Clicked(...)
{...}
}
public partial class Default2age
{
Singleton s = Singleton.getInstance(); // this retrieved the
instance from Default1
void Page_Load(...)
{...}
void Button1Clicked(...)
{...}
}



Can anyone please explain how a Singleton object created within a Page
class is able to get application scope?


Rich

 
Reply With Quote
 
 
 
 
Rune B
Guest
Posts: n/a
 
      02-21-2006
sure, it's stored for all users in the static field " private static
Singleton uniqueInstance; "
-- Instead you can save it in the HttpContext.Items collection, so it is a
"singleton per HttpRequest" instead:

public class Singleton
{
private Singleton()
{
}

static Singleton(){
_ticket = new object();
}

private static object _ticket;

public static Singleton getInstance(HttpContext context)
{
Singleton uniqueInstance = context.Items[_ticket] as Singleton;
if(uniqueInstance == null)
{
uniqueInstance = new Singleton();
context.Items.Add(_ticket, uniqueInstance);
}
return uniqueInstance;
}

}

R-)



"Rich" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> The following code produced a singleton object with application scope
> when it should have had page scope:
>
> public class Singleton
> {
> private static Singleton uniqueInstance = null;
> private Singleton()
> {
> }
> public static Singleton getInstance()
> {
> if (uniqueInstance == null)
> uniqueInstance = new Singleton();
> return uniqueInstance;
> }
> }
> public partial class Default1age
> {
> Singleton s = Singleton.getInstance();
> void Page_Load(...)
> {...}
> void Button1Clicked(...)
> {...}
> }
> public partial class Default2age
> {
> Singleton s = Singleton.getInstance(); // this retrieved the
> instance from Default1
> void Page_Load(...)
> {...}
> void Button1Clicked(...)
> {...}
> }
>
>
>
> Can anyone please explain how a Singleton object created within a Page
> class is able to get application scope?
>
>
> Rich
>



 
Reply With Quote
 
 
 
 
sloan
Guest
Posts: n/a
 
      02-21-2006
Check my blog

http://spaces.msn.com/sholliday/

10/24/2005

Everywhere I have
System.Web.HttpContext.Current.Session

You'd replace with
System.Web.HttpContext.Current.Application (going from memory)





"Rich" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> The following code produced a singleton object with application scope
> when it should have had page scope:
>
> public class Singleton
> {
> private static Singleton uniqueInstance = null;
> private Singleton()
> {
> }
> public static Singleton getInstance()
> {
> if (uniqueInstance == null)
> uniqueInstance = new Singleton();
> return uniqueInstance;
> }
> }
> public partial class Default1age
> {
> Singleton s = Singleton.getInstance();
> void Page_Load(...)
> {...}
> void Button1Clicked(...)
> {...}
> }
> public partial class Default2age
> {
> Singleton s = Singleton.getInstance(); // this retrieved the
> instance from Default1
> void Page_Load(...)
> {...}
> void Button1Clicked(...)
> {...}
> }
>
>
>
> Can anyone please explain how a Singleton object created within a Page
> class is able to get application scope?
>
>
> Rich
>



 
Reply With Quote
 
Karl Seguin [MVP]
Guest
Posts: n/a
 
      02-21-2006
There's a great write-up in singleton pattern in C# and making it thread
safe - your implementation isn't - at:
http://www.yoda.arachsys.com/csharp/singleton.html

But whatever mechanics you use to implement a singleton, static fields are
scoped to the entire appDomain/application. I'm not sure why you think it
should be page scoped, when you declare a static field there's only one
instance for the entire appdomain..that's just how it works

What exact scope are you looking for? For a given page and all users (create
a static field in the page)? For a given user (store in the session)? For a
given request(store in the HttpContext)...

Karl
--
http://www.openmymind.net/



"Rich" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> The following code produced a singleton object with application scope
> when it should have had page scope:
>
> public class Singleton
> {
> private static Singleton uniqueInstance = null;
> private Singleton()
> {
> }
> public static Singleton getInstance()
> {
> if (uniqueInstance == null)
> uniqueInstance = new Singleton();
> return uniqueInstance;
> }
> }
> public partial class Default1age
> {
> Singleton s = Singleton.getInstance();
> void Page_Load(...)
> {...}
> void Button1Clicked(...)
> {...}
> }
> public partial class Default2age
> {
> Singleton s = Singleton.getInstance(); // this retrieved the
> instance from Default1
> void Page_Load(...)
> {...}
> void Button1Clicked(...)
> {...}
> }
>
>
>
> Can anyone please explain how a Singleton object created within a Page
> class is able to get application scope?
>
>
> Rich
>



 
Reply With Quote
 
Rich
Guest
Posts: n/a
 
      02-22-2006
Thanks all for the response. In regard to Karl's, wouldn't a given
request automatically have a session associated with that request (ie 1
user = 1 request)?

Rich

 
Reply With Quote
 
Karl Seguin [MVP]
Guest
Posts: n/a
 
      02-22-2006
A request is shorter than lived than a session. Yes a request is associated
with a session,but you can scope a singleton to live only for the individual
request - subsequent request (made by the same user in the same session)
would have a new instance.

Karl

--
http://www.openmymind.net/



"Rich" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Thanks all for the response. In regard to Karl's, wouldn't a given
> request automatically have a session associated with that request (ie 1
> user = 1 request)?
>
> Rich
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton object vs. enhancing singleton class Paul McMahon Ruby 3 06-09-2008 06:05 AM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM
declared or not declared ? JohnZing ASP .Net 3 02-05-2006 08:38 PM



Advertisments