环境:.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 }