Accessing a Persistent Object throughout a user session

G

Guest

I want to create an object which is attached to the specific user session and
I want to be able to access that object directly throughout the Pages, Page
Controls, and Master Pages of the site.

I'm assuming that the best way to do that is to create the object and then
put it into the Session? If so, a few questions about the best way to do that:

1. If I want to reliably access that object on any and every page, how do I
make sure that the object is already in the Session for that user? Do I have
to put logic EVERYWHERE that it's used that says, 'if(Session["MyObj"]==null)
Session["MyObj"] = new MyObj()'?

2. When I access the object from the Session, do I have to 'cast' the object
before I can use it on the pages (e.g. '(MyObj)Session["MyObj"]')?

3. Is there some better way to make the object available throughout the
Session without actually inserting it manually INTO the Session and then
extracting it on every page where it's used?

Alex
 
G

Guest

Alex,
see inline:

Alex Maghen said:
I want to create an object which is attached to the specific user session and
I want to be able to access that object directly throughout the Pages, Page
Controls, and Master Pages of the site.

I'm assuming that the best way to do that is to create the object and then
put it into the Session? If so, a few questions about the best way to do that:

-- Yes, store the object in Session with a descriptive key.
1. If I want to reliably access that object on any and every page, how do I
make sure that the object is already in the Session for that user? Do I have
to put logic EVERYWHERE that it's used that says, 'if(Session["MyObj"]==null)
Session["MyObj"] = new MyObj()'?

-- You could create the session object one time in the Session_Start
global.asax handler. Note that you must actually put something into Session
for a user in order to get Session_Start to fire.

2. When I access the object from the Session, do I have to 'cast' the object
before I can use it on the pages (e.g. '(MyObj)Session["MyObj"]')?

Yes: MyObj obj = (MyObj)Session["MyObj"];
3. Is there some better way to make the object available throughout the
Session without actually inserting it manually INTO the Session and then
extracting it on every page where it's used?
-- Covered above.

Good Luck!
Peter
 
S

Steven Cheng[MSFT]

Hi Alex,

I think Peter's suggestion reasonable. As for SessionState, it is
associated with each client user through a sessionID, by default it is
stored in client user's cookie. And the SessionState colleciton is like a
dictionary collection, it store all the objects or object reference as
object type, so you need to explicitly cast it when retrieve the certain
classs instance from SessionState.

And Peter's suggestion on using Session_Start event to initialize a Session
variable is a good idea. BTW, I still recommend you use the protected code
block such as (if Session[key] == null then Session[key] = new....) when
accessing such object, this is the best practice which can avoid Null
reference exception.

In addition, I'm not sure whether you're using the ASP.NET 2.0 and the
users in your application is authenticated through the forms
authentication. If that's the case, you can also consider using the Profile
service which can provide strong-typed data storage for user specific datas:

#ASP.NET Profile Properties
http://msdn2.microsoft.com/en-us/library/at64shx3(VS.80).aspx

Please feel free to post here if there is anything else we can help.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Alex,

Have you got any progress on this issue or do you still have any further
questions on this? If anything else we can help, please feel free to post
here.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top