Tuesday, June 3, 2008

Perfect ASP.NET application, Part 4

clsStandardPage and clsBrowser

Note: You must see previous post to understand what i am talking about here.

Look at your ASP.NET application. It's full of references to Session object. It's everywhere. In one spot you have Session["FirstName"] = 'George' in another you have Response.Write((string)Session["FirstName"]. What if you make a misspell. And write .....Session["FirstNane"] (notice nane not name). Who is going to know... only when you populate your order database with empty names you will see it. And hopefully QA department will see it.

Plus every time you write Session["..."] it's a look up in hashtable.

So here is an idea. Let's make a class

public class clsBrowser
{
    public string _sFirstName = '';
}

and put it in out clsStandardPage. Then since every our page is derived from clsStandardPage it will have access to _objBs. So if we need to get user's first name we simply write _objBs._sFirstName.

Benefits:

  1. Faster, no hashtable look-ups.
  2. Type safe. We do not need to do conversion to string or Int32 every time we use it.
  3. Misspell proof. Compiler will throw an error if you try to write _sFirsNane.

Continue >>

No comments: