HttpContext.Current - how expensive?

S

SpaceMarine

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
 
J

John Saunders

SpaceMarine said:
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.
 
G

George

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

bruce barker

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)
 
S

SpaceMarine

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
 
S

SpaceMarine

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

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
 
J

John Saunders

SpaceMarine said:
...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.
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.
 
S

SpaceMarine

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
 
S

SpaceMarine

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top