473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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 Default1:Page
{
Singleton s = Singleton.getInstance();
void Page_Load(...)
{...}
void Button1Clicked(...)
{...}
}
public partial class Default2:Page
{
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

Feb 21 '06 #1
5 1973
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" <lo******@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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 Default1:Page
{
Singleton s = Singleton.getInstance();
void Page_Load(...)
{...}
void Button1Clicked(...)
{...}
}
public partial class Default2:Page
{
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

Feb 21 '06 #2
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" <lo******@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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 Default1:Page
{
Singleton s = Singleton.getInstance();
void Page_Load(...)
{...}
void Button1Clicked(...)
{...}
}
public partial class Default2:Page
{
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

Feb 21 '06 #3
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" <lo******@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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 Default1:Page
{
Singleton s = Singleton.getInstance();
void Page_Load(...)
{...}
void Button1Clicked(...)
{...}
}
public partial class Default2:Page
{
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

Feb 21 '06 #4
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

Feb 22 '06 #5
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" <lo******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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

Feb 22 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: aa | last post by:
I am migrating to PHP from ASP where there are the Application Scope variables which are accessible from any page on a website and which are used, in particular, for hit counters. Is there a similar...
0
by: mabrin | last post by:
Hi, I have a simple web application with some web controls. And those controls share some information that are depending on the request url. My first idea was I create an object at...
3
by: Simon Niederberger | last post by:
How can I give a user-defined object session scope? From http://www.microsoft.com/windows2000/en/server/iis/default.asp?url=/windows2000/en/server/iis/htm/asp/iiwaobu.htm I gather that it can...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
4
by: Srini | last post by:
Hi , Can anyone tell me when I can use singleton pattern. Will it be good for implementing the dataaccess Layer. Will it be usefull for the buisness object layer . Could you give me a practicle...
1
by: Christopher Pragash | last post by:
Hello All, I created a class object and a console application to host the object as a Singleton Remoting Object. I overloaded the new inside the class object and loaded a private Arraylist with...
10
by: newjazzharmony | last post by:
All, When I create a class with a static member variable and reference that class in a console applicaton it appears as though each instance of the application has its own separate copy of the...
1
by: spolsky | last post by:
i use an application scope recordset object for holding the list of online users. i am not sure that if i have to handle concurrency issues like one user is looping through the recordset for...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.