objects in session

M

mark_drewersback

I've been developing in .net for approx 6 months now and would like an
opinion on whether it is ok to use session to store objects?

I normally create a property such as below in code behind on every page
that will use the object.

Public Property Environment() As bsEnvironment
Get
Return CType(Session("Environment"), bsEnvironment)
End Get
Set(ByVal Value As bsEnvironment)
Session("Environment") = Value
End Set
End Property

To populate properties I would use:

Dim environment As New bsEnvironment()
environment.Background = "White"
environment.Foreground = "Black"
Session("environment") = environment

On my other pages I would just access the object using
Environment.Background etc..

Is this the correct way to have objects available to many pages? Back
in the asp days I remember that using session to store objects was
advised against. Are things different now with .net?

Opinions valued.

Regards
 
M

Marina Levit [MVP]

So you are not using the Set of your property definition? You are putting it
directly into session? Why bother with the property then?

Also, it is a lot of work to rewrite these properties on every page. I would
expect these properties to be defined once in an object someplace (like
shared properties), and then you call them to get things in and out of
session.

There is nothing wrong with putting things in session. There are always
considerations, such as, making sure you deal properly with the situation
where the session has timed out, you don't put too many things in session
(as you can run out of memory if you are putting tons of large objects),
etc.But in general there is nothing wrong with using session.
 
C

Cowboy \(Gregory A. Beamer\)

I will concur with Marina. In traditional ASP, session was overused and it
was not wise. Not as big a deal with ASP.NET. If the objects are large or
there are lots of them, you will most likely kill scalability compared to
grabbing data as needed, but there is nothing inherently wrong with storing
in session.

I am not sure, however, that I would store object properties in session.
Store an entire property bag object in session? Sure. But, store all
properties on an object in Session? That is blurring the lines and could be
a train wreck if you continue down that path.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
 
M

mark_drewersback

Thanks for the reply.
So you are not using the Set of your property definition? You are putting it
directly into session? Why bother with the property then?

Because it was the way I was shown to do it. :(

So rather than using the property on every page I should use something
like:

Dim env As bsEnvironment = Session("Environment")

Makes sense. Therefore I can do away with the property...Is this what
you meant?
 
M

Marina Levit [MVP]

That isn't what I said at all.

I said if you are going to bother writing a property, then always go through
the property. If you are going to use Session directly, then don't bother
with the property. But there is no point in writing a property, and then not
using it.

I also suggested that you do not copy the same property into every page, but
rather, you write it once in a central place, and have everything use it.
 

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

Top