Singletons in Session...?

L

laziers

Hi,
It is a good practice to insert singletons in Session?
I have more than 5 singletons and Im not sure that inserting all in
session state is a good idee :|

singleton looks like this:

public class DAO
{
public static DAO Instance
{
get
{
if (System.Web.HttpContext.Current.Session["DAO"] ==
null)
{
System.Web.HttpContext.Current.Session["DAO"] =
new DAO();
}

return System.Web.HttpContext.Current.Session["DAO"]
as DAO;
}
}

private DAO() { }
}

bye.
 
M

Michael Nemtsev [MVP]

Hello (e-mail address removed),

Could you explain what are u going to reach?

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


l> Hi,
l> It is a good practice to insert singletons in Session?
l> I have more than 5 singletons and Im not sure that inserting all in
l> session state is a good idee :|
l> singleton looks like this:
l>
l> public class DAO
l> {
l> public static DAO Instance
l> {
l> get
l> {
l> if (System.Web.HttpContext.Current.Session["DAO"] ==
l> null)
l> {
l> System.Web.HttpContext.Current.Session["DAO"] =
l> new DAO();
l> }
l> return System.Web.HttpContext.Current.Session["DAO"]
l> as DAO;
l> }
l> }
l> private DAO() { }
l> }
l> bye.
l>
 
L

laziers

Hello (e-mail address removed),

Could you explain what are u going to reach?

I want my DataAccessObject, BusinessLogic classes and Helper classes
to be a singleton

bye bye
 
M

Michael Nemtsev [MVP]

Hello (e-mail address removed),

why not to be singletons for them on the server side. why u wanna keep them
in Session?

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


l> I want my DataAccessObject, BusinessLogic classes and Helper classes
l> to be a singleton
l>
l> bye bye
l>
 
L

laziers

Hello (e-mail address removed),

why not to be singletons for them on the server side. why u wanna keep them
in Session?

because any of each user call the one and the same method

eg.

public DataTable GetUSERS(UInt64 ID)
{
query = "SELECT * FROM users WHERE id = '"+ID+"'";

//etc.
return getData(query);
}

1st user call GetUSERS(1);
2nd user call GetUSERS(2);

and then

1st user:
public DataTable GetUSERS(UInt64 1)
{
query = "SELECT * FROM users WHERE id = '"+1+"'";

//etc.
1st USER IS HERE!
return getData(query);
}

2nd user
public DataTable GetUSERS(UInt64 2)
{
query = "SELECT * FROM users WHERE id = '"+2+"'";
2nd USER IS HERE!
//etc.

return getData(query);
}

and 1st user gets users with ID 2

am I wrong?
 
P

Patrice

Calling code is not a problem in itself as all code runs in its own context
i.e. if properly done you are *really* calling GetUsers(1) for user 1 and
GetUsers(2) for user 2 giving each user a distinct response. The response
that is given by a given call won't be automagically transferred as a
reponse to the other call that is made in a different context...

The problem you could likely have is if this code uses data that are shared
by multiple users (for example most often using a static member). If you
really saw this, you likely have some static data somewhere (either the
argument passed to GetUsers is stored in a static member, or "query" is
defined as a static string or somewhere in GetData you are using a static
dataset or whatever...)
 
L

laziers

Calling code is not a problem in itself as all code runs in its own context
i.e. if properly done you are *really* calling GetUsers(1) for user 1 and
GetUsers(2) for user 2 giving each user a distinct response. The response
that is given by a given call won't be automagically transferred as a
reponse to the other call that is made in a different context...

The problem you could likely have is if this code uses data that are shared
by multiple users (for example most often using a static member). If you
really saw this, you likely have some static data somewhere (either the
argument passed to GetUsers is stored in a static member, or "query" is
defined as a static string or somewhere in GetData you are using a static
dataset or whatever...)

oh, I see, so when I ONLY call a static method like a GetUSERS it will
be everithing ok, yes?

because every user runs a method in the different context :) thats
great. I didnt know that

thanx.

bye bye.
 
P

Patrice

Being a shared *method* or not is not relevant. You should never have any
problem with CODE itself. By design, running several threads in parallel and
saving/restoring context for those threads is a built in mechanism.

The real problem is not the CODE but rather the DATA on which this code
operates as there are no automatic mechanism to handle this so your code
will do exactly what you asked for (including using the same data for
multiple threads of execution which is sometimes what we want and sometimes
not). On the other hand we never want (and we'll never have) executable
statements in multiple threads to interfere with others *just* because they
happen at the same time (but it can because it operates on the same *data*
even if not exactly at the same time)

Try http://en.wikipedia.org/wiki/Thread_(computer_science) and/or
http://en.wikipedia.org/wiki/Thread_safety for details.

--
Patrice

<[email protected]> a écrit dans le message de
(e-mail address removed)...
Calling code is not a problem in itself as all code runs in its own
context
i.e. if properly done you are *really* calling GetUsers(1) for user 1 and
GetUsers(2) for user 2 giving each user a distinct response. The response
that is given by a given call won't be automagically transferred as a
reponse to the other call that is made in a different context...

The problem you could likely have is if this code uses data that are
shared
by multiple users (for example most often using a static member). If you
really saw this, you likely have some static data somewhere (either the
argument passed to GetUsers is stored in a static member, or "query" is
defined as a static string or somewhere in GetData you are using a static
dataset or whatever...)

oh, I see, so when I ONLY call a static method like a GetUSERS it will
be everithing ok, yes?

because every user runs a method in the different context :) thats
great. I didnt know that

thanx.

bye bye.
 
L

laziers

Being a shared *method* or not is not relevant. You should never have any
problem with CODE itself. By design, running several threads in parallel and
saving/restoring context for those threads is a built in mechanism.

The real problem is not the CODE but rather the DATA on which this code
operates as there are no automatic mechanism to handle this so your code
will do exactly what you asked for (including using the same data for
multiple threads of execution which is sometimes what we want and sometimes
not). On the other hand we never want (and we'll never have) executable
statements in multiple threads to interfere with others *just* because they
happen at the same time (but it can because it operates on the same *data*
even if not exactly at the same time)

Tryhttp://en.wikipedia.org/wiki/Thread_%28computer_science%29and/orhttp://en.wikipedia.org/wiki/Thread_safetyfor details.

ok :) thx for reply
 
A

Alvin Bruney [ASP.NET MVP]

Based on the code, the singleton is overkill. Moreso, singleton in a session
variable doesn't solve much

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top