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

环境:.net4.6+csla4.6

实现:对象的数据库访问及数据库执行使用Factory方式进行封闭。

正文:

以前在使用csla框架完成业务对象的定义时所有的数据处理都在对象内部实现,也不能说不好,对象很大。作者给了通过Factory的方式将对象的数据访问层进行分离,代码更容易维护。以下是我通过Factory的方式实现对象数据访问。

首先是通过csla的约定定义业务对象,其中包含了属性验证:

  1     [Serializable]
  2     [Csla.Server.ObjectFactory("HFunM.Business.Service.CustomDevelop.AssetManage.CategoriesService, HFunM.Business.Service", "CreateCategory", "FetchCategory", "UpdateCategory", "DeleteCategory")]
  3     public class Asset_CategoryER : Csla.BusinessBase<Asset_CategoryER>
  4     {
  5         #region Contructor
  6
  7         public Asset_CategoryER()
  8         { /* Require use of factory methods */ }
  9
 10         #endregion
 11
 12         #region Properties
 13
 14         public static readonly PropertyInfo<System.String> _actIdProperty
 15             = RegisterProperty<System.String>(p => p.ActId, "主键ID");
 16         /// <summary>
 17         /// 主键ID
 18         /// </summary>
 19         [System.ComponentModel.DataObjectField(true, false)]
 20         public System.String ActId
 21         {
 22             get { return GetProperty(_actIdProperty); }
 23             set { SetProperty(_actIdProperty, value); }
 24         }
 25
 26         public static readonly PropertyInfo<System.String> _aCTParentIDProperty
 27             = RegisterProperty<System.String>(p => p.ACTParentID, "上级分类ID", (System.String)null);
 28         /// <summary>
 29         /// 上级分类ID
 30         /// </summary>
 31         public System.String ACTParentID
 32         {
 33             get { return GetProperty(_aCTParentIDProperty); }
 34             set { SetProperty(_aCTParentIDProperty, value); }
 35         }
 36
 37         public static readonly PropertyInfo<System.String> _aCTSysCodeProperty
 38             = RegisterProperty<System.String>(p => p.ACTSysCode, "系统分类编号", (System.String)null);
 39         /// <summary>
 40         /// 系统分类编号
 41         /// </summary>
 42         public System.String ACTSysCode
 43         {
 44             get { return GetProperty(_aCTSysCodeProperty); }
 45             set { SetProperty(_aCTSysCodeProperty, value); }
 46         }
 47
 48         public static readonly PropertyInfo<System.String> _aCTNameProperty
 49             = RegisterProperty<System.String>(p => p.ACTName, "分类名称", (System.String)null);
 50         /// <summary>
 51         /// 分类名称
 52         /// </summary>
 53         public System.String ACTName
 54         {
 55             get { return GetProperty(_aCTNameProperty); }
 56             set { SetProperty(_aCTNameProperty, value); }
 57         }
 58
 59         public static readonly PropertyInfo<System.String> _aCTShortNameProperty
 60             = RegisterProperty<System.String>(p => p.ACTShortName, "名称缩写", (System.String)null);
 61         /// <summary>
 62         /// 名称缩写
 63         /// </summary>
 64         public System.String ACTShortName
 65         {
 66             get { return GetProperty(_aCTShortNameProperty); }
 67             set { SetProperty(_aCTShortNameProperty, value); }
 68         }
 69
 70         public static readonly PropertyInfo<System.String> _aCTStateProperty
 71             = RegisterProperty<System.String>(p => p.ACTState, "分类状态", (System.String)null);
 72         /// <summary>
 73         /// 分类状态
 74         /// </summary>
 75         public System.String ACTState
 76         {
 77             get { return GetProperty(_aCTStateProperty); }
 78             set { SetProperty(_aCTStateProperty, value); }
 79         }
 80
 81         public static readonly PropertyInfo<System.Int32?> _aCTServiceLifeProperty
 82             = RegisterProperty<System.Int32?>(p => p.ACTServiceLife, "使用年限", (System.Int32?)null);
 83         /// <summary>
 84         /// 使用年限
 85         /// </summary>
 86         public System.Int32? ACTServiceLife
 87         {
 88             get { return GetProperty(_aCTServiceLifeProperty); }
 89             set { SetProperty(_aCTServiceLifeProperty, value); }
 90         }
 91
 92         public static readonly PropertyInfo<System.Double?> _aCTSalvageRateProperty
 93             = RegisterProperty<System.Double?>(p => p.ACTSalvageRate, "残值率(%)", (System.Double?)null);
 94         /// <summary>
 95         /// 残值率(%)
 96         /// </summary>
 97         public System.Double? ACTSalvageRate
 98         {
 99             get { return GetProperty(_aCTSalvageRateProperty); }
100             set { SetProperty(_aCTSalvageRateProperty, value); }
101         }
102
103         public static readonly PropertyInfo<System.String> _aCTUnitProperty
104             = RegisterProperty<System.String>(p => p.ACTUnit, "计算单位", (System.String)null);
105         /// <summary>
106         /// 计算单位
107         /// </summary>
108         public System.String ACTUnit
109         {
110             get { return GetProperty(_aCTUnitProperty); }
111             set { SetProperty(_aCTUnitProperty, value); }
112         }
113
114         public static readonly PropertyInfo<System.String> _aCTRemarkProperty
115             = RegisterProperty<System.String>(p => p.ACTRemark, "备注", (System.String)null);
116         /// <summary>
117         /// 备注
118         /// </summary>
119         public System.String ACTRemark
120         {
121             get { return GetProperty(_aCTRemarkProperty); }
122             set { SetProperty(_aCTRemarkProperty, value); }
123         }
124
125         public static readonly PropertyInfo<System.String> _aCTVoucherProperty
126             = RegisterProperty<System.String>(p => p.ACTVoucher, "会计凭证号", (System.String)null);
127         /// <summary>
128         /// 会计凭证号
129         /// </summary>
130         public System.String ACTVoucher
131         {
132             get { return GetProperty(_aCTVoucherProperty); }
133             set { SetProperty(_aCTVoucherProperty, value); }
134         }
135
136         #endregion
137
138         #region Business Rules
139
140         /// <summary>
141         /// Contains the CodeSmith generated validation rules.
142         /// </summary>
143         protected override void AddBusinessRules()
144         {
145             // Call the base class, if this call isn‘t made than any declared System.ComponentModel.DataAnnotations rules will not work.
146             base.AddBusinessRules();
147
148             BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(_actIdProperty));
149             BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(_aCTNameProperty));
150             BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(_aCTParentIDProperty));
151             BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(_aCTSysCodeProperty));
152             BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_actIdProperty, 36));
153             BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTParentIDProperty, 36));
154             BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTSysCodeProperty, 50));
155             BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTNameProperty, 50));
156             BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTShortNameProperty, 50));
157             BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTStateProperty, 20));
158             BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTUnitProperty, 20));
159             BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTRemarkProperty, 200));
160             BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTVoucherProperty, 50));
161         }
162
163         #endregion
164
165         #region Factory
166
167         public static Asset_CategoryER NewCategory(string parentID)
168         {
169             return DataPortal.Create<Asset_CategoryER>(parentID);
170         }
171
172         public static Asset_CategoryER GetCategory(string id)
173         {
174             return DataPortal.Fetch<Asset_CategoryER>(id);
175         }
176
177         public static void DeleteCategory(string id)
178         {
179             DataPortal.Delete<Asset_CategoryER>(id);
180         }
181
182         #endregion
183     }

可能已经发现了属性Csla.Server.ObjectFactoryAttribute,它就是定义Factory的,其中提供了多种构造方式,如下:

 1 namespace Csla.Server
 2 {
 3     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = false)]
 4     public class ObjectFactoryAttribute : Attribute
 5     {
 6         public ObjectFactoryAttribute(string factoryType);
 7         public ObjectFactoryAttribute(Type factoryType);
 8         public ObjectFactoryAttribute(string factoryType, string fetchMethod);
 9         public ObjectFactoryAttribute(Type factoryType, string fetchMethod);
10         public ObjectFactoryAttribute(string factoryType, string createMethod, string fetchMethod);
11         public ObjectFactoryAttribute(Type factoryType, string createMethod, string fetchMethod);
12         public ObjectFactoryAttribute(string factoryType, string createMethod, string fetchMethod, string updateMethod, string deleteMethod);
13         public ObjectFactoryAttribute(Type factoryType, string createMethod, string fetchMethod, string updateMethod, string deleteMethod);
14         public ObjectFactoryAttribute(string factoryType, string createMethod, string fetchMethod, string updateMethod, string deleteMethod, string executeMethod);
15         public ObjectFactoryAttribute(Type factoryType, string createMethod, string fetchMethod, string updateMethod, string deleteMethod, string executeMethod);
16
17         public string FactoryTypeName { get; }
18         public string CreateMethodName { get; }
19         public string FetchMethodName { get; }
20         public string UpdateMethodName { get; }
21         public string DeleteMethodName { get; }
22         public string ExecuteMethodName { get; }
23     }
24 }

以下是Factory的数据访问实现:

  1     public class CategoriesService : Data.Repository.RepositoryFactory<Entity.CustomDevelop.AssetEntity.Asset_CategoryEntity>
  2     {
  3         #region 列表
  4
  5         /// <summary>
  6         /// 加载所有数据
  7         /// </summary>
  8         /// <returns></returns>
  9         public Asset_CategoryList FetchList()
 10         {
 11             var obj = (Asset_CategoryList)MethodCaller.CreateInstance(typeof(Asset_CategoryList));
 12             var rlce = obj.RaiseListChangedEvents;
 13             obj.RaiseListChangedEvents = false;
 14             base.SetIsReadOnly(obj, false);
 15
 16             var child = base.BaseRepository().IQueryable(p => p.ACT_State == "1");
 17             foreach (var item in child)
 18             {
 19                 obj.Add(CreateItem(item));
 20             }
 21
 22             base.SetIsReadOnly(obj, true);
 23             obj.RaiseListChangedEvents = rlce;
 24             return obj;
 25         }
 26
 27         /// <summary>
 28         /// 创建列表子对象
 29         /// </summary>
 30         /// <returns></returns>
 31         public Asset_CategoryInfo CreateItem()
 32         {
 33             var obj = (Asset_CategoryInfo)MethodCaller.CreateInstance(typeof(Asset_CategoryInfo));
 34             LoadProperty(obj, Asset_CategoryInfo._actIdProperty, Util.CommonHelper.GetGuid);
 35             MarkNew(obj);
 36             CheckRules(obj);
 37             return obj;
 38         }
 39
 40         /// <summary>
 41         ///
 42         /// </summary>
 43         /// <param name="et"></param>
 44         /// <returns></returns>
 45         public Asset_CategoryInfo CreateItem(Entity.CustomDevelop.AssetEntity.Asset_CategoryEntity et)
 46         {
 47             var obj = (Asset_CategoryInfo)MethodCaller.CreateInstance(typeof(Asset_CategoryInfo));
 48             MarkAsChild(obj);
 49
 50             LoadProperty(obj, Asset_CategoryInfo._actIdProperty, et.ACT_ID);
 51             LoadProperty(obj, Asset_CategoryInfo._aCTNameProperty, et.ACT_Name);
 52             LoadProperty(obj, Asset_CategoryInfo._aCTParentIDProperty, et.ACT_ParentID);
 53             LoadProperty(obj, Asset_CategoryInfo._aCTRemarkProperty, et.ACT_Remark);
 54             LoadProperty(obj, Asset_CategoryInfo._aCTSalvageRateProperty, et.ACT_SalvageRate);
 55             LoadProperty(obj, Asset_CategoryInfo._aCTServiceLifeProperty, et.ACT_ServiceLife);
 56             LoadProperty(obj, Asset_CategoryInfo._aCTShortNameProperty, et.ACT_ShortName);
 57             LoadProperty(obj, Asset_CategoryInfo._aCTStateProperty, et.ACT_State);
 58             LoadProperty(obj, Asset_CategoryInfo._aCTSysCodeProperty, et.ACT_SysCode);
 59             LoadProperty(obj, Asset_CategoryInfo._aCTUnitProperty, et.ACT_Unit);
 60             LoadProperty(obj, Asset_CategoryInfo._aCTVoucherProperty, et.ACT_Voucher);
 61
 62             MarkOld(obj);
 63             return obj;
 64         }
 65
 66         #endregion
 67
 68         #region 主对象
 69
 70         /// <summary>
 71         /// 创建类别
 72         /// </summary>
 73         /// <returns></returns>
 74         public Asset_CategoryER CreateCategory(string parentID)
 75         {
 76             var obj = (Asset_CategoryER)MethodCaller.CreateInstance(typeof(Asset_CategoryER));
 77             LoadProperty(obj, Asset_CategoryER._actIdProperty, Util.CommonHelper.GetGuid);
 78             LoadProperty(obj, Asset_CategoryER._aCTParentIDProperty, parentID);
 79             MarkNew(obj);
 80             CheckRules(obj);
 81             return obj;
 82         }
 83
 84         /// <summary>
 85         /// 加载类别
 86         /// </summary>
 87         /// <param name="id">类别ID</param>
 88         /// <returns></returns>
 89         public Asset_CategoryER FetchCategory(string id)
 90         {
 91             var et = base.BaseRepository().FindEntity(id);
 92             if (et != null)
 93             {
 94                 var obj = (Asset_CategoryER)MethodCaller.CreateInstance(typeof(Asset_CategoryER));
 95                 LoadProperty(obj, Asset_CategoryER._actIdProperty, et.ACT_ID);
 96                 LoadProperty(obj, Asset_CategoryER._aCTNameProperty, et.ACT_Name);
 97                 LoadProperty(obj, Asset_CategoryER._aCTParentIDProperty, et.ACT_ParentID);
 98                 LoadProperty(obj, Asset_CategoryER._aCTRemarkProperty, et.ACT_Remark);
 99                 LoadProperty(obj, Asset_CategoryER._aCTSalvageRateProperty, et.ACT_SalvageRate);
100                 LoadProperty(obj, Asset_CategoryER._aCTServiceLifeProperty, et.ACT_ServiceLife);
101                 LoadProperty(obj, Asset_CategoryER._aCTShortNameProperty, et.ACT_ShortName);
102                 LoadProperty(obj, Asset_CategoryER._aCTStateProperty, et.ACT_State);
103                 LoadProperty(obj, Asset_CategoryER._aCTSysCodeProperty, et.ACT_SysCode);
104                 LoadProperty(obj, Asset_CategoryER._aCTUnitProperty, et.ACT_Unit);
105                 LoadProperty(obj, Asset_CategoryER._aCTVoucherProperty, et.ACT_Voucher);
106
107                 MarkOld(obj);
108                 CheckRules(obj);
109                 return obj;
110             }
111             else
112             {
113                 return null;
114             }
115         }
116
117         /// <summary>
118         /// 更新类别
119         /// </summary>
120         /// <param name="obj">更新对象</param>
121         /// <returns></returns>
122         public Asset_CategoryER UpdateCategory(Asset_CategoryER obj)
123         {
124             if (obj.IsDeleted)
125             {
126                 if (!obj.IsNew)
127                 {
128                     //旧对象,删除
129                     DeleteCategory(obj.ActId);
130                 }
131                 //创建新的
132                 MarkNew(obj);
133             }
134             else
135             {
136                 CheckRules(obj);
137                 if (!obj.IsSavable)
138                 {
139                     throw new Csla.Rules.ValidationException(obj.BrokenRulesCollection.ToString());
140                 }
141                 if (obj.IsNew)
142                 {
143                     //创建
144                     Entity.CustomDevelop.AssetEntity.Asset_CategoryEntity entity = new Entity.CustomDevelop.AssetEntity.Asset_CategoryEntity();
145                     entity.ACT_ID = obj.ActId;
146                     entity.ACT_Name = obj.ACTName;
147                     entity.ACT_ParentID = obj.ACTParentID;
148                     entity.ACT_Remark = obj.ACTRemark;
149                     entity.ACT_SalvageRate = obj.ACTSalvageRate;
150                     entity.ACT_ServiceLife = obj.ACTServiceLife;
151                     entity.ACT_ShortName = obj.ACTShortName;
152                     entity.ACT_State = obj.ACTState;
153                     entity.ACT_SysCode = obj.ACTSysCode;
154                     entity.ACT_Unit = obj.ACTUnit;
155                     entity.ACT_Voucher = obj.ACTVoucher;
156
157                     base.BaseRepository().Insert(entity);
158                 }
159                 else
160                 {
161                     //更新
162                     var entity = base.BaseRepository().FindEntity(obj.ActId);
163                     entity.ACT_Name = obj.ACTName;
164                     entity.ACT_ParentID = obj.ACTParentID;
165                     entity.ACT_Remark = obj.ACTRemark;
166                     entity.ACT_SalvageRate = obj.ACTSalvageRate;
167                     entity.ACT_ServiceLife = obj.ACTServiceLife;
168                     entity.ACT_ShortName = obj.ACTShortName;
169                     entity.ACT_State = obj.ACTState;
170                     entity.ACT_SysCode = obj.ACTSysCode;
171                     entity.ACT_Unit = obj.ACTUnit;
172                     entity.ACT_Voucher = obj.ACTVoucher;
173
174                     base.BaseRepository().Update(entity);
175                 }
176                 MarkOld(obj);
177             }
178             return obj;
179         }
180
181         public void DeleteCategory(string id)
182         {
183             base.BaseRepository().Delete(id);
184         }
185
186         #endregion
187     }

数据的访问是通过EF实现,通过定义数据访问基类,承载了EF的访问接口以及csla框架提供的ObjectFactory:

1 public class RepositoryFactory<T> : Csla.Server.ObjectFactory
2         where T : class, new()

以上即实现的Csla业务对象及其数据访问对象,其他类型的如列表对象等都可以通过该方式实现。

欢迎转载,请留标记《HFun.net快速开发平台》

时间: 2024-10-30 00:29:26

csla框架__使用Factory方式实现Csla.BusinessBase对象数据处理的相关文章

CSLA框架的codesmith模板改造

一直有关注CSLA框架,最近闲来无事,折腾了下,在最新的r3054版本基础上修改了一些东西,以备自己用,有兴趣的园友可以下载共同研究 1.添加了默认的授权规则 如果是列表对象则生成列表权限,User的只读列表和可编辑列表生成的都是User.List权限,admin角色具有所有权限: public partial class UserInfoList { #region Authorization Rules /// <summary> /// Allows the specification

SSH框架整合的其它方式

--------------------siwuxie095 SSH 框架整合的其它方式 1.主要是整合 Spring 框架和 Hibernate 框架时,可以不写 Hibernate 核心配置文件:hibernate.cfg.xml 2.把Hibernate 核心配置文件中的配置全都转移到 Spring 核心配置文件中 3.具体实现 applicationContext.xml: <?xml version="1.0" encoding="UTF-8"?&g

PHP--TP框架----生成验证码的方式

TP框架----生成验证码的方式 xianshi.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <

SSM框架搭建之maven方式(二)

一.MAVEN下载安装 maven的下载地址:http://maven.apache.org/download.cgi 解压之后路径 D:\java\apache-maven-3.6.2;新增系统变量[MAVEN_HOME],变量名替换成自己本地解压路径 系统变量[Path]追加值:%MAVEN_HOME%\bin 运行cmd,输入mvn –version或者mvn –v:如果看见下图则说明maven安装配置完毕 本地仓储配置文件:D:\java\apache-maven-3.6.2\conf\

《开源框架那点事儿25》:对框架模板引擎实现方式的改造实录

点滴悟透设计思想,Tiny模板引擎优化实录! 增加框架设计兴趣小组:http://bbs.tinygroup.org/group-113-1.html Tiny模板引擎的实现方式原来是採用的编译方式,近期发生了一些问题.因此我认为有必要把编译方式调整为解释方式,为此就開始了此次实现活动. 编译方式存在的问题 当时採用编译方式.主要是考虑到编译方式在执行时不必再去遍历语法树.因此就採用了编译方式. 可是在实际应用其中,出现了例如以下问题: 文件路径冲突的问题 因为採用的是编译方式,这个时候就存在在

spring框架 事务 注解配置方式

user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.OracleDriver initialPoolSize=15 maxPoolSize=30 minPoolSize=5 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www

开源框架那点事儿25》:对框架模板引擎实现方式的改造实录

点滴悟透设计思想,Tiny模板引擎优化实录! Tiny模板引擎的实现方式原来是采用的编译方式,最近发生了一些问题,因此我觉得有必要把编译方式调整为解释方式,为此就开始了此次实现活动. 编译方式存在的问题 当时采用编译方式,主要是考虑到编译方式在运行时不必再去遍历语法树,因此就采用了编译方式.但是在实际应用当中,出现了如下问题: 文件路径冲突的问题 由于采用的是编译方式,这个时候就存在在一个选择,即:Java源代码落地或不落地的选择.如果Java文件不落地,则在有问题的时候,如果想要进行代码调试(

XPosed框架__一次简单的应用

0. Xposed框架简介 关于Xposed框架相信大家应该不陌生了,他是Android中Hook技术的一个著名的框架,而Xposed框架是免费的而且还是开源的,本文主要介绍如何通过这个框架来进行系统方法的拦截功能,比如我们开发过程中,对于一些测试环境很难模拟,特别是测试同学有时候像随机改变设备的imei,mcc等信息用来模拟不同测试场景,这时候如果可以去修改系统的这个值的话对于测试来说就非常方便了,其实这些在网上已经有很多类似的小工具了,下面就来详细的讲解如何使用这个框架. 1. 编写模块功能

Android(java)学习笔记213:开源框架post和get方式提交数据(qq登录案例)

1.前面提到Http的get/post方式  . HttpClient方式,实际工作的时候不常用到,因为这些方式编写代码是很麻烦的 2.Android应用会经常使用http协议进行传输,网上会有很完善开源框架,已经包装好了http的get/post,还有httpclient等等:这样我们开发周期也会缩短: 3.下面通过一个Android案例来进行,说明常用的Http框架:  在http://github.com/中搜索async http: 这里发现明显第1个android-async-http