SIGN UP MEMBER LOGIN:    
ARTICLE

Read Asp.net Request Param and Session dynamically into properties

Posted by Jigar Desai Articles | Visual C# October 04, 2007
This Article shows how you can use Attributes and Reflection to dynamically populate property with value from param, session or context without writing any addition code. I have used this technique in past and I hope you will also find it useful.
Reader Level:

Introduction:

 

This article shows how you can use Attributes and Reflection to dynamically populate property with value from param, session or context without writing any addition code. I have used this technique in past and I hope you will also find it useful.

 

Why do I need this?

 

I am a lazy programmer, I won't write a code that I do not have to, I also believe that amount of mistakes that I make while writing code is directly proportional to number of lines of the code that I write.

Why would I write following code?

 

public int id;

public string userName;

 

void Page_Init(object sender, EventArgs e)

{

    if (!string.IsNullOrEmpty(Request.QueryString["id"]))

    {

        id = int.Parse(Request.QueryString["id"]);

    }

 

    if (Request.Cookies["userName"] != null && Request.Cookies["userName"].Value != null)

    {

        userName = Request.Cookies["userName"].Value;

    }

}

 

If I can just write following.

[PageProperty]

public string id;

[PageProperty(ReadFrom = DictionaryType.Cookie)]

public string userName;

 

Lets define what are we interested in reading:

 

Most common place where we need to read data is query string dictionary but we will also read from additional places like Form, Session, Cookie, Headers and Dictionary that comes with Context. We will create flag that will define places that we can read data from.

 

[flags]

public enum DictionaryType

{

    QueryString = 1,

    Form = 2,

    Session = 4,

    Context = 8,

    Cookie = 16,

    Header = 32,

}

 

Next step will be to create Attribute class for our properties and fields:

  1. Attributes provide a powerful method of associating declarative information with code. We need two properties in our attribute.
  2. Key which we need to pass to read data from dictionary, If user do not provide this key then we will assume that name of property/field is key.

Enumeration that defines dictionary to read data from which will default to query string. 

 

public class PageProperty : Attribute

{

    private DictionaryType readFrom = DictionaryType.QueryString;

    privatestring key;

 

    publicstring Key

    {

        get

        {

            return key;

        }

        set

        {

            key = value;

        }

    }

    public DictionaryType ReadFrom

    {

        get

        {

            return readFrom;

        }

        set

        {

            readFrom = value;

        }

    }

}

 

Once we have above class we can tag fields in our page with above attribute.

 

[PageProperty(ReadFrom = DictionaryType.Cookie)]

public int userId;

Reading attributes using reflection and populating values:
  1. We will use reflection to inspect page class to find all property or public fields with PageProperty Attribute.
  2. Next step will be to get key and DictionaryType.
  3. And once we know source and target to populate and we will get information from appropriate dictionary and populate it.

We will warp this logic into a small static class with static Read method, which takes reference of page object.

 

public static void Read(object pageObject)

{

    // read for all fields.

    FieldInfo[] fields = (pageObject.GetType()).GetFields();

    foreach (FieldInfo fieldInfo in fields)

    {

        object[] attributes = fieldInfo.GetCustomAttributes(typeof(PageProperty), false);

 

        if (attributes.Length > 0)

        {

            PageProperty pageProperty = attributes[0] as PageProperty;

            string key = pageProperty.Key;

            if (key == null) key = fieldInfo.Name;

 

            object val = getValue(key, pageProperty.ReadFrom);

            if (val != null)

            {

                fieldInfo.SetValue(pageObject, changeType(val, fieldInfo.FieldType));

            }

        }

    }

}

 

Retriving value from Dictionary:

 

Since we are using flags user might specify more than one place to read from, for example user might specify following:

 

[PageProperty(ReadFrom = DictionaryType.QueryString | DictionaryType.Cookie)]

 

To handle this we will go through all selected enumerations and return first with value, so in above mentioned example if query string value was present it will return query string value, otherwise it will return value from cookie.

 

private static object getValue(string key, DictionaryType readFrom)

{

 

    if ((readFrom & DictionaryType.QueryString) == DictionaryType.QueryString &&

    HttpContext.Current.Request.QueryString[key] !=null)

    {

        return HttpContext.Current.Request.QueryString[key];

    }

 

    elseif ((readFrom & DictionaryType.Form) == DictionaryType.Form &&

    HttpContext.Current.Request.Form[key] !=null)

    {

          return HttpContext.Current.Request.Form[key];

    }

}

    
How to use Property Reader in your web page?

  1. Drop Jigar.Web.PagePropertyReader.dll in to bin directory of your web project.
  2. In your Page class add following line in Pre_Init Event.
    PropertyReader.Read(this);
    Alternatively you can also create PageBase class by inheriting from System.Web.UI.Page and override PreInit event.
  3. Finally in your page just create public property or field with PageProperty Attribute and you are done.

Source code includes sample web application, which shows usage of PropertyReader. It also includes test page with different senerios.

 

What about performance cost associated with reflection?

 

Reflection is definitely going to add some performance cost to your application, you will have to analyze value vs. cost analysis for your application and decide best for you.

Instead of reflection you might also want to try using Reflection.Emit to improve performance.

Login to add your contents and source code to this article
share this article :
post comment
 

Sir, i req.u i am new student in asp.net & c# so idont information Dot net so plz. i req u u send me u r experience in developing database driven wed application & ur Articles in asp.net may email add bharat.mendhe@rediffmail.com ur faithfully bharat kumar mnedhe

Posted by bharat kumar mendhe Oct 05, 2007
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor