Click here to Skip to main content
15,881,803 members
Articles / Web Development / ASP.NET
Article

ViewState management using AOP - PostSharp4ViewState

Rate me:
Please Sign up or sign in to vote.
4.40/5 (4 votes)
20 Sep 2008Ms-PL2 min read 27.2K   17   4
How to automate ViewState and control state management using AOP techniques.

Introduction

PostSharp4ViewState is a plug-in for the PostSharp[^] AOP framework by Gael Fraiteur. Its goal is to simplify state management in ASP.NET pages and controls using a declarative programming concept.

How do I use it?

Whenever you want to store some value between postbacks, you decorate it with an attribute - namely PersistAttribute. You can store both property and field values this way. The attribute lets you decide whether a value should be stored in the ViewState or in the control state. The difference is that storing ViewState can be disabled to decrease page size. Storing control state can not be disabled externally, but it should be explicitly enabled by the control itself.

How does it work?

The PostSharp4ViewState main component is an advice provider which searches for types with fields/properties decorated with the Persist attribute. For each such type, a weaving process is initiated as follows:

  • If there is one or more fields/properties to be stored in the ViewState, LoadViewState and SaveViewState are created or modified.
  • If there is one or more fields/properties to be stored in the control state, LoadControlState and SaveControlState are created or modified. Also, the OnInit method is created if it does not exist, and a call to Page.RegisterRequiresControlState(this) is added.

Methods for handling storing in ViewState and control state are, in fact, identical, differing only by name. Their generation is conducted using the following rules:

  • If the Save*State method exists, new code is added after the existing one. It stores the values of the decorated fields/properties in an object[] array and puts it together with the result of the existing code in another object[] array.
  • If the Save*State method doesn't exist, it is generated as if it existed and contained only a call to base.Save*State.
  • If the Load*State method exists, new code is added before the existing one. It casts the passed value to an object[] array, and uses the second element (cast as another object[]) to initiate the decorated fields/properties. The first value is passed to the existing code.
  • If the Load*State method doesn't exist, it is generated as if it existed and contained only a call to base.Load*State.

For example, a class like this:

C#
public class Properties : BaseControl
{
   private int _intValue;
  
   [Persist(Mode = PersistMode.ControlState)]
   public int IntProp
   {
      get { return _intValue; }
      set { _intValue = value; }
   }
}

will be transformed, after compilation, into:

C#
public class Properties : BaseControl
{    
   private int _intValue;
   
   protected override void LoadControlState(object A_1)
   {
      object[] ~tmp~0 = (object[]) A_1;
      object[] ~tmp2~1 = (object[]) ~tmp~0[1];
      if (~tmp2~1[0] != null)
      {
         this.IntProp = (int) ~tmp2~1[0];
      }
      A_1 = ~tmp~0[0];
      base.LoadControlState(A_1);
   }

   protected override void OnInit(EventArgs A_1)
   {
      this.Page.RegisterRequiresControlState(this);
      base.OnInit(A_1);
   }

   protected override object SaveControlState()
   {
      object ~tmpOldValue~1 = base.SaveControlState();
      object[] ~tmp~0 = new object[] { this.IntProp };
      return new object[] { ~tmpOldValue~1, ~tmp~0 };
   }
   
   [Persist(Mode=PersistMode.ControlState)]
   public int IntProp
   {
      get
      {
         return this._intValue;
      }
      set
      {
         this._intValue = value;
      }
   }
}

How about performance?

PostSharp4ViewState is as fast as your hand-written code! It does not use Laos, but pure low-level PostSharp, so no additional objects are being instantiated because of using aspects. The only effect is pure code to save and load persistent values. You can use Reflector[^] to disassemble the DLL, and make sure there are no excess code in your classes.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) VSoft
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNice idea, but... Pin
leppie25-Sep-08 1:04
leppie25-Sep-08 1:04 
GeneralRe: Nice idea, but... Pin
Szymon Pobiega19-Oct-08 19:58
Szymon Pobiega19-Oct-08 19:58 
I written a solution with base class some time ago. It worked, but it had one downside - it used reflection so it was slow. To avoid reflection I possibly could generate dynamic methods but, for me, using Post# is more simple and elegant.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.