|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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 ? Hi,
david@nospam.com wrote: Show quoteHide quote > I have a class with some business-logic and with every roundtrip, I need Generally speaking, the web paradigm is to be stateless. The server > 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 ? 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/CommentView.aspx?guid=5d482643-a44e-4d67-944b-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 Show quoteHide quote > 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) <david@nospam.com> wrote in message Show quoteHide quote news:fu3df2df30e1g446od2spphtijj3l8676o@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 ?
Show quote
Hide quote
> I have a class with some business-logic and with every roundtrip, I need It's not a problem to store "objects" (as opposed to value-types like > 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 ? 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" <news.2.hansdk@spamgourmet.com> wrote in message How do people feel about Cache vs Application for storage of "singleton" news:mn.fa6e7d6884ecc4f2.43821@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. type stuff...?
Show quote
Hide quote
> I have a class with some business-logic and with every roundtrip, I need Everybody thanks for your responses.> 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 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. ... <david@nospam.com> wrote in message Show quoteHide quote news:fu3df2df30e1g446od2spphtijj3l8676o@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:
Show quoteHide quote > 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. > > .. > > > > <david@nospam.com> wrote in message > news:fu3df2df30e1g446od2spphtijj3l8676o@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 bya***@rediffmail.com wrote: Show quoteHide quote > 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. > > > > .. > > > > > > > > <david@nospam.com> wrote in message > > news:fu3df2df30e1g446od2spphtijj3l8676o@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 ?
Other interesting topics
parameter from JScript to Asp.net
Running and debugging a website on http://www.mysite.com instead of http://localhost Configuring the Menu Control CSS Woes formatting problem with dynamic gridview BulletedList inside BulletedList Let and Set not supported under aspx Better timeout mechanism? ASP.NET Web-Based Bulk Email System - HELP Unable to call COM dll's from application in ASP .NET 2.0 on IIS 6.0 |
|||||||||||||||||||||||