Singletons in asp

P

PokerMan

Hey guys

Just a quick question. If i had a static var, then all users coming onto my
site will be sharing the same var. So if i have a class that is a singleton
would the same happen there?

Basically i have a class that will only ever have one instance so, singleton
makes sense. But due to asp and lots of connecting users it had me wondering
if the singleton would work as one isntance per user or one instance for all
users (not what i want)

Thanks
 
S

sloan

First, I'll assume you're speaking of asp.net and not just asp.

They are different things.

Here is an example of a asp.net singleton:

10/24/2005
Web Session Wrapper for storing and retrieving objects
http://sholliday.spaces.live.com/blog/


PS
You should mark
public class WebSessionDataStore

as [Serializable]

and make any objects you put in there serializable if you go to sql server
session state management.
 
P

PokerMan

yes .net sorry.

I am more wanting to know how it will handle a singleton implementation not
how to implement one, tho i cant see an singleton implementation on that
blog? Is it very different to a standard one on .net?



sloan said:
First, I'll assume you're speaking of asp.net and not just asp.

They are different things.

Here is an example of a asp.net singleton:

10/24/2005
Web Session Wrapper for storing and retrieving objects
http://sholliday.spaces.live.com/blog/


PS
You should mark
public class WebSessionDataStore

as [Serializable]

and make any objects you put in there serializable if you go to sql server
session state management.



PokerMan said:
Hey guys

Just a quick question. If i had a static var, then all users coming onto my
site will be sharing the same var. So if i have a class that is a singleton
would the same happen there?

Basically i have a class that will only ever have one instance so, singleton
makes sense. But due to asp and lots of connecting users it had me wondering
if the singleton would work as one isntance per user or one instance for all
users (not what i want)

Thanks
 
P

PokerMan

Ah found ur singletom implementation on the blog, my apologies. And its
similar to mine, not sure why you use a method and dont set the instance as
a property get{} but its the same in essence.

So in answer to my original question a singleton will work as i expect?
Single instance per user, not one instance effected by all users?



sloan said:
First, I'll assume you're speaking of asp.net and not just asp.

They are different things.

Here is an example of a asp.net singleton:

10/24/2005
Web Session Wrapper for storing and retrieving objects
http://sholliday.spaces.live.com/blog/


PS
You should mark
public class WebSessionDataStore

as [Serializable]

and make any objects you put in there serializable if you go to sql server
session state management.



PokerMan said:
Hey guys

Just a quick question. If i had a static var, then all users coming onto my
site will be sharing the same var. So if i have a class that is a singleton
would the same happen there?

Basically i have a class that will only ever have one instance so, singleton
makes sense. But due to asp and lots of connecting users it had me wondering
if the singleton would work as one isntance per user or one instance for all
users (not what i want)

Thanks
 
A

Aidy

If you have static values it is one instance for *all* users. If you want
one instance for *each* user then you could store your object in the
Session.
 
S

sloan

Did you look at entry at the correct date?

private WebSessionDataStore() //constructor
{
this.m_itemCollection = new HybridDictionary();
}
/// <summary>
/// Singleton representing WebSessionDataStore.
/// </summary>
/// <returns></returns>
public static WebSessionDataStore GetInstance()
{
//other stuff here
}


Singleton is a DESIGN PATTERN, not "just an asp.net thing".

http://www.dofactory.com/Patterns/PatternSingleton.aspx


static/shared variables are another thing.


Based on your description of what you want
if the singleton would work as one isntance per user or one instance for

I've shown you how to create a true SINGLETON for the web environment.

You can find more by googling also:
http://www.google.com/search?hl=en&q=singleton+"asp.net"





PokerMan said:
yes .net sorry.

I am more wanting to know how it will handle a singleton implementation not
how to implement one, tho i cant see an singleton implementation on that
blog? Is it very different to a standard one on .net?



sloan said:
First, I'll assume you're speaking of asp.net and not just asp.

They are different things.

Here is an example of a asp.net singleton:

10/24/2005
Web Session Wrapper for storing and retrieving objects
http://sholliday.spaces.live.com/blog/


PS
You should mark
public class WebSessionDataStore

as [Serializable]

and make any objects you put in there serializable if you go to sql server
session state management.



PokerMan said:
Hey guys

Just a quick question. If i had a static var, then all users coming
onto
my
site will be sharing the same var. So if i have a class that is a singleton
would the same happen there?

Basically i have a class that will only ever have one instance so, singleton
makes sense. But due to asp and lots of connecting users it had me wondering
if the singleton would work as one isntance per user or one instance
for
all
users (not what i want)

Thanks
 
S

sloan

My singleton example piggybacks off of SESSION, therefore you will get "one
per user".

I use a method ... because I think thats what GOF (gang of four) does.

http://www.dofactory.com/Patterns/PatternSingleton.aspx
uses a method also.

.....

Again, because I piggy back off of Session, its "one per user".

If I were to piggyback off of Application (object, instead of Session)
(which I actually do in another scenario)....
it would be "all users".




PokerMan said:
Ah found ur singletom implementation on the blog, my apologies. And its
similar to mine, not sure why you use a method and dont set the instance as
a property get{} but its the same in essence.

So in answer to my original question a singleton will work as i expect?
Single instance per user, not one instance effected by all users?



sloan said:
First, I'll assume you're speaking of asp.net and not just asp.

They are different things.

Here is an example of a asp.net singleton:

10/24/2005
Web Session Wrapper for storing and retrieving objects
http://sholliday.spaces.live.com/blog/


PS
You should mark
public class WebSessionDataStore

as [Serializable]

and make any objects you put in there serializable if you go to sql server
session state management.



PokerMan said:
Hey guys

Just a quick question. If i had a static var, then all users coming
onto
my
site will be sharing the same var. So if i have a class that is a singleton
would the same happen there?

Basically i have a class that will only ever have one instance so, singleton
makes sense. But due to asp and lots of connecting users it had me wondering
if the singleton would work as one isntance per user or one instance
for
all
users (not what i want)

Thanks
 
B

bruce barker

it can be coded either way, depends on how you code it.

-- bruce (sqlwork.com)
Ah found ur singletom implementation on the blog, my apologies. And its
similar to mine, not sure why you use a method and dont set the instance as
a property get{} but its the same in essence.

So in answer to my original question a singleton will work as i expect?
Single instance per user, not one instance effected by all users?



sloan said:
First, I'll assume you're speaking of asp.net and not just asp.

They are different things.

Here is an example of a asp.net singleton:

10/24/2005
Web Session Wrapper for storing and retrieving objects
http://sholliday.spaces.live.com/blog/


PS
You should mark
public class WebSessionDataStore

as [Serializable]

and make any objects you put in there serializable if you go to sql server
session state management.



PokerMan said:
Hey guys

Just a quick question. If i had a static var, then all users coming onto my
site will be sharing the same var. So if i have a class that is a singleton
would the same happen there?

Basically i have a class that will only ever have one instance so, singleton
makes sense. But due to asp and lots of connecting users it had me wondering
if the singleton would work as one isntance per user or one instance for all
users (not what i want)

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

singletons 12
Singletons in Session...? 9
Singletons in ASP.NET 2.0 5
Classes and modules are singletons? 15
Alexei's singletons 11
Singletons and Swing 14
Phoenix Singletons 5
C++ singletons and .NET AppDomains 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top