一直有关注CSLA框架,最近闲来无事,折腾了下,在最新的r3054版本基础上修改了一些东西,以备自己用,有兴趣的园友可以下载共同研究

1、添加了默认的授权规则

如果是列表对象则生成列表权限,User的只读列表和可编辑列表生成的都是User.List权限,admin角色具有所有权限:
  1. public partial class UserInfoList
  2. {
  3. #region Authorization Rules
  4.  
  5. /// <summary>
  6. /// Allows the specification of CSLA based authorization rules for a collection list. Specifies what roles can
  7. /// perform which operations for a given business object
  8. /// </summary>
  9. public static void AddObjectAuthorizationRules()
  10. {
  11. Csla.Rules.BusinessRules.AddRule(typeof(UserInfoList), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.GetObject, "admin","User.List"));
  12. }
  13. #endregion
  14. }
  1. public partial class UserList
  2. {
  3. #region Authorization Rules
  4.  
  5. /// <summary>
  6. /// Allows the specification of CSLA based authorization rules for a collection list. Specifies what roles can
  7. /// perform which operations for a given business object
  8. /// </summary>
  9. public static void AddObjectAuthorizationRules()
  10. {
  11. Csla.Rules.BusinessRules.AddRule(typeof(UserList), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.GetObject, "admin","User.List"));
  12. }
  13. #endregion
  14. }
如果是可编辑的跟对象则生成增删改读权限,admin角色具有所有权限:
  1. public partial class User
  2. {
  3. #region Authorization Rules
  4.  
  5. /// <summary>
  6. /// Allows the specification of CSLA based authorization rules. Specifies what roles can
  7. /// perform which operations for a given business object
  8. /// </summary>
  9. public static void AddObjectAuthorizationRules()
  10. {
  11. Csla.Rules.BusinessRules.AddRule(typeof(User), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.GetObject, "admin","User.Get"));
  12. Csla.Rules.BusinessRules.AddRule(typeof(User), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.CreateObject, "admin","User.Create"));
  13. Csla.Rules.BusinessRules.AddRule(typeof(User), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.EditObject, "admin","User.Edit"));
  14. Csla.Rules.BusinessRules.AddRule(typeof(User), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.DeleteObject, "admin", "User.Delete"));
  15. }
  16. #endregion
  17. }

2、生成权限列表的sql脚本

对应生成的权限脚本:
  1. insert into S_Permision(Name) values('User.List');
  2. insert into S_Permision(Name) values('User.Get');
  3. insert into S_Permision(Name) values('User.Create');
  4. insert into S_Permision(Name) values('User.Edit');
  5. insert into S_Permision(Name) values('User.Delete');

3、添加了查询支持

criteria本来的查询是每个字段都是取相等的值,改造后,cirteria中每个对应的字段都增加了一个字段名+Operator的属性。Operator的值即可根据用户选择 like ,not like ,> , < ,<>

  1. var form = ASPxNavBar1.Groups[].FindControl("ASPxFormLayout1") as ASPxFormLayout;
  2. var criteria = new Business.UserCriteria();
  3. Csla.Data.DataMapper.Map(FormHelper.GetFormData(form), criteria);
  4. return criteria;
  1. public class FormHelper
  2. {
  3. public static Dictionary<string, object> GetFormData(DevExpress.Web.ASPxFormLayout.ASPxFormLayout form)
  4. {
  5. var dict = new Dictionary<string, object>();
  6. foreach (DevExpress.Web.ASPxFormLayout.LayoutItem item in form.Items)
  7. {
  8. if (string.IsNullOrEmpty(item.FieldName)) continue;
  9. if (dict.ContainsKey(item.FieldName))
  10. throw new Exception("布局中存在重复的字段");
  11. var value = form.GetNestedControlValueByFieldName(item.FieldName);
  12. if (value != null)
  13. dict.Add(item.FieldName, value);
  14. }
  15. return dict;
  16. }
  17. }

4、添加了批量删除

没有加到模板中,直接复制即可使用,但是用到了criteria中新加的属性

  1. [Serializable]
  2. public class MultyDeleteCommand<T, C> : CommandBase<MultyDeleteCommand<T, C>>
  3. where T : BusinessBase<T>
  4. where C : IGeneratedCriteria, new()
  5. {
  6. #region Authorization Methods
  7.  
  8. public static bool CanExecuteCommand()
  9. {
  10. return Csla.Rules.BusinessRules.HasPermission(Csla.Rules.AuthorizationActions.DeleteObject,
  11. typeof(T));
  12. }
  13.  
  14. #endregion
  15.  
  16. #region Factory Methods
  17.  
  18. public static bool Execute(IEnumerable<object> pkList)
  19. {
  20. if (!CanExecuteCommand())
  21. throw new System.Security.SecurityException("没有权限执行删除操作");
  22.  
  23. MultyDeleteCommand<T, C> cmd = new MultyDeleteCommand<T, C>();
  24. cmd.PKList = pkList;
  25. cmd.BeforeServer();
  26. cmd = DataPortal.Execute<MultyDeleteCommand<T, C>>(cmd);
  27. cmd.AfterServer();
  28. return cmd.Result;
  29. }
  30.  
  31. private MultyDeleteCommand()
  32. { /* require use of factory methods */ }
  33.  
  34. #endregion
  35.  
  36. #region Client-side Code
  37.  
  38. public static readonly PropertyInfo<bool> ResultProperty = RegisterProperty<bool>(p => p.Result);
  39. public bool Result
  40. {
  41. get { return ReadProperty(ResultProperty); }
  42. set { LoadProperty(ResultProperty, value); }
  43. }
  44.  
  45. public IEnumerable<object> PKList { get; set; }
  46.  
  47. private void BeforeServer()
  48. {
  49. // TODO: implement code to run on client
  50. // before server is called
  51. }
  52.  
  53. private void AfterServer()
  54. {
  55. // TODO: implement code to run on client
  56. // after server is called
  57. }
  58.  
  59. #endregion
  60.  
  61. #region Server-side Code
  62.  
  63. protected override void DataPortal_Execute()
  64. {
  65. string temp = ""; string key = "";
  66. var criteria = new C();
  67. if (string.IsNullOrWhiteSpace(criteria.TableFullName) || string.IsNullOrWhiteSpace(criteria.PKName))
  68. throw new Exception("表名和主键名不能为空");
  69.  
  70. SqlParameter[] parm = new SqlParameter[PKList.Count()]; //初始化参数个数
  71. for (int i = ; i < PKList.Count(); i++)
  72. {
  73. key = "@StringId" + i.ToString();
  74. temp += key + ","; //将每个参数连接起来
  75. parm[i] = new SqlParameter(key, PKList.ElementAt(i));
  76. }
  77. temp = (temp + ")").Replace(",)", ""); //去掉最后一个逗号
  78.  
  79. string commandText = string.Format("DELETE {0} WHERE [{1}] IN ({2})",criteria.TableFullName, criteria.PKName, temp);
  80. if (!string.IsNullOrEmpty(criteria.SoftDeletedName))
  81. commandText = string.Format("UPDATE {0} SET [{1}]=1 WHERE [{2}] IN ({3})", criteria.TableFullName, criteria.SoftDeletedName, criteria.PKName, temp);
  82.  
  83. using (var connection = new SqlConnection(ADOHelper.ConnectionString))
  84. {
  85. connection.Open();
  86. using (var command = new SqlCommand(commandText, connection))
  87. {
  88. command.Parameters.AddRange(parm);
  89.  
  90. //result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed.
  91. int result = command.ExecuteNonQuery();
  92. if (result == )
  93. throw new DBConcurrencyException("您提交的数据已过期,请刷新您的界面后重试.");
  94. else
  95. Result = true;
  96. }
  97. }
  98.  
  99. }
  100.  
  101. #endregion
  102. }

5、添加了软删除的功能

当数据表中有一列名为bit类型的DeleteFlag(默认值为0,not null)时,自动启用软删除,即执行对象的删除操作时并不是从数据库中删除对象,而是设置DeleteFlag字段为1,使用criteria条件查询时,除非指定显式设置DeletedFlag属性值,否则默认按照DeleteFlag<>1查询,即已经删除的记录不会查出来,就像真的被从数据库中删除了一样。

如果要修改数据库中的列名,修改模板Common\AutoColunmConfig.cst中的DeletedFlag属性即可

6、添加了添加修改时间,创建者修改者的自动处理

  数据库表中存在CreateAt,UpdateAt,CreateUserID,UpdateUserID时自动生成代码,在Common\AutoColunmConfig.cst模板中可以配置对应的数据库字段和读取当前用户ID的代码。UserIDAccessor配置生成当前用户ID的代码。
 <%@ Property Name="DeletedFlag" Default="DeletedFlag" Type="System.String" %>
<%@ Property Name="Create_At" Default="CreateAt" Type="System.String" %>
<%@ Property Name="Update_At" Default="UpdateAt" Type="System.String" %>
<%@ Property Name="Create_UserID" Default="CreateUserID" Type="System.String" %>
<%@ Property Name="Update_UserID" Default="UpdateUserID" Type="System.String" %>
<%@ Property Name="UserIDAccessor" Default="Csla.ApplicationContext.User == null ? (int?)null : (Csla.ApplicationContext.User.Identity as MFKIdentity).UserID" Type="System.String" %>
 

