473,320 Members | 1,841 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,320 software developers and data experts.

Best practice : storing objects in session-variables ?

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 be 'better' to store
an instance of this class in a session-variable, so it's available all the time
and needs to be instanced only once.

Is this, generally speaking, a good idea, storing objects in session-variables ?
Do you guys ever use this 'technique' ?

Or, in other words, what's the 'best practice' when one wants to
use objects (and/or want to do OO-programming) in ASP.Net ?

Who can give me some insights ?
Aug 31 '06 #1
9 7264
Hi,

da***@nospam.com wrote:
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 be 'better' to store
an instance of this class in a session-variable, so it's available all the time
and needs to be instanced only once.

Is this, generally speaking, a good idea, storing objects in session-variables ?
Do you guys ever use this 'technique' ?

Or, in other words, what's the 'best practice' when one wants to
use objects (and/or want to do OO-programming) in ASP.Net ?

Who can give me some insights ?
Generally speaking, the web paradigm is to be stateless. The server
should, in theory, be able to evaluate the current state of the system
on each request.

However, this is hardly possible for web applications often for
performance reasons (to avoid creating classes on every roundtrip, to
avoid fetching data from a DB too often, to avoid having to send
information back and forth...).

IMHO, your role as developer is to evaluate each scenario and to decide
if it makes sense to store things in the Session (or using other
mechanisms, for example static classes, the Application object, files,
etc...) to persist state, or if a more "webby" approach is possible.

Note however that stateless is, in my experience, easier to implement.
As soon as you have a state in your application, then the test cases get
much more complicated.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Aug 31 '06 #2
I find that you want to use Session as infrequently as possible because
it can take up a lot of memory on a higher traffic website. Instead
try to make use of System.Web.Cache.

When you do this a business object, you may have criteria you can use
to identify this business object. Say it is a Product which has a
unique ID. You can access with the following code which will save you
from hitting the database for every request.

Besides, placing it in the session only makes it available to that
single user while the cache is available to everyone so it will help a
high traffic website.

public Product FindProduct(string productId)
{
Cache cache = HttpRuntime.Cache;

string cacheKey = "Product-" + productId;
Product product = cache[cacheKey] as Product;

if (product == null)
{
... code to load product from domain ...
... add the product to the cache using cacheKey ...
}

return product;
}

With the default cache settings you should see some nice performance
improvments. And also keep in mind that you can use System.Web.Caching
outside of an ASP.NET runtime.

http://www.hanselman.com/blog/Commen...b-f08c75183cc1

Here are a few related articles on the topic which I found useful.

http://msdn2.microsoft.com/en-us/library/ms178597.aspx
http://msdn2.microsoft.com/en-us/library/ms178604.aspx
http://msdn2.microsoft.com/en-us/library/ms164606.aspx

CacheItemPriority
http://msdn2.microsoft.com/en-us/lib...mpriority.aspx

CacheItemRemovedCallback
http://msdn2.microsoft.com/en-us/lib...dcallback.aspx

--
Brennan Stehling
http://brennan.offwhite.net/blog/

da***@nospam.com wrote:
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 be 'better' to store
an instance of this class in a session-variable, so it's available all the time
and needs to be instanced only once.

Is this, generally speaking, a good idea, storing objects in session-variables ?
Do you guys ever use this 'technique' ?

Or, in other words, what's the 'best practice' when one wants to
use objects (and/or want to do OO-programming) in ASP.Net ?

Who can give me some insights ?
Aug 31 '06 #3
Whether it is or is not good practice to instanciate the object each time
often depends on what the performance impact might be, and whether holding
state of an object may cause other issues. DB connections for example
should always try to connect, extract/update data and disconnect, so if your
class is holding connections open it then storing it in the session is not a
good idea as you could in theory run out of available connections and
pooling is less efficient.

Also, on a heavy traffic site, adding memory hungry objects to session can
grind your server to its knees. So, you will need to test how your
application performs with objects in session, and without them - and make a
design decision.

Regards

John Timney (MVP)
<da***@nospam.comwrote in message
news:fu********************************@4ax.com...
>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 be 'better' to store
an instance of this class in a session-variable, so it's available all the
time
and needs to be instanced only once.

Is this, generally speaking, a good idea, storing objects in
session-variables ?
Do you guys ever use this 'technique' ?

Or, in other words, what's the 'best practice' when one wants to
use objects (and/or want to do OO-programming) in ASP.Net ?

Who can give me some insights ?

Aug 31 '06 #4
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 be 'better' to store
an instance of this class in a session-variable, so it's available all the
time and needs to be instanced only once.

Is this, generally speaking, a good idea, storing objects in
session-variables ? Do you guys ever use this 'technique' ?

Or, in other words, what's the 'best practice' when one wants to
use objects (and/or want to do OO-programming) in ASP.Net ?

Who can give me some insights ?
It's not a problem to store "objects" (as opposed to value-types like
int) into Session. You could even say it's better to store objects than
value-types, because those value-types need "boxing" and "unboxing".

