singleton pattern

  • Thread starter Thread starter donet programmer
  • Start date Start date
D

donet programmer

Can somebody comment on the usage of the singleton pattern for
maintaining session variables??

Thanks
 
Can somebody comment on the usage of the singleton pattern for
maintaining session variables??

Thanks

You do NOT want to do that, as this means that ALL visitors of your
site share the same "session" variables.
For a webapp there is 1 application running that will serve multiple
request from mutiple users. This means there is one singleton that is
shared by all visitors.

Why not use "Session" to store session variables?

Hans Kesting
 
Thanks for the prompt response, maintaining all the session variables
in a class would be easier to do than maintaining variables in Session
object collection.
One of the other reasons I wanted to go this route was so that I can
pass the session information to my business and data layers without
much hassle....
 
session uses the singleton pattern. you can piggyback on it. you could also
use one session object that you store in session or your object could use
the session singleton (HtttpContext.Current.Session) to access session
variables.

-- bruce (sqlwork.com)
 
In many situations, It does make sense to use one object to to hold
various information, that is, one session variable v.s. multiple
session variables.

But, this has nothing to do with singleton PATTERN. A singleton is a
class with only one instance that is shared by the many.
 
Just responded then saw bruce's post. Yes, Session object itself is a
singleton. But I don't think that's what the original poster meant.
 
Thanks ya all for taking time and providing me with the valuable input.

I understand now that singleton is not really the solution for what I
was trying to achieve ...
The pattern I am interested in is the one that would allow me to pass
session information from my UI layer to the business layer.

Here is a more verbose version of the problem:
- I have an application consisting of 3 layers: UI, Business and Data.
- My UI layer is the ASP.NET Web application that contain my Web Forms
(.aspx pages) with a code behind.
- In the code behind I handle page events and create business layer
instances that perform the functionality the page provides.
- To these business layer objects I need to pass the session
information. This needs to be done for all classes in the Business
Layer.
- One not so good way of doing this would be to do something like this
on the page event:
// instantiate the Business layer class
BusinessLayerClass1 objBLClass1 = new BusinessLayerClass1();
// assign the session from the page to the Session member
variable
objBLClass1.Session = Page.Session["mySessionItem"];

I need to initialize this session variable everytime I need to write an
event handler on the UI layer.
Is there a pattern that I can use or can somebody suggest so that when
I instantiate an object of Business layer I dont also have to pass
this session information everytime.
 
Thanks ya all for taking time and providing me with the valuable input.
I understand now that singleton is not really the solution for what I
was trying to achieve ...
The pattern I am interested in is the one that would allow me to pass
session information from my UI layer to the business layer.

Here is a more verbose version of the problem:
- I have an application consisting of 3 layers: UI, Business and Data.
- My UI layer is the ASP.NET Web application that contain my Web Forms
(.aspx pages) with a code behind.
- In the code behind I handle page events and create business layer
instances that perform the functionality the page provides.
- To these business layer objects I need to pass the session
information. This needs to be done for all classes in the Business
Layer.
- One not so good way of doing this would be to do something like this
on the page event:
// instantiate the Business layer class
BusinessLayerClass1 objBLClass1 = new BusinessLayerClass1();
// assign the session from the page to the Session member
variable
objBLClass1.Session = Page.Session["mySessionItem"];

I need to initialize this session variable everytime I need to write an
event handler on the UI layer.
Is there a pattern that I can use or can somebody suggest so that when
I instantiate an object of Business layer I dont also have to pass
this session information everytime.


You don't have to pass the session variable explicitly to that business
object. From within that business object you can access the Session by:
System.Web.HttpContext.Current.Session

some notes:
- the project needs a reference to the System.Web.dll
- this only works if it is really called from within an HttpContext
(that is, as a direct result of a Request)


And, as said before, instead of storing a lot of disconnected int's and
strings, you can put all of them into some statebag class that is
stored as a single session-object (per user)

Hans Kesting
 
Session is a way to persist data. The BL generally should not contain
session. And it looks to me that "mySessionItem" is the data should be
inside the BL. I would also simply put the BL in to the session so it
is accesseble to all pages. A big problem with the approach is the life
span of the session and will expire. Also this approach may persist
more data than you wanted.

Hans said:
Thanks ya all for taking time and providing me with the valuable input.

I understand now that singleton is not really the solution for what I
was trying to achieve ...
The pattern I am interested in is the one that would allow me to pass
session information from my UI layer to the business layer.

Here is a more verbose version of the problem:
- I have an application consisting of 3 layers: UI, Business and Data.
- My UI layer is the ASP.NET Web application that contain my Web Forms
(.aspx pages) with a code behind.
- In the code behind I handle page events and create business layer
instances that perform the functionality the page provides.
- To these business layer objects I need to pass the session
information. This needs to be done for all classes in the Business
Layer.
- One not so good way of doing this would be to do something like this
on the page event:
// instantiate the Business layer class
BusinessLayerClass1 objBLClass1 = new BusinessLayerClass1();
// assign the session from the page to the Session member
variable
objBLClass1.Session = Page.Session["mySessionItem"];

I need to initialize this session variable everytime I need to write an
event handler on the UI layer.
Is there a pattern that I can use or can somebody suggest so that when
I instantiate an object of Business layer I dont also have to pass
this session information everytime.