7、添加了禁止删除系统预设字段的业务规则(不在模板中,复制代码在需要的地方调用即可)

  1. public class DenyDeleteSystemDefinedObject<T> : Csla.Rules.CommonRules.IsInRole
  2. where T : Csla.BusinessBase<T>
  3. {
  4. Func<T, bool> isSystemDefined;
  5. public DenyDeleteSystemDefinedObject(Func<T, bool> _isSystemDefined, params string[] roles)
  6. : base(Csla.Rules.AuthorizationActions.DeleteObject, roles)
  7. {
  8. isSystemDefined = _isSystemDefined;
  9. }
  10.  
  11. protected override void Execute(Csla.Rules.AuthorizationContext context)
  12. {
  13. base.Execute(context);
  14. if (!context.HasPermission) return;
  15.  
  16. if (context.Target != null && context.Target is T && isSystemDefined(context.Target as T))
  17. {
  18. context.HasPermission = false;
  19. throw new System.InvalidOperationException(Properties.Resources.SystemDefinedObjectCannotDelete);
  20. }
  21. }
  22. }

  使用方式,例如:有一个系统设置的根对象Setting:

  1. public partial class Setting: BusinessBase<Setting>
  2. {
  3. static Dictionary<int, string> _systemDefinedList = null;
  4. /// <summary>
  5. /// 系统预设值
  6. /// </summary>
  7. public static Dictionary<int, string> SystemDefined
  8. {
  9. get
  10. {
  11. if (_systemDefinedList == null)
  12. {
  13. _systemDefinedList = new Dictionary<int, string>();
  14. _systemDefinedList.Add(, "站点名称");
  15. _systemDefinedList.Add(, "LOGO图片");
  16. }
  17. return _systemDefinedList;
  18. }
  19. }
  20.  
  21. public bool IsSystemDefined
  22. {
  23. get
  24. {
  25. return Setting.SystemDefined.ContainsValue(KeyName) || Setting.SystemDefined.ContainsKey(Identification);
  26. }
  27. }
  28.  
  29.     #region Authorization Rules
  30.  
  31. /// <summary>
  32. /// Allows the specification of CSLA based authorization rules. Specifies what roles can
  33. /// perform which operations for a given business object
  34. /// </summary>
  35. public static void AddObjectAuthorizationRules()
  36. {
  37. Csla.Rules.BusinessRules.AddRule(typeof(Setting), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.GetObject, "admin", "Setting.Get"));
  38. Csla.Rules.BusinessRules.AddRule(typeof(Setting), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.CreateObject, "admin", "Setting.Create"));
  39. Csla.Rules.BusinessRules.AddRule(typeof(Setting), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.EditObject, "admin", "Setting.Edit"));
  40. Csla.Rules.BusinessRules.AddRule(typeof(Setting), new DenyDeleteSystemDefinedObject<Setting>(
  41. s => s.IsSystemDefined, "admin", "Setting.Delete"));
  42. }
  43. #endregion
  44.  
  45. partial void OnDeleting(SettingCriteria criteria, ref bool cancel)
  46. {
  47. if (Setting.SystemDefined.ContainsValue(criteria.KeyName)
  48. || Setting.SystemDefined.ContainsKey(criteria.Identification))
  49. {
  50. throw new System.InvalidOperationException(Properties.Resources.SystemDefinedObjectCannotDelete);
  51. }
  52. }
  53. }

  

注意事项:

1、表名不能以Info结尾
2、字段名不可以和Csla的业务对象的同名,例如: 字段名不能为Parent,IsDeleted 等

附:可能由于CSLA对使用者的要求相对其他框架较高,目前国内CSLA的学习社区太少,这么优秀的框架,居然国内都不火,真是可惜了,我建了个交流群,欢迎感兴趣的朋友一起来学习。

CSLA&DevExpress交流群: 367088648

 
 
 

