D
donet programmer
Can somebody comment on the usage of the singleton pattern for
maintaining session variables??
Thanks
maintaining session variables??
Thanks
maintaining session variables??
Thanks
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.
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
I would consider creating a wrapper class for the Session object. TheThanks 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.
Registered said:I would consider creating a wrapper class for the Session object. TheThanks 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.
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.