If the object is specific to a user, then you can use Session. If it's
the same for all users then consider Cache: then you can use a single
instance everywhere, instead of multiple identical copies.

Hans Kesting
Aug 31 '06 #5
"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:mn***********************@spamgourmet.com...
If the object is specific to a user, then you can use Session. If it's the
same for all users then consider Cache: then you can use a single instance
everywhere, instead of multiple identical copies.
How do people feel about Cache vs Application for storage of "singleton"
type stuff...?
Aug 31 '06 #6
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 be 'better' to store
an instance of this class in a session-variable, so it's available all the time
and needs to be instanced only once.

Is this, generally speaking, a good idea, storing objects in session-variables ?
Do you guys ever use this 'technique' ?

Or, in other words, what's the 'best practice' when one wants to
use objects (and/or want to do OO-programming) in ASP.Net ?

Who can give me some insights ?

Everybody thanks for your responses.
Aug 31 '06 #7

I have a more formalized "web object holder" ... that I use.
Its basically a session object via a singleton.

See
http://sholliday.spaces.live.com/ 10/24/2005

Use the cache with care. I do it alot, but I don't put huge objects in
there.

But when I have a webpage, where a user gets a list of data... and then they
want to resort it ...
I put the backend (strong) DataSet into this object cacher thing.

With my object cacher, remember to always use .Remove(key) if you only need
it once. Indexer will leave it in the cache.

...

<da***@nospam.comwrote in message
news:fu********************************@4ax.com...
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 be 'better' to store
an instance of this class in a session-variable, so it's available all the
time
and needs to be instanced only once.

Is this, generally speaking, a good idea, storing objects in
session-variables ?
Do you guys ever use this 'technique' ?

Or, in other words, what's the 'best practice' when one wants to
use objects (and/or want to do OO-programming) in ASP.Net ?

Who can give me some insights ?

Sep 1 '06 #8

sloan wrote:
I have a more formalized "web object holder" ... that I use.
Its basically a session object via a singleton.

See
http://sholliday.spaces.live.com/ 10/24/2005

Use the cache with care. I do it alot, but I don't put huge objects in
there.

But when I have a webpage, where a user gets a list of data... and then they
want to resort it ...
I put the backend (strong) DataSet into this object cacher thing.

With my object cacher, remember to always use .Remove(key) if you only need
it once. Indexer will leave it in the cache.

..

<da***@nospam.comwrote in message
news:fu********************************@4ax.com...
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 be 'better' to store
an instance of this class in a session-variable, so it's available all the
time
and needs to be instanced only once.

Is this, generally speaking, a good idea, storing objects in
session-variables ?
Do you guys ever use this 'technique' ?

Or, in other words, what's the 'best practice' when one wants to
use objects (and/or want to do OO-programming) in ASP.Net ?

Who can give me some insights ?
Sep 11 '06 #9
Hi,
One can go through an article on "Boxing & Unboxing in .NET" at
http://aspalliance.com/986_Boxing_and_Unboxing_in_NET
by****@rediffmail.com wrote:
sloan wrote:
I have a more formalized "web object holder" ... that I use.
Its basically a session object via a singleton.

See
http://sholliday.spaces.live.com/ 10/24/2005

Use the cache with care. I do it alot, but I don't put huge objects in
there.

But when I have a webpage, where a user gets a list of data... and then they
want to resort it ...
I put the backend (strong) DataSet into this object cacher thing.

With my object cacher, remember to always use .Remove(key) if you only need
it once. Indexer will leave it in the cache.

..

<da***@nospam.comwrote in message
news:fu********************************@4ax.com...
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 be 'better' to store
an instance of this class in a session-variable, so it's available all the
time
and needs to be instanced only once.
>
Is this, generally speaking, a good idea, storing objects in
session-variables ?
Do you guys ever use this 'technique' ?
>
Or, in other words, what's the 'best practice' when one wants to
use objects (and/or want to do OO-programming) in ASP.Net ?
>
Who can give me some insights ?
Sep 11 '06 #10

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

Similar topics

14
by: Howard | last post by:
Hi, I recently had a problem where I decided to store objects in a vector. (Previously, I had always stored pointers in vectors). Well, naturally, when storing an object in a vector, using...
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: Sjaakie Helderhorst | last post by:
Hi, What's the best way of storing a dataset (+/-1000 rows, 9 columns)? I'm using a session-variable but this seems to have serious impact on performance. It's a shop and I'm using the same...
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...
6
by: Sandy | last post by:
Hello - I have a book that illustrates pulling menu items from a Sql Server table into an ascx via a stored procedure. Is this something that is done in the real world? I do like the effect...
17
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access 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...
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...
3
by: =?Utf-8?B?SlA=?= | last post by:
I have a web application that will handle anywhere for 50 to 175 users at one time but will likely have a user base in the 100s. This application contains a GUI interface for the every day user and...
13
by: DigitalDave | last post by:
A project I did awhile back stored php5 objects in elements of the $_SESSION array between pages that were navigated on the site. There were object classes representing teachers, and object classes...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.