You don't have to pass the session variable explicitly to that business
object. From within that business object you can access the Session by:
System.Web.HttpContext.Current.Session

some notes:
- the project needs a reference to the System.Web.dll
- this only works if it is really called from within an HttpContext
(that is, as a direct result of a Request)


And, as said before, instead of storing a lot of disconnected int's and
strings, you can put all of them into some statebag class that is
stored as a single session-object (per user)

Hans Kesting
 
Thanks ya all for taking time and providing me with the valuable input.

I understand now that singleton is not really the solution for what I
was trying to achieve ...
The pattern I am interested in is the one that would allow me to pass
session information from my UI layer to the business layer.

Here is a more verbose version of the problem:
- I have an application consisting of 3 layers: UI, Business and Data.
- My UI layer is the ASP.NET Web application that contain my Web Forms
(.aspx pages) with a code behind.
- In the code behind I handle page events and create business layer
instances that perform the functionality the page provides.
- To these business layer objects I need to pass the session
information. This needs to be done for all classes in the Business
Layer.
- One not so good way of doing this would be to do something like this
on the page event:
// instantiate the Business layer class
BusinessLayerClass1 objBLClass1 = new BusinessLayerClass1();
// assign the session from the page to the Session member
variable
objBLClass1.Session = Page.Session["mySessionItem"];

I need to initialize this session variable everytime I need to write an
event handler on the UI layer.
Is there a pattern that I can use or can somebody suggest so that when
I instantiate an object of Business layer I dont also have to pass
this session information everytime.
I would consider creating a wrapper class for the Session object. The
wrapper's properties can read and write to the Session, contain the
required casting, and perform whatever validation might be needed. The
wrapper class becomes a business layer between the UI's code behind
and the Session as a data layer.

The business layer types would all have constructors that take
business data types as an argument. That will get rid of the external
assignments to set the business layer types' properties. The business
data type is just a container for the data needed by a business layer
class. When this type is created it will need to be populated with
values from the Session. The session wrapper class can have properties
or methods that create, populate and return the needed business data
types.

This indirection does not eliminate the need for ugly code with all
the casting, grunt-work assignments etc. The benefit is that code will
be will be written only once and it all will be encapsulated within
the wrapper class. The business object's c'tors will need to make some
assignments but again that code will be written only once. By written
once I mean the code will not be a snippet that is used over and over
again.

bizDataA dataA = SessionWrapper.getBizDataA();
BusinessLayerClass1 objBLClass1 = new BusinessLayerClass1(dataA);
....

regards
A.G.
 
Thanks again to everybody ...
I did not quite understand how the wrapper class is going to work and
it looks a complex approach ...
I did like the idea of referencing System.Web.dll and using
System.Web.HttpContext.Current.Session
since I want my business layer classes to be separate from the UI and
dont want them referencing System.Web ... I am going to create another
project that will reference System.Web and hold the current session
values ... Next I can reference this new Session project in my Business
Layer project ... :-) ... I think that should be a good solution
Thanks again .. have a great weekend


Registered said:
Thanks ya all for taking time and providing me with the valuable input.

I understand now that singleton is not really the solution for what I
was trying to achieve ...
The pattern I am interested in is the one that would allow me to pass
session information from my UI layer to the business layer.

Here is a more verbose version of the problem:
- I have an application consisting of 3 layers: UI, Business and Data.
- My UI layer is the ASP.NET Web application that contain my Web Forms
(.aspx pages) with a code behind.
- In the code behind I handle page events and create business layer
instances that perform the functionality the page provides.
- To these business layer objects I need to pass the session
information. This needs to be done for all classes in the Business
Layer.
- One not so good way of doing this would be to do something like this
on the page event:
// instantiate the Business layer class
BusinessLayerClass1 objBLClass1 = new BusinessLayerClass1();
// assign the session from the page to the Session member
variable
objBLClass1.Session = Page.Session["mySessionItem"];

I need to initialize this session variable everytime I need to write an
event handler on the UI layer.
Is there a pattern that I can use or can somebody suggest so that when
I instantiate an object of Business layer I dont also have to pass
this session information everytime.
I would consider creating a wrapper class for the Session object. The
wrapper's properties can read and write to the Session, contain the
required casting, and perform whatever validation might be needed. The
wrapper class becomes a business layer between the UI's code behind
and the Session as a data layer.

The business layer types would all have constructors that take
business data types as an argument. That will get rid of the external
assignments to set the business layer types' properties. The business
data type is just a container for the data needed by a business layer
class. When this type is created it will need to be populated with
values from the Session. The session wrapper class can have properties
or methods that create, populate and return the needed business data
types.

This indirection does not eliminate the need for ugly code with all
the casting, grunt-work assignments etc. The benefit is that code will
be will be written only once and it all will be encapsulated within
the wrapper class. The business object's c'tors will need to make some
assignments but again that code will be written only once. By written
once I mean the code will not be a snippet that is used over and over
again.

bizDataA dataA = SessionWrapper.getBizDataA();
BusinessLayerClass1 objBLClass1 = new BusinessLayerClass1(dataA);
...

regards
A.G.
 
Back
Top