I am developing 4 laayered web application which includes Presentation, Service, Business and DataAccess. I am confused how i maaintain session between layers or this is a bad ideaa?
Eg: I have 2 Business Class named Application and User. In the Application i have a method named CreateUser which contains User entity as paaramater. In User class i haave a method getEmaail(). Here i need to retrieve email for the curent login user so i
dont need to send any paraameter like user id..
Generally for this purpose, maintain one session variable UserID (Session["UserID]) when the user logged in and access the session where ever needed. But in layered archicture :
how can i use this?
Which layer i need to take this session variable ?
Can anyone tell me how you people handled this scenario?
Regards,
Akhil Raj K R
Please Mark as Answer if it helps u...
If you want to reference a session variable from a class in a layer somewhere, you need to use HttpContext.Current (http://odetocode.com/articles/112.aspx). You do not need to explicitly maintain sessions
across layers. ASP.NET is responsible for maintaining sessions. HttpContext.Current gives you access the to the current request context from anywhere within the application domain.
Thanks for your answer. But which laayer is best choice. I am thinking to handle in Business layer and pass the paramateres to DAL explicely.
Which is best practice?
And when i call HttpContext.Current.Session["ID"], it saays null reference. I specify Session["ID"] in the page load event of webpage. Why? Aany code needed in Service layer to access
this session in business layer?
Regards,
Akhil Raj K R
Please Mark as Answer if it helps u...
But which layer is best choice. I am thinking to handle in Business layer and pass the paramateres to DAL explicely.
Actually I recommend extracting all references to any type of configuration, viewstate, session, application values into a new
vertical layer called an Infrastructure Layer. Where you traditionally think of the 3 horizontal layers in an application as UI-BLL-DAL, add an Infrastructure layer that goes vertically alongside and facilitates to
all layers of the application.
This type of layer is great for extracting configuration data from the host, 'helper/utility' methods, etc. that really need to be present in all layers. Doing so in this manner prevents the need to pass values along, or try to get to the HttpContext in
multiple different layers. Most everything in this layer is accessible via Static or Shared methods since there really is no cohesive and common behavior to encapsulate like a business object or entity.
Also by wrapping and re-exposing configuration or session values by Shared properties, then can be accessed through Intellisense which is much preferred over accessing values by hardcoding a key name all throughout the application.
The Infrastructure layer will be referenced by each layer that needs it in the application (typically all of the layers).
I recommend using Session to store data and access across the layer. Instead of using UserID in the session object, create a new object like UserEO and store the value, use across the layer so that it will be compile time safety with objects & also inherit
contract between layers.
The session variables should be ideally referenced in only the presentation layer and while accessing its a better approch not to use Session directly.
Rather create a SessionWrapper or Adapter and better if this could be on its own assembly, which is referenced in the presentation layer.
This is also beneficial as you can unit test your code and swap the asp.net session with mock session object.
I blog at http://rajeshpillai.net and have a community startup http://ownabook.org/
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)
thanks friends. Anway from all suggesions i got that i need to create a infracture layer project (C# project in business layer) like business entities and add this project reference to all other layer projects. This infracture project includes take session
variables and give as properties. So i can take the user id in the DAL itseld using these properties. Am i right?
I have a confusion here that how i set the session variables? I mean using session from web application or i set the property of infracture laayer(i think this is again not session based)?
Regards,
Akhil Raj K R
Please Mark as Answer if it helps u...
This infracture project includes take session variables and give as properties. So i can take the user id in the DAL itseld using these properties. Am i right?
Yes correct. All of the session values could be re-exposed as Shared/Static properties. You always Get/Set them via the Infrastructure layer and
not directly. This way you can control casting, etc in a central place too. In your Infrastructure Layer make sure to add a reference to System.Web so you have access to the Session.
You always Get/Set them via the Infrastructure layer and not directly
I din't get this sentence. Actually as per my understanding in the presentation layer (web application) i need to set the session variable like below..
Session["ID"] = UserID;
So where i set these session variable to property in the infracture layer? any sample? Because her i simply set session only. So how this session variable set to the property
Regards,
Akhil Raj K R
Please Mark as Answer if it helps u...
akhilrajau
Participant
1744 Points
537 Posts
Session in Layared Design
Apr 03, 2012 06:00 AM|LINK
hi all,
I am developing 4 laayered web application which includes Presentation, Service, Business and DataAccess. I am confused how i maaintain session between layers or this is a bad ideaa?
Eg: I have 2 Business Class named Application and User. In the Application i have a method named CreateUser which contains User entity as paaramater. In User class i haave a method getEmaail(). Here i need to retrieve email for the curent login user so i dont need to send any paraameter like user id..
Generally for this purpose, maintain one session variable UserID (Session["UserID]) when the user logged in and access the session where ever needed. But in layered archicture :
how can i use this?
Which layer i need to take this session variable ?
Can anyone tell me how you people handled this scenario?
Akhil Raj K R
Please Mark as Answer if it helps u...
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Session in Layared Design
Apr 03, 2012 06:20 AM|LINK
If you want to reference a session variable from a class in a layer somewhere, you need to use HttpContext.Current (http://odetocode.com/articles/112.aspx). You do not need to explicitly maintain sessions across layers. ASP.NET is responsible for maintaining sessions. HttpContext.Current gives you access the to the current request context from anywhere within the application domain.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
akhilrajau
Participant
1744 Points
537 Posts
Re: Session in Layared Design
Apr 03, 2012 07:17 AM|LINK
Thanks for your answer. But which laayer is best choice. I am thinking to handle in Business layer and pass the paramateres to DAL explicely.
Which is best practice?
And when i call HttpContext.Current.Session["ID"], it saays null reference. I specify Session["ID"] in the page load event of webpage. Why? Aany code needed in Service layer to access this session in business layer?
Akhil Raj K R
Please Mark as Answer if it helps u...
tdesai
Member
151 Points
34 Posts
Re: Session in Layared Design
Apr 03, 2012 08:37 AM|LINK
I would strongly reccoment managing sessions only at presentation layer and not using them directly in businuess or data access layer.
Pass values as parameters to your Serive, Business & DataAccess layers.
ForExample:
public string GetUserEmail(int userId) { }atconway
All-Star
16846 Points
2756 Posts
Re: Session in Layared Design
Apr 03, 2012 05:56 PM|LINK
Actually I recommend extracting all references to any type of configuration, viewstate, session, application values into a new vertical layer called an Infrastructure Layer. Where you traditionally think of the 3 horizontal layers in an application as UI-BLL-DAL, add an Infrastructure layer that goes vertically alongside and facilitates to all layers of the application.
This type of layer is great for extracting configuration data from the host, 'helper/utility' methods, etc. that really need to be present in all layers. Doing so in this manner prevents the need to pass values along, or try to get to the HttpContext in multiple different layers. Most everything in this layer is accessible via Static or Shared methods since there really is no cohesive and common behavior to encapsulate like a business object or entity.
Also by wrapping and re-exposing configuration or session values by Shared properties, then can be accessed through Intellisense which is much preferred over accessing values by hardcoding a key name all throughout the application.
The Infrastructure layer will be referenced by each layer that needs it in the application (typically all of the layers).
Hope this helps!
PSP_152890
Participant
783 Points
154 Posts
Re: Session in Layared Design
Apr 03, 2012 06:31 PM|LINK
I recommend using Session to store data and access across the layer. Instead of using UserID in the session object, create a new object like UserEO and store the value, use across the layer so that it will be compile time safety with objects & also inherit contract between layers.
Saravana Prakash P.
thinkrajesh
Participant
1356 Points
232 Posts
Re: Session in Layared Design
Apr 03, 2012 08:15 PM|LINK
The session variables should be ideally referenced in only the presentation layer and while accessing its a better approch not to use Session directly.
Rather create a SessionWrapper or Adapter and better if this could be on its own assembly, which is referenced in the presentation layer.
This is also beneficial as you can unit test your code and swap the asp.net session with mock session object.
(Don't forget to click "Mark as Answer" on the post(s) that helped you.)
akhilrajau
Participant
1744 Points
537 Posts
Re: Session in Layared Design
Apr 04, 2012 04:54 AM|LINK
thanks friends. Anway from all suggesions i got that i need to create a infracture layer project (C# project in business layer) like business entities and add this project reference to all other layer projects. This infracture project includes take session variables and give as properties. So i can take the user id in the DAL itseld using these properties. Am i right?
I have a confusion here that how i set the session variables? I mean using session from web application or i set the property of infracture laayer(i think this is again not session based)?
Akhil Raj K R
Please Mark as Answer if it helps u...
atconway
All-Star
16846 Points
2756 Posts
Re: Session in Layared Design
Apr 04, 2012 08:40 PM|LINK
Yes correct. All of the session values could be re-exposed as Shared/Static properties. You always Get/Set them via the Infrastructure layer and not directly. This way you can control casting, etc in a central place too. In your Infrastructure Layer make sure to add a reference to System.Web so you have access to the Session.
akhilrajau
Participant
1744 Points
537 Posts
Re: Session in Layared Design
Apr 05, 2012 02:08 AM|LINK
Thanks friend.
I din't get this sentence. Actually as per my understanding in the presentation layer (web application) i need to set the session variable like below..
Session["ID"] = UserID;
So where i set these session variable to property in the infracture layer? any sample? Because her i simply set session only. So how this session variable set to the property
Akhil Raj K R
Please Mark as Answer if it helps u...