Singletons in Session...?
Singletons in Session...?
am 28.01.2008 10:35:19 von 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.
Re: Singletons in Session...?
am 28.01.2008 10:54:17 von nemtsev
Hello laziers@gmail.com,
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>
Re: Singletons in Session...?
am 28.01.2008 11:05:15 von laziers
On 28 Sty, 10:54, Michael Nemtsev [MVP] wrote:
> Hello lazi...@gmail.com,
>
> Could you explain what are u going to reach?
>
I want my DataAccessObject, BusinessLogic classes and Helper classes
to be a singleton
bye bye
Re: Singletons in Session...?
am 28.01.2008 11:39:08 von nemtsev
Hello laziers@gmail.com,
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> On 28 Sty, 10:54, Michael Nemtsev [MVP] wrote:
l>
>> Hello lazi...@gmail.com,
>>
>> Could you explain what are u going to reach?
>>
l> I want my DataAccessObject, BusinessLogic classes and Helper classes
l> to be a singleton
l>
l> bye bye
l>
Re: Singletons in Session...?
am 28.01.2008 12:36:55 von laziers
On 28 Sty, 11:39, Michael Nemtsev [MVP] wrote:
> Hello lazi...@gmail.com,
>
> 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?
Re: Singletons in Session...?
am 28.01.2008 13:06:34 von 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...)
--
Patrice
a écrit dans le message de news:
4b44b6e1-47a9-4e6c-9a54-79f69c4dfb86@j20g2000hsi.googlegroup s.com...
> On 28 Sty, 11:39, Michael Nemtsev [MVP] wrote:
>> Hello lazi...@gmail.com,
>>
>> 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?
Re: Singletons in Session...?
am 28.01.2008 14:19:51 von laziers
On 28 Sty, 13:06, "Patrice" wrote:
> Calling code is not a problem in itself as all code runs in its own contex=
t
> 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 share=
d
> 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.
> --
> Patrice
>
> a =E9crit dans le message de news:
> 4b44b6e1-47a9-4e6c-9a54-79f69c4df...@j20g2000hsi.googlegroup s.com...
>
>
>
> > On 28 Sty, 11:39, Michael Nemtsev [MVP] wrote:
> >> Hello lazi...@gmail.com,
>
> >> 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 =3D "SELECT * FROM users WHERE id =3D '"+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 =3D "SELECT * FROM users WHERE id =3D '"+1+"'";
>
> > //etc.
> >>1st USER IS HERE!
> > return getData(query);
> > }
>
> > 2nd user
> > public DataTable GetUSERS(UInt64 2)
> > {
> > query =3D "SELECT * FROM users WHERE id =3D '"+2+"'";
> >>2nd USER IS HERE!
> > //etc.
>
> > return getData(query);
> > }
>
> > and 1st user gets users with ID 2
>
> > am I wrong?- Ukryj cytowany tekst -
>
> - Poka=BF cytowany tekst -
Re: Singletons in Session...?
am 28.01.2008 15:04:32 von 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_%28computer_science%29 and/or
http://en.wikipedia.org/wiki/Thread_safety for details.
--
Patrice
a écrit dans le message de news:
34ace649-5769-44ce-bf02-d415197049bb@y5g2000hsf.googlegroups .com...
On 28 Sty, 13:06, "Patrice" wrote:
> 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.
> --
> Patrice
>
> a écrit dans le message de news:
> 4b44b6e1-47a9-4e6c-9a54-79f69c4df...@j20g2000hsi.googlegroup s.com...
>
>
>
> > On 28 Sty, 11:39, Michael Nemtsev [MVP] wrote:
> >> Hello lazi...@gmail.com,
>
> >> 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?- Ukryj cytowany tekst -
>
> - Poka¿ cytowany tekst -
Re: Singletons in Session...?
am 28.01.2008 15:37:17 von laziers
On 28 Sty, 15:04, "Patrice" wrote:
> 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%2 9and/orhttp://en.wikipedia.org/wiki/Thread_safetyfor details.
>
> --
> Patrice
>
ok :) thx for reply
Re: Singletons in Session...?
am 28.01.2008 17:21:18 von Alvin Bruney
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
-------------------------------------------------------
wrote in message
news:4b44b6e1-47a9-4e6c-9a54-79f69c4dfb86@j20g2000hsi.google groups.com...
> On 28 Sty, 11:39, Michael Nemtsev [MVP] wrote:
>> Hello lazi...@gmail.com,
>>
>> 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?