473,320 Members | 1,952 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.

HttpContext.Current - how expensive?

hello,

my web app form has many DropDownLists that pull their content from a
database. these calls are in a Business Access Layer, when first
checks the context's Cache object for existing copies. to do that, the
BAL needs to be aware of the user's HttpContext.

question: how expensive is it for each BAL method to instantiate the
current context, like so:

HttpContext context = HttpContext.Current;

....if im doing this a dozen times from a consuming page class, is this
bad?

or, would it be better for me to have the consuming page class *pass
in* its context to the dozen BAL methods? will a see a speed savings,
or is it moot?
thanks!
sm
Oct 1 '08 #1
8 2447
"SpaceMarine" <sp*********@mailinator.comwrote in message
news:b6**********************************@q9g2000h sb.googlegroups.com...
hello,

my web app form has many DropDownLists that pull their content from a
database. these calls are in a Business Access Layer, when first
checks the context's Cache object for existing copies. to do that, the
BAL needs to be aware of the user's HttpContext.

question: how expensive is it for each BAL method to instantiate the
current context, like so:

HttpContext context = HttpContext.Current;
The term "instantiate" means - to create an instance. This does not create
an instance, it simply refers to one. In fact, you don't need to copy the
reference into "context" - you can just use HttpContext.Current directly.

OTOH, I might pass in the Cache object as IDictionary. Create an instance of
your BAL class and pass HttpContext.Current.Cache as a constructor
parameter. Let the BAL class save that off to use later. That will also make
it easier to unit test the BAL layer, since it will not need to be used
inside of a web application.

--
John Saunders | MVP - Connected System Developer

Oct 2 '08 #2
This is equivalent to a Hashtable lookup (might be even faster like memory
lookup) (It's just a guess)
I recommend to write code right and then optimize it.

Actually from my point of view the BAL methods must be able to do the job no
mater which context they were called from. Windows Forms or ASP.NET
So if you BAL object relies on existence of the HttpContext then it's not
really a BAL object.

While I agree that sometimes you can save some development time on making
BAL object aware of the context they are called from I always ended up
redoing my application later and eliminating staff like HttpContext from my
BAL objects.
George.
"SpaceMarine" <sp*********@mailinator.comwrote in message
news:b6**********************************@q9g2000h sb.googlegroups.com...
hello,

my web app form has many DropDownLists that pull their content from a
database. these calls are in a Business Access Layer, when first
checks the context's Cache object for existing copies. to do that, the
BAL needs to be aware of the user's HttpContext.

question: how expensive is it for each BAL method to instantiate the
current context, like so:

HttpContext context = HttpContext.Current;

...if im doing this a dozen times from a consuming page class, is this
bad?

or, would it be better for me to have the consuming page class *pass
in* its context to the dozen BAL methods? will a see a speed savings,
or is it moot?
thanks!
sm
Oct 2 '08 #3
its pretty cheap, its stored in the threads context. but this is pretty
poor design for the bal. its now tightly coupled to the httpcontext, and
can not be used by a non-asp.net application.

a better design would be an adapter that connects to the drop downs to
the bal or a factory that returns the cache handler.

-- bruce (sqlwork.com)

SpaceMarine wrote:
hello,

my web app form has many DropDownLists that pull their content from a
database. these calls are in a Business Access Layer, when first
checks the context's Cache object for existing copies. to do that, the
BAL needs to be aware of the user's HttpContext.

question: how expensive is it for each BAL method to instantiate the
current context, like so:

HttpContext context = HttpContext.Current;

...if im doing this a dozen times from a consuming page class, is this
bad?

or, would it be better for me to have the consuming page class *pass
in* its context to the dozen BAL methods? will a see a speed savings,
or is it moot?
thanks!
sm
Oct 2 '08 #4
thanks for the responses.

our company's platform is entirely ASP.NET, we are not a desktop
applications shop. thus coupling asp.net objects like context is not a
concern. i can see why it would be important to cross-platform
environments, however. (this is similar to the argument against using
SQL Server-specific data objects; but since we are a SQL Server shop,
it is not a concern).

the execution speed is a concern, tho. but it sounds that since
HttpContext.Current is not actually instantiating a new object, merely
referencing something, that its not extremely expensive to use
repeatedly.

the idea of instantiating the BAL class and having it save the Cache
obj for all its methods isnt a bad one. currently these lookups are
static methods, but i may consider changing that if there are
significant performance gains. thoughts?
sm
Oct 2 '08 #5
On Oct 1, 8:46*pm, "John Saunders" <n...@dont.do.that.comwrote:
* HttpContext context = HttpContext.Current;

The term "instantiate" means - to create an instance. This does not create
an instance, it simply refers to one. In fact, you don't need to copy the
reference into "context" - you can just use HttpContext.Current directly.
....even if im using it 3-4 times? for most anything if i use it more
than twice i create a local object for it. is there no savings w/
this? if not, why?
OTOH, I might pass in the Cache object as IDictionary. Create an instanceof
your BAL class and pass HttpContext.Current.Cache as a constructor
parameter. Let the BAL class save that off to use later.
using this tecnique, then the BAL would only be digging up the Cache
object once per page class (or wherever one is instantiating the BAL
obj. for me, this would be on each page; it would then be called 6-12
times for the dropdownlist lookups). and there would be performance
gains w/ this, over using the HttpContext.Current.Cache several times?

is that much better than just passing the Cache obj in w/ each static
lookup method? (again, we dont mind coupling our BAL to ASP.NET;
desktop or otherwise is not an option).
sm
Oct 2 '08 #6
"SpaceMarine" <sp*********@mailinator.comwrote in message
news:94**********************************@64g2000h sm.googlegroups.com...
On Oct 1, 8:46 pm, "John Saunders" <n...@dont.do.that.comwrote:
HttpContext context = HttpContext.Current;

The term "instantiate" means - to create an instance. This does not
create
an instance, it simply refers to one. In fact, you don't need to copy the
reference into "context" - you can just use HttpContext.Current directly.

...even if im using it 3-4 times? for most anything if i use it more
than twice i create a local object for it. is there no savings w/
this? if not, why?
A local object might be faster, but are you even able to measure the
performance improvement? The way I do performance enhancement is to always
wait for a performance _problem_ first, then measure the performance so I
can tell if I'm making it any better or worse.
>OTOH, I might pass in the Cache object as IDictionary. Create an instance
of
your BAL class and pass HttpContext.Current.Cache as a constructor
parameter. Let the BAL class save that off to use later.

using this tecnique, then the BAL would only be digging up the Cache
object once per page class (or wherever one is instantiating the BAL
obj. for me, this would be on each page; it would then be called 6-12
times for the dropdownlist lookups). and there would be performance
gains w/ this, over using the HttpContext.Current.Cache several times?
Yes, since you'd have the Cache object already available, and wouldn't have
to first fetch HttpContext.Current and then get the Cache property.
is that much better than just passing the Cache obj in w/ each static
lookup method? (again, we dont mind coupling our BAL to ASP.NET;
desktop or otherwise is not an option).
It's a toss-up. I personally prefer not to see duplication of code, even
duplication of parameters passed to methods. I always find that if you're
passing the same parameters into a set of methods, then that suggests you
need to create a class that takes those constant parameters as constructor
arguments, then make those static methods into instance methods of the new
class.

--
John Saunders | MVP - Connected System Developer

Oct 2 '08 #7
On Oct 2, 11:58*am, "John Saunders" <n...@dont.do.that.comwrote:
is that much better than just passing the Cache obj in w/ each static
lookup method? (again, we dont mind coupling our BAL to ASP.NET;
desktop or otherwise is not an option).

It's a toss-up. I personally prefer not to see duplication of code, even
duplication of parameters passed to methods. I always find that if you're
passing the same parameters into a set of methods, then that suggests you
need to create a class that takes those constant parameters as constructor
arguments, then make those static methods into instance methods of the new
class.
those are good points. thank you.
sm
Oct 2 '08 #8
On Oct 2, 11:58*am, "John Saunders" <n...@dont.do.that.comwrote:
...even if im using it 3-4 times? for most anything if i use it more
than twice i create a local object for it. is there no savings w/
this? if not, why?

A local object might be faster, but are you even able to measure the
performance improvement? The way I do performance enhancement is to always
wait for a performance _problem_ first, then measure the performance so I
can tell if I'm making it any better or worse.
on the topic of using local variables instead of drilling-down thru an
object hierarchy ("HttpContext.Current.Cache", etc..) -- its been my
long-standing practice to always create an object for anything used
more than twice. back in the legacy ASP days, and in javascript, it
was considered expensive to keep referring to a deep collection.
wasted CPU time spent re-digging for something that you already
identified once. thus, the best practice was to not re-dig each and
every time.

if you do this everywhere, for everything, i believe you will be
conserving system resources.

as for testing the performance gain of this, the only way i know is to
use Trace.Writes (or custom stopwatching) and evaluate the execution
time. however, in singular cases this diff may likely be minute. but
applied to your entire application....i believe it adds up. thus my
practice.
sm
Oct 3 '08 #9

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

Similar topics

0
by: Nayt Grochowski | last post by:
Does anyone see any problem with the loading a SqlConnection into the System.Web.HttpContextCurrent.Items collection in a Page's Constructor. Then Closing and Disposing of it the OnUnload method? ...
2
by: Francois Malgreve | last post by:
hello guys, I have some helper class in my ASP.NET pplication who basically contains static methods. I used them as helper methods to do small jobs that I can use at many places in my code. ...
2
by: moondaddy | last post by:
I need to set a variable to a session variable (if that's what you call it) like this: dim ds as dataset = HttpContext.Current.Session("CustDataSet") But I get an exception if this variable...
4
by: Makarand Keer | last post by:
Hi All I have problem in using Threading. I have ASP.NET application in which I am using multithreading to start a process. Now the object instances which are used in this thread access...
7
by: Keith Patrick | last post by:
After completely giving up on finding some way in my ASP.Net app to take a query string URL and redirect to it as a POST instead, I went with a system like so: Upon "redirection," all the...
0
by: Aaron Morton | last post by:
I'm working on a IHttpModule that handles the PreSendRequestHeaders event from the HttpApplication, if the event is raised after EndRequest then HttpContext.Current is null. If it is raised before...
2
by: Dave | last post by:
After some digging, I discovered HttpContext.Current.Session is null when trying to access a session variable, username, in my upload.cs code which is in the App_Code folder. I just determined...
2
by: =?Utf-8?B?YWxiZXJ0b3Nvcmlh?= | last post by:
Hi, I'm using Threads, and when I try to do Server.Transfer, I recieved an error. (child object does not exist...) My Code: Dim t As New Thread(AddressOf Hilo) Private Sub Hilo()...
3
by: Madhur | last post by:
Hello I am delivering an asp.net 2.0 application to my customer. I need to know, If I need to check for the condition of HttpContext.Current to be null in my business logic. I have extensively...
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: 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.