473,395 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Storing Objects in Session Object

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....

Nov 19 '05 #1
3 1549
#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" <sh********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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....

Nov 19 '05 #2
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.

Nov 19 '05 #3
Thanks guys... will do further research & tests...

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: mjkahn | last post by:
I've read (and read!) that you shouldn't store objects in Session variables. I've read these reasons: - The object takes up memory that may not be freed until the session times out. Better to...
0
by: Sandra | last post by:
I am using a VB6 COM object with an asp.net project. I am storing the COM object in a session variable but am having a problem accessing the COM object from the session variable. I am getting...
2
by: jakk | last post by:
Below is the exception that Iam getting. It says that the DataView that Iam storing in the session is not Serializable. BUt works fine if I store in the inproc session and fails if I switch to...
2
by: Curt tabor | last post by:
Hi, I have several pages in my app that all use the same oleDBConnection(s). When this connection gets created, I store it as a Session variable so that other pages can access it w/o having...
5
by: James Vickers | last post by:
All, I am a bit of noob to ASP.NET, and I am writing some OO based pages to try and emerge myself into it, however, I have come across a problem that has me completely stumped! What I want to...
10
by: Mark Rae | last post by:
Hi, This relates to the previous thread "Disappearing Sessions", but is a bit more generic so I thought I'd start a new thread. This one relates to the storing of objects in Session once only to...
9
by: david | last post by:
I have a class with some business-logic and with every roundtrip, I need an instance of this class, so I have to create it, every time again. That doesn't seem very efficient. I thought it would...
3
by: RSH | last post by:
Hi, I have a situation where I have created an object that contains fields,properties and functions. After creating the object I attempted to assign it to a session variable so i could retrieve...
4
by: =?Utf-8?B?YmFzdWxhc3o=?= | last post by:
Hi; I want to store a datatable (or an arraylist) as a session variable but when I try; Session = al_RecNo; I get an error that; "Cannot implicitly convert type...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.