文章分类

站点统计

  • 分类总数: 13 个
  • 文章总数: 145 篇
  • 评论总数: 48 条
  • 附件总数: 59 个
  • 建站日期: 2008-08-18
  • 访问总数: 492895 人次
  • RSS订阅: 文章|评论

通过PostSharp4ViewState实现以AOP方式进行ViewState管理

Admin 于 2008-09-21 22:02:07 发表.Net

PostSharp4ViewState是PostSharp的一个插件,用于简化Asp.Net页面与控件的状态管理。如果需要将页面或控件中的 字段或属性值保存在ViewState或ControllState中,以便在页面Postback后仍可取得,只需在相应的字段或属性上添加 PersistAttribute特性的声明。
PostSharp4ViewState的主要组件为一个用于搜寻声明了PersistAttribute的字段和属性的AdviceProvider,对于搜寻到字段和属性以如下方式进行织入:
如果字段或属性被配置为存储于ViewState,则创建或修改LoadViewState与SaveViewState;如果字段或属性被配置为存储于 ControlState,则创建或修改 LoadControlState与SaveControlState,同时如果没有OnInit方法则创建一个同时添加 Page.RegisterRequiresControlState(this)调用。

处理ViewState 与 ControlState 存储的方法除了名称不同外其它的都一样,并按以下进行代码生成:
If Save*State method exists new code is added after existing. It stores values of decorated fields/properties in object[] array and puts it together with resut of existing code in another object[] array
If Save*State method doesn't exist it is generated as if it existed and contained only a call to base.Save*State
If Load*State method exists new code is added before existing. It casts passed value to object[] array and uses second element (casted as another object[]) to initiate decorated fields/properties. First value is passed to existing code
If Load*State method does't exist it is generated as if it existed and contained only a call to base.Load*State
例子:

  1. public class Properties : BaseControl 
  2.    private int _intValue; 
  3.   
  4.    [Persist(Mode = PersistMode.ControlState)] 
  5.    public int IntProp 
  6.    { 
  7.       get { return _intValue; } 
  8.       set { _intValue = value; } 
  9.    } 

相应编译后的内容为:

  1. public class Properties : BaseControl 
  2. {    
  3.    private int _intValue; 
  4.    
  5.    protected override void LoadControlState(object A_1) 
  6.    { 
  7.       object[] ~tmp~0 = (object[]) A_1; 
  8.       object[] ~tmp2~1 = (object[]) ~tmp~0[1]; 
  9.       if (~tmp2~1[0] != null
  10.       { 
  11.          this.IntProp = (int) ~tmp2~1[0]; 
  12.       } 
  13.       A_1 = ~tmp~0[0]; 
  14.       base.LoadControlState(A_1); 
  15.    } 
  16.  
  17.    protected override void OnInit(EventArgs A_1) 
  18.    { 
  19.       this.Page.RegisterRequiresControlState(this); 
  20.       base.OnInit(A_1); 
  21.    } 
  22.  
  23.    protected override object SaveControlState() 
  24.    { 
  25.       object ~tmpOldValue~1 = base.SaveControlState(); 
  26.       object[] ~tmp~0 = new object[] { this.IntProp }; 
  27.       return new object[] { ~tmpOldValue~1, ~tmp~0 }; 
  28.    } 
  29.    
  30.    [Persist(Mode=PersistMode.ControlState)] 
  31.    public int IntProp 
  32.    { 
  33.       get 
  34.       { 
  35.          return this._intValue; 
  36.       } 
  37.       set 
  38.       { 
  39.          this._intValue = value; 
  40.       } 
  41.    } 

由于是编译期代码生成,只是在编译结果中插入用于存储属性值的代码,所以PostSharp4ViewState代码性能与手写代码性能一样。可以通过Reflector反编译结果程序集来进行检查。

被阅814次, 0票PostSharp ViewState 发表评论
1 / 1 / 1 | « 1 » |
Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 开源吧!. All rights reserved.

粤ICP备07500939号