468,549 Members | 2,199 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,549 developers. It's quick & easy.

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 2206
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Francois Malgreve | last post: by
4 posts views Thread by Makarand Keer | last post: by
7 posts views Thread by Keith Patrick | last post: by
reply views Thread by Aaron Morton | last post: by
2 posts views Thread by Dave | last post: by
2 posts views Thread by =?Utf-8?B?YWxiZXJ0b3Nvcmlh?= | last post: by
3 posts views Thread by Madhur | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.