Storing Objects in Session Object

M

MrShovel

I'm new to this ASP.NET caper and have the following questions.

I have a TestObject that contains about 50 fields of data and 3 member
procedures. Below is a simplified explanation of what I do.

At the start of each session I initialise this TestObject.

On entering every page I create a local TestObject and do this:
TestObject = Session("TestObject")

On leaving every page I do this:
Session("TestObject") = TestObject

This way my TestObject is kept across all the pages until my user quits
or the session times out. My site will probably have up to 100
concurrent sessions running.

Question 1:
Is there a better way to do what I've just described?
-------------------------------------------------
Question 2:
Which is the better way? (Efficient)
[assuming Test=Session("TestObject")]

Test.Field1.value = "1234"
or this?
Session("TestObject").Field1.value = "1234"

Is it quicker to manipulate a local object then put it into the Session
Object when finished?
-------------------------------------------------
Question 3:
Assuming the TestObject member procedures looks something like this:

Procudure SomeProc()
me.Field1.value = "1234"
'heaps more line like above...
'heaps more line like above...
End Sub
-------------------------------------------------
Would it be better to have this?

Procudure SomeProc()
ExternalProc(Me)
End Sub

And in an another module (local on the site)

Procudure ExternalProc(ByRef Test as TestObject)
test.Field1.value = "1234"
'heaps more line like above...
'heaps more line like above...
End Sub

I'm assuming that when you transfer a sessionobject into you code it
also has to transfer all the code defined in the Object Member
Procedures, so it those procedures only have a call to an externaly
referneced module (passing a pointer to the ojbect) then there is
hardly any code in the object itself?

Am I right in this?

Thanks....
 
K

Karl Seguin

#1:
There are different ways, but urs is good. I hate pining things in memory.
I'd prefer to store the data in the database and cache it, which gives you
the speed benefit of in memory (like a session) and the smaller footprint of
the database. It's hard to say though, it depends on how exactly it's
beeing used, you should profile

#2
It doesn't matter too much, but creating a local variable is probably better
(ie, your first example). Otherwise you need to do a hash lookup every
time.

#3
is where you are most-wrong. your alternative doesn't make any sense. Keep
the code in the procedure. Extra lines of code aren't stored in memory for
every instance. Just because the code of your object is big, doesn't
translate into large objects. It's all about hte data you store in them.

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!



MrShovel said:
I'm new to this ASP.NET caper and have the following questions.

I have a TestObject that contains about 50 fields of data and 3 member
procedures. Below is a simplified explanation of what I do.

At the start of each session I initialise this TestObject.

On entering every page I create a local TestObject and do this:
TestObject = Session("TestObject")

On leaving every page I do this:
Session("TestObject") = TestObject

This way my TestObject is kept across all the pages until my user quits
or the session times out. My site will probably have up to 100
concurrent sessions running.

Question 1:
Is there a better way to do what I've just described?
-------------------------------------------------
Question 2:
Which is the better way? (Efficient)
[assuming Test=Session("TestObject")]

Test.Field1.value = "1234"
or this?
Session("TestObject").Field1.value = "1234"

Is it quicker to manipulate a local object then put it into the Session
Object when finished?
-------------------------------------------------
Question 3:
Assuming the TestObject member procedures looks something like this:

Procudure SomeProc()
me.Field1.value = "1234"
'heaps more line like above...
'heaps more line like above...
End Sub
-------------------------------------------------
Would it be better to have this?

Procudure SomeProc()
ExternalProc(Me)
End Sub

And in an another module (local on the site)

Procudure ExternalProc(ByRef Test as TestObject)
test.Field1.value = "1234"
'heaps more line like above...
'heaps more line like above...
End Sub

I'm assuming that when you transfer a sessionobject into you code it
also has to transfer all the code defined in the Object Member
Procedures, so it those procedures only have a call to an externaly
referneced module (passing a pointer to the ojbect) then there is
hardly any code in the object itself?

Am I right in this?

Thanks....
 
J

john_teague

When you say
TestObject = Session("TestObject")

TestObject is a reference to Session("TestObject") any change to
TestObject will be reflected in the session variable. If there are
places where you need to make changes that don't effect the session
variable implement ICloneable interface.

If this is used on every page, you might consider making a BasePage
with a protected property to access the session variable. I prefer to
encapsulate persisted variable in a property, that way I know that
initialization will be handled regardless of what event/method I am in
on the page.

C#
protected TestObject test{
get{
TestObject o = (TestObject)Session["TestObject"];
if(o == null){
//whatever I need to do to create the object for the first time
Session.Add("TestObject", o);
}
return o;
set{
...
}
}

This could be on a base page class and all pages that inherit from it
will have access. Also, if you choose to change your persistence
medium (cache, straight from db) you only have to change it in one
place.
 

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

No members online now.

Forum statistics

Threads
473,871
Messages
2,569,919
Members
46,171
Latest member
A.N.Omalum

Latest Threads

Top