Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session("MyVar") into objects "automatically"?

Status
Not open for further replies.

johnfraser

Programmer
Jul 25, 2007
33
US
I have an application that relies on 5-6 fundementle objects to run properly. They are session level objects that are accessed through out the application.

My question is... what is the perferred method to access these throughout the application?

I could:

provide a static property which handled the "unsafe" style of (MyClass)Session("MyVar"); and return the correct type

access Session each and every time.

I'm primarily a thick app developer so this sort of thing is kind of new.

Finaly question, if I use the object on one request and modify it, do I have to reinsert the object back into session? That is, is an object stored in session a reference type?
 
session is specific to a single user for the life of there current session. that probally doesn't make much sense though. with web development each request is autonomous. they don't know about each other. Session is a simple bag to store values between requests. Session starts when the user first visits the website and ends when they either 1. logoff, 2. inactive for a period of time. 20 minutes by default

for your scenario
I would create an interface for the objects you want to return. then create a Session specific implementation.
Code:
interface Something
{
   int I {get;set;}
   string S {get;set;}
   DateTime D {get;set;}
   SomeComplexObject O {get;set;}
}

class SessionSomething : Something
{
   private HttpContext context;
   public SessionSomething(HttpContext context)
   {
      this.context = context;
   }
   public int I 
   {
      get {return (int)Context.Session["i"]; } 
      set {Context.Session["i"] = value; }
   }
   public string S
   {
      get {return (string)Context.Session["s"]; } 
      set {Context.Session["s"] = value; }
   }
   public DateTime D
   {
      get {return (DateTime)Context.Session["d"]; } 
      set {Context.Session["d"] = value; }
   }
   public SomeComplexObject O
   {
      get {return (SomeComplexObject)Context.Session["o"]; } 
      set {Context.Session["o"] = value; }
   }


}
usage
Code:
Something something = new SessionSomething(HttpContext.Current);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top