CSLA框架的codesmith模板改造的更多相关文章

  1. DotNet 资源大全中文版,内容包括:编译器、压缩、应用框架、应用模板、加密、数据库、反编译、IDE、日志、风格指南等

    DotNet 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-dotnet 是由 quozd 发起和维护.内容包括:编译器. ...

  2. csla框架__使用Factory方式实现Csla.BusinessBase对象数据处理

    环境:.net4.6+csla4.6 实现:对象的数据库访问及数据库执行使用Factory方式进行封闭. 正文: 以前在使用csla框架完成业务对象的定义时所有的数据处理都在对象内部实现,也不能说不好 ...

  3. CodeSmith模板代码生成实战详解

    前言 公司项目是基于soa面向服务的架构思想开发的,项目分解众多子项目是必然的.然而子项目的架子结构种类也过多的话,就会对后期的开发维护产生一锅粥的感觉.为了尽可能的在结构层避免出现这种混乱的现象,我 ...

  4. NFine框架的T4模板

    1.前言 前段时间在网上看到一个开源框架很好的.开源:ASP.NET MVC+EF6+Bootstrap开发框架,写代码就是比较比较麻烦,分层比较多,对于我这种偷懒的人就想到了写一个T4模板.不了解框 ...

  5. Ci框架整合smarty模板引擎

    Ci框架整合smarty模板引擎 备注:下载smarty时,最好选择2.6版本,其他测试有坑,ci可以是2.2或其他 大体思路:将smarty封装成ci框架的一个类,然后重新配置一下smarty,这样 ...

  6. [King.yue]关于CSLA框架的一些看法

    CSLA.Net 是一个有帮助的成熟开发框架,但不适于初学者.该框架支持在任何地方.任何时间创建对象,值得我们花时间去学习了解这一框架.CSLA.Net 框架设计的业务对象,支持对完全透明的数据源进行 ...

  7. Django框架简介及模板Template,filter

    Django框架简介 MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View) ...

  8. Spring框架的JDBC模板技术和事物

    Spring框架的JDBC模板技术         技术分析之Spring框架的JDBC模板技术概述  1. Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单     ...

  9. 解析Spring第四天(Spring中的事物、Spring框架来管理模板类)

    JDBC模板技术: Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单 template 模板 都是Spring框架提供XxxTemplate 提供了JDBC模板,Sp ...

随机推荐

  1. 四大MQ比较及MQ详解

    消息队列已经逐渐成为企业IT系统内部通信的核心手段.它具有低耦合.可靠投递.广播.流量控制.最终一致性等一系列功能,成为异步RPC的主要手段之 一.当今市面上有很多主流的消息中间件,如老牌的Activ ...

  2. SSH框架整合截图总结(一)

    分页相关属性 --------------------------------------------------------------- 分页思路表单提交(只需传递当前页的值) ->acti ...

  3. Oracle解除表锁定问题

    1.肯定是你同时打开了多个操作页面,要记得关闭多个打开的sql窗口. 2.可以变相删除表,再重新创建一张同名的表来解除表被锁住的问题

  4. SSAO + FXAA

    如今已经完毕了渲染器的屏幕空间环境光遮挡(SSAO)算法和FXAA高速反走样算法,等有时间就把当中的相关原理和当中遇到的问题进行总结发表.

  5. Cocos2d-x 动手实现游戏主循环

    因为Cocos2d-x封装的非常好,所以对于非常多新手,他们仅仅知道先new一个场景,在场景上加入布景或精灵,然后用Director的runWithScene便能够执行游戏了.假设给一个精灵加个动作, ...

  6. 赵雅智_android获取本机运营商,手机号部分能获取

    手机号码不是全部的都能获取.仅仅是有一部分能够拿到. 这个是因为移动运营商没有把手机号码的数据写入到sim卡中.SIM卡仅仅有唯一的编号.供网络与设备 识别那就是IMSI号码,手机的信号也能够说是通过 ...

  7. iOS-UIImageView载入网络下载的图片(异步+多线程)

    最原始的载入网络下载的图片方式: //最原始载入网络图片方法,相当堵塞主线程,界面卡顿 -(void)setImageWithURL:(NSString *)imageDownloadUrl{ UII ...

  8. DOM基础----DOM(一)

    DOM(Document Object Model),中文名称为文档对象模型.是处理可扩展标识语言的标准编程接口,主要针对HTML和XML.DOM描绘了一个层次化的节点树,开发者能够加入.改动和移除页 ...

  9. Bitcoin学习篇之---PPS和PPLNS挖矿模式介绍

    PPS和PPLNS挖矿模式介绍 比特币每10分钟产生一个区块,会有千万人竞争.而这个区块终于仅仅归1个人全部.其他人都颗粒无收. 你或许要挖5年才干获得一个区块. 组队挖矿就是.一旦队伍里不论什么人获 ...

  10. FFMpeg在Windows下搭建开发环境【转】

    本文转载自:http://blog.csdn.net/wootengxjj/article/details/51758621 版权声明:本文为博主原创文章,未经博主允许不得转载. FFmpeg 是一个 ...