Wrap Your NameValue Variables

ASP.NET gives a developer a lot of loosely typed key-value collections in which to stash stash variables, depending upon persistence needs. The short list includes ViewState, Session, Application and HttpContext.Items. These collections can come in very handy when one needs to keep an object around outside of a single request or shuttle things between different bits of the http pipeline. But it comes at a price—these collections are loosely typed, just returning Objects. Moreover, there is no compile-time checking to ensure that you are requesting the right key in the right place. Errors can lead to crashes at best, and interesting data corruption at worst.

But those issues can easily be avoided with a little discipline up front. You see, rather than calling these variables directly, it is very possible to wrap them in an appropriately-scoped property and have that manage access to the underlying data. For the examples we are going to use the Session, but the semantics will remain the same with just about any of the above collections.

So, let’s say we have a little web store. And this web store has a shopping cart. And we are going to stash this Shopping Cart object in the session. Now, one could many instances of code like this:

  ShoppingCart sc=(ShoppingCart)Session[“ShoppingCart”]; If (sc==null) {       sc=new ShoppingCart(); } //do stuff with ShoppingCart. 

Or, one a base page class somewhere, you could define something like:

  protected ShoppingCart Cart {     get     {         if (Session["ShoppingCart"] == null)         {             Session["ShoppingCart"] = new ShoppingCart();         }         return (ShoppingCart)Session["ShoppingCart"];     } } 

The advantages of this approach are that the calling code never even needs to worry about where/how the shopping cart is stored, or if it can be null, or how to cast it out of the underlying store. Furthermore, if, at a future date, you decide that you are better off storing this cart in, say, the viewstate, the change is very, very easy because it only needs to be updated in one place.

Enjoy and don’t be afraid to .


Category: asp.net Time: 2007-02-25 Views: 1

Related post

  • Make sure the 'git' executable is on your PATH variable 2012-11-14

    Hi am working on salesforce touch. I am trying to Install the Mobile SDK for Android but while doing it in the step 3. their is 'Open a command prompt in the directory where you installed the cloned repository, and run the install script from the com

  • Reset your PATH variable 2011-04-11

    I stuffed up (basically wiped) my PATH variable. Is there any way to reset it to the default? I look at How do I reset the $PATH variable on Mac OS X?, but I find it very confusing. As far as I know I stuffed up my ~/.profile file. --------------Solu

  • How do you name your private variables in C#? 2010-09-09

    What is the best practice, most commonly accepted naming conventions for private variables in C#? private int myInteger; private int MyInteger; private int mMyInteger; private int _myInteger; private int _MyInteger; Mysterious other option Which do y

  • How do you add a bash script to your PATH variable using symlinks? 2015-06-14

    I run a gaming server called PocketMine. So basically I have a folder in my home directory that has a bash script to run the server: ~/PocketMine/start.sh Everytime I want to run the server I either cd into the folder and ./start.sh or PocketMine/./s

  • How to add the location of websocketd to your PATH variable? 2016-02-21

    I'm new to Linux environment (Mint), and recently I was trying to create a websocket for my HTML web app. Eventually I came across websocketd. As per installation steps, I'm supposed to add this websocketd to PATH. I know adding variables to PATH in

  • Changing your path variable 2011-04-01

    I finally made the jump and got a MacBook, and I'm trying to move in. I have the OSX port of gedit installed, but I'm having trouble adding it to my PATH. My current PATH looks like this: export PATH=/opt/local/bin:/Applications/gedit.app/Contents/Ma

  • Why aren't my variables being updated? 2011-12-09

    Given following python 2.7 code: def A() : def B() : b = 1 def C() : # I can access 'b' from here. print( b ) # But can i modify 'b' here? 'global' and assignment will not work. C() print( b ) B() A() For the code in C() function variable b is in out

  • Harness the Power of CVS for Your Site 2002-07-31

    Now Where did I Put that File? In your travels on the Internet, you may have come across the acronym CVS, which is used with a kind of fanaticism by software developers who work on Open Source projects. If you've been too shy to ask, CVS stands for C

  • The Best Structure for your Flash Site 2006-04-07

    Adobe's (formerly Macromedia's) Flash application is, according to them, "The industry's most advanced authoring environment for creating interactive web sites and digital experiences." However, many users have trouble structuring their site eff

  • Bash custom prompt breaking wrapping of long command lines 2009-12-13

    I have a custom bash prompt that colours my username, hostname and current working directory. The colours display correctly, however when I type a command line that needs to wrap, the terminal does not start a new line, and writes over the beginning

  • 10 Tips to improve your jQuery 2010-11-30

    There's jQuery almost everywhere, you can find it on famous websites available today including social networking websites, micro blogs, etc. jQuery is indeed cool and very useful, but sometimes it's best to see a different flavor or another side of i

  • Take Variables Set in Functions.php and Echo Them Inside My Custom Post Template 2010-12-06

    I have the following function set up in my functions.php file to allow my custom post type "Slideshow" to work. add_action( 'the_post', 'paginate_slide' ); function paginate_slide( $post ) { global $pages, $multipage, $numpages; if( is_single()

  • c shell environment variable error: "Bad : modifier in $" 2011-01-17

    I am using tcsh and define an environmental variable as follows: setenv mycomp [email protected] so that when I need to copy files from the remote my.computer.com, I type the following: scp $mycomp:sourcepath destpath But when I do this, I g

  • Wrapping a javascript "class"? 2011-12-23

    Below is a simple "class" for displaying messages. It stores the output html div and then displays messages to it as needed. It works fine. For readability, and encapsulation purposes, I'd like the componenets of the "class" in a conti

  • Post variables not displaying correctly in custom function 2012-01-13

    There seems to be something wrong with the simple function below, but I can't figure out what it is. It is displaying as the following and I can't figure out why. I know it has something to do with the functions the_ID(), the_title(), and the_permali

  • Module variable scoping in Scheduled Tasks? 2012-04-01

    I ran across a scoping puzzle while experimenting with ScheduledTasks, and I'd be grateful for an explanation from the sages here. I found a workaround by intuitive horse sense, but was unable to explain to myself adequately what was wrong with my or

  • How to Pass External Variables to Filters/Actions 2012-04-11

    I find myself needing to pass custom data to a filter provided by a 3rd party plugin. All of the ways I've seen to do this are really complicated and hard to wrap my head around. Take this example: $score = 42; //Some crazy calculation I don't want t

  • Setting truncation and wrap in Emacs config file 2012-05-23

    I'm using Emacs 23 in a Debian GNU/Linux variant distribution. In a default Emacs session launched with emacs -q (to confirm the settings) I execute the following to setup the environment I would like: M-x toggle-truncate-lines <RET> M-x set-variabl

  • Mysql comparison operators from variable 2012-07-04

    I am trying to learn the best way to write queries. I also understand the importance of being consistent, and up until now I have essentially randomly used single quotes, double quotes, and backticks without any real thought. Example: $query = 'INSER

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C) avrocks.com, All Rights Reserved.

processed in 1.180 (s). 13 q(s)