Skip to main content
Email Password   helpLost your password?

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:

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:

For example, a class like this:

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:

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralWhere is the source ? Pin
emperon
0:27 3 Oct '08  
GeneralRe: Where is the source ? Pin
Szymon Pobiega
20:58 19 Oct '08  
GeneralNice idea, but... Pin
leppie
2:04 25 Sep '08  
GeneralRe: Nice idea, but... Pin
Szymon Pobiega
20:58 19 Oct '08  


Last Updated 20 Sep 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009