HttpContext.Current - how expensive?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SpaceMarine

    #1

    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.Cur rent;

    ....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
  • John Saunders

    #2
    Re: HttpContext.Cur rent - how expensive?

    "SpaceMarin e" <spacemarine@ma ilinator.comwro te in message
    news:b625162c-0445-4094-96a6-e7b088897ce0@q9 g2000hsb.google groups.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.Cur rent;
    The term "instantiat e" 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.Cur rent directly.

    OTOH, I might pass in the Cache object as IDictionary. Create an instance of
    your BAL class and pass HttpContext.Cur rent.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

    Comment

    • George

      #3
      Re: HttpContext.Cur rent - how expensive?

      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.


      "SpaceMarin e" <spacemarine@ma ilinator.comwro te in message
      news:b625162c-0445-4094-96a6-e7b088897ce0@q9 g2000hsb.google groups.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.Cur rent;
      >
      ...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

      Comment

      • bruce barker

        #4
        Re: HttpContext.Cur rent - how expensive?

        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.Cur rent;
        >
        ...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

        Comment

        • SpaceMarine

          #5
          Re: HttpContext.Cur rent - how expensive?

          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.Cur rent 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

          Comment

          • SpaceMarine

            #6
            Re: HttpContext.Cur rent - how expensive?

            On Oct 1, 8:46 pm, "John Saunders" <n...@dont.do.t hat.comwrote:
              HttpContext context = HttpContext.Cur rent;
            >
            The term "instantiat e" 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.Cur rent 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.Cur rent.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.Cur rent.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

            Comment

            • John Saunders

              #7
              Re: HttpContext.Cur rent - how expensive?

              "SpaceMarin e" <spacemarine@ma ilinator.comwro te in message
              news:945d3eda-3013-437a-ac55-3cdfd065a156@64 g2000hsm.google groups.com...
              On Oct 1, 8:46 pm, "John Saunders" <n...@dont.do.t hat.comwrote:
              HttpContext context = HttpContext.Cur rent;
              >>
              >The term "instantiat e" 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.Cur rent 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.Cur rent.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.Cur rent.Cache several times?
              Yes, since you'd have the Cache object already available, and wouldn't have
              to first fetch HttpContext.Cur rent 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

              Comment

              • SpaceMarine

                #8
                Re: HttpContext.Cur rent - how expensive?

                On Oct 2, 11:58 am, "John Saunders" <n...@dont.do.t hat.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

                Comment

                • SpaceMarine

                  #9
                  Re: HttpContext.Cur rent - how expensive?

                  On Oct 2, 11:58 am, "John Saunders" <n...@dont.do.t hat.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.C urrent.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

                  Comment

                  Working...