当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  扩展SqlDataSource 2

 扩展SqlDataSource 2

点击次数:29 次 发布日期:2008-11-26 11:47:17 作者:源代码网
源代码网推荐      然后再根据GetView()方法的定义,可以找到一个和SqlDataSource相关紧密的类SqlDataSourceView类,原来Selecting事件是在SqlDataSourceView的ExcuteSelect方法中触发出来的。
源代码网推荐   protected override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
源代码网推荐   ...{
源代码网推荐   //通过SelectParameter构造DBCommand
源代码网推荐   DbCommand command1 = this._owner.CreateCommand(this.SelectCommand, connection1);
源代码网推荐   this.InitializeParameters(command1, this.SelectParameters, null);
源代码网推荐   command1.CommandType = SqlDataSourceView.GetCommandType(this.SelectCommandType);
源代码网推荐  
源代码网推荐   //触发Selecting事件
源代码网推荐   SqlDataSourceSelectingEventArgs args2 = new SqlDataSourceSelectingEventArgs(command1, arguments);
源代码网推荐  
源代码网推荐   this.OnSelecting(args2);
源代码网推荐   if (args2.Cancel)
源代码网推荐   ...{
源代码网推荐   return null;
源代码网推荐   }
源代码网推荐   //执行查询
源代码网推荐   }
源代码网推荐  
源代码网推荐  
源代码网推荐  所以,更改SelectCommand的正确位置是在SqlDataSourceView执行ExcuteSelect方法之前。
源代码网推荐  因此,需要一个类来继承SqlDataSourceView
源代码网推荐  public class SecuritySqlDataSourceView : System.Web.UI.WebControls.SqlDataSourceView
源代码网推荐  
源代码网推荐  然后重写ExcuteSelect方法:
源代码网推荐   Override Method#region Override Method
源代码网推荐   protected override System.Collections.IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
源代码网推荐   ...{
源代码网推荐   //替换查询动作安全上下文
源代码网推荐   ProcessSelectCommand();
源代码网推荐   return base.ExecuteSelect(arguments);
源代码网推荐   }
源代码网推荐   #endregion
源代码网推荐  
源代码网推荐   Virtual Method#region Virtual Method
源代码网推荐   /**//// <summary>
源代码网推荐   /// 如果用户没有此权限动作的授权,则系统会抛出异常
源代码网推荐   /// </summary>
源代码网推荐   protected virtual void ProcessSelectCommand()
源代码网推荐   ...{
源代码网推荐   // 如果设置了查询数据的动作权限名称
源代码网推荐   if (!string.IsNullOrEmpty(this.owner.SelectRefAction))
源代码网推荐   ...{
源代码网推荐   // 获得权限上下文
源代码网推荐   PlatSecurityContext security = (PlatSecurityContext)PlatSecurityContext.Current;
源代码网推荐   // 获得该权限动作对应的规则
源代码网推荐   string rule = security.GetRuleByRefAction(this.owner.SelectRefAction);
源代码网推荐   this.SelectCommand = this.SelectCommand
源代码网推荐   .Replace(string.Format("{{{0}}}",this.owner.SelectRefAction), rule);
源代码网推荐   }
源代码网推荐   }
源代码网推荐   #endregion
源代码网推荐  
源代码网推荐  其中获得权限上下文以及获得该权限动作对应的规则由其他类完成,执行结果返回当前用户执行当前动作,比如ListOrder动作,需要的规则。然后替换SelectCommand中的关于权限的占位符。
源代码网推荐  
源代码网推荐  那么, SecuritySqlDataSource类和SecuritySqlDataSourceView类如何关联起来呢?
源代码网推荐  通过反编译SqlDataSource的代码,找到最佳的位置是在CreateDataSourceView方法中。在这里创建新的SecuritySqlDataSourceView实例,并被SecuritySqlDataSource作为局部变量保存起来,然后通过GetView方法获得。
源代码网推荐   protected override System.Web.UI.WebControls.SqlDataSourceView CreateDataSourceView(string viewName)
源代码网推荐   ...{
源代码网推荐   return new SecuritySqlDataSourceView((System.Web.UI.WebControls.SqlDataSource)this, viewName, HttpContext.Current);
源代码网推荐   }
源代码网推荐  
源代码网推荐  到现在为止,一个支持安全上下文的SqlDataSource就完成了。
源代码网推荐  
源代码网推荐  在页面上使用该控件时,只需要指定前述属性,避免了在每个页面逻辑中求解安全上下文并拼凑Sql的麻烦。使代码更简洁。
源代码网推荐  
源代码网推荐  实现支持复杂检索的SqlDataSource
源代码网推荐  通常,业务逻辑并不只是根据当前用户的身份检索出所有有权限查看的数据,因为数据量太大,不方便查看,所以通常都会缩小检索的范围,也就是对检索条件加以限制,使结果更精确。
源代码网推荐  例如,在Pubs数据库中检索作者信息
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华