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 ? 9 7131
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
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 ?
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 ?
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
"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...?
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.
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 ?
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 ?
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 ?
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...
| |