csla框架__使用Factory方式实现Csla.BusinessBase对象数据处理
环境:.net4.6+csla4.6
实现:对象的数据库访问及数据库执行使用Factory方式进行封闭。
正文:
以前在使用csla框架完成业务对象的定义时所有的数据处理都在对象内部实现,也不能说不好,对象很大。作者给了通过Factory的方式将对象的数据访问层进行分离,代码更容易维护。以下是我通过Factory的方式实现对象数据访问。
首先是通过csla的约定定义业务对象,其中包含了属性验证:
[Serializable]
[Csla.Server.ObjectFactory("HFunM.Business.Service.CustomDevelop.AssetManage.CategoriesService, HFunM.Business.Service", "CreateCategory", "FetchCategory", "UpdateCategory", "DeleteCategory")]
public class Asset_CategoryER : Csla.BusinessBase<Asset_CategoryER>
{
#region Contructor public Asset_CategoryER()
{ /* Require use of factory methods */ } #endregion #region Properties public static readonly PropertyInfo<System.String> _actIdProperty
= RegisterProperty<System.String>(p => p.ActId, "主键ID");
/// <summary>
/// 主键ID
/// </summary>
[System.ComponentModel.DataObjectField(true, false)]
public System.String ActId
{
get { return GetProperty(_actIdProperty); }
set { SetProperty(_actIdProperty, value); }
} public static readonly PropertyInfo<System.String> _aCTParentIDProperty
= RegisterProperty<System.String>(p => p.ACTParentID, "上级分类ID", (System.String)null);
/// <summary>
/// 上级分类ID
/// </summary>
public System.String ACTParentID
{
get { return GetProperty(_aCTParentIDProperty); }
set { SetProperty(_aCTParentIDProperty, value); }
} public static readonly PropertyInfo<System.String> _aCTSysCodeProperty
= RegisterProperty<System.String>(p => p.ACTSysCode, "系统分类编号", (System.String)null);
/// <summary>
/// 系统分类编号
/// </summary>
public System.String ACTSysCode
{
get { return GetProperty(_aCTSysCodeProperty); }
set { SetProperty(_aCTSysCodeProperty, value); }
} public static readonly PropertyInfo<System.String> _aCTNameProperty
= RegisterProperty<System.String>(p => p.ACTName, "分类名称", (System.String)null);
/// <summary>
/// 分类名称
/// </summary>
public System.String ACTName
{
get { return GetProperty(_aCTNameProperty); }
set { SetProperty(_aCTNameProperty, value); }
} public static readonly PropertyInfo<System.String> _aCTShortNameProperty
= RegisterProperty<System.String>(p => p.ACTShortName, "名称缩写", (System.String)null);
/// <summary>
/// 名称缩写
/// </summary>
public System.String ACTShortName
{
get { return GetProperty(_aCTShortNameProperty); }
set { SetProperty(_aCTShortNameProperty, value); }
} public static readonly PropertyInfo<System.String> _aCTStateProperty
= RegisterProperty<System.String>(p => p.ACTState, "分类状态", (System.String)null);
/// <summary>
/// 分类状态
/// </summary>
public System.String ACTState
{
get { return GetProperty(_aCTStateProperty); }
set { SetProperty(_aCTStateProperty, value); }
} public static readonly PropertyInfo<System.Int32?> _aCTServiceLifeProperty
= RegisterProperty<System.Int32?>(p => p.ACTServiceLife, "使用年限", (System.Int32?)null);
/// <summary>
/// 使用年限
/// </summary>
public System.Int32? ACTServiceLife
{
get { return GetProperty(_aCTServiceLifeProperty); }
set { SetProperty(_aCTServiceLifeProperty, value); }
} public static readonly PropertyInfo<System.Double?> _aCTSalvageRateProperty
= RegisterProperty<System.Double?>(p => p.ACTSalvageRate, "残值率(%)", (System.Double?)null);
/// <summary>
/// 残值率(%)
/// </summary>
public System.Double? ACTSalvageRate
{
get { return GetProperty(_aCTSalvageRateProperty); }
set { SetProperty(_aCTSalvageRateProperty, value); }
} public static readonly PropertyInfo<System.String> _aCTUnitProperty
= RegisterProperty<System.String>(p => p.ACTUnit, "计算单位", (System.String)null);
/// <summary>
/// 计算单位
/// </summary>
public System.String ACTUnit
{
get { return GetProperty(_aCTUnitProperty); }
set { SetProperty(_aCTUnitProperty, value); }
} public static readonly PropertyInfo<System.String> _aCTRemarkProperty
= RegisterProperty<System.String>(p => p.ACTRemark, "备注", (System.String)null);
/// <summary>
/// 备注
/// </summary>
public System.String ACTRemark
{
get { return GetProperty(_aCTRemarkProperty); }
set { SetProperty(_aCTRemarkProperty, value); }
} public static readonly PropertyInfo<System.String> _aCTVoucherProperty
= RegisterProperty<System.String>(p => p.ACTVoucher, "会计凭证号", (System.String)null);
/// <summary>
/// 会计凭证号
/// </summary>
public System.String ACTVoucher
{
get { return GetProperty(_aCTVoucherProperty); }
set { SetProperty(_aCTVoucherProperty, value); }
} #endregion #region Business Rules /// <summary>
/// Contains the CodeSmith generated validation rules.
/// </summary>
protected override void AddBusinessRules()
{
// Call the base class, if this call isn't made than any declared System.ComponentModel.DataAnnotations rules will not work.
base.AddBusinessRules(); BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(_actIdProperty));
BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(_aCTNameProperty));
BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(_aCTParentIDProperty));
BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(_aCTSysCodeProperty));
BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_actIdProperty, ));
BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTParentIDProperty, ));
BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTSysCodeProperty, ));
BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTNameProperty, ));
BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTShortNameProperty, ));
BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTStateProperty, ));
BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTUnitProperty, ));
BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTRemarkProperty, ));
BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_aCTVoucherProperty, ));
} #endregion #region Factory public static Asset_CategoryER NewCategory(string parentID)
{
return DataPortal.Create<Asset_CategoryER>(parentID);
} public static Asset_CategoryER GetCategory(string id)
{
return DataPortal.Fetch<Asset_CategoryER>(id);
} public static void DeleteCategory(string id)
{
DataPortal.Delete<Asset_CategoryER>(id);
} #endregion
}
可能已经发现了属性Csla.Server.ObjectFactoryAttribute,它就是定义Factory的,其中提供了多种构造方式,如下:
namespace Csla.Server
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = false)]
public class ObjectFactoryAttribute : Attribute
{
public ObjectFactoryAttribute(string factoryType);
public ObjectFactoryAttribute(Type factoryType);
public ObjectFactoryAttribute(string factoryType, string fetchMethod);
public ObjectFactoryAttribute(Type factoryType, string fetchMethod);
public ObjectFactoryAttribute(string factoryType, string createMethod, string fetchMethod);
public ObjectFactoryAttribute(Type factoryType, string createMethod, string fetchMethod);
public ObjectFactoryAttribute(string factoryType, string createMethod, string fetchMethod, string updateMethod, string deleteMethod);
public ObjectFactoryAttribute(Type factoryType, string createMethod, string fetchMethod, string updateMethod, string deleteMethod);
public ObjectFactoryAttribute(string factoryType, string createMethod, string fetchMethod, string updateMethod, string deleteMethod, string executeMethod);
public ObjectFactoryAttribute(Type factoryType, string createMethod, string fetchMethod, string updateMethod, string deleteMethod, string executeMethod); public string FactoryTypeName { get; }
public string CreateMethodName { get; }
public string FetchMethodName { get; }
public string UpdateMethodName { get; }
public string DeleteMethodName { get; }
public string ExecuteMethodName { get; }
}
}
以下是Factory的数据访问实现:
public class CategoriesService : Data.Repository.RepositoryFactory<Entity.CustomDevelop.AssetEntity.Asset_CategoryEntity>
{
#region 列表 /// <summary>
/// 加载所有数据
/// </summary>
/// <returns></returns>
public Asset_CategoryList FetchList()
{
var obj = (Asset_CategoryList)MethodCaller.CreateInstance(typeof(Asset_CategoryList));
var rlce = obj.RaiseListChangedEvents;
obj.RaiseListChangedEvents = false;
base.SetIsReadOnly(obj, false); var child = base.BaseRepository().IQueryable(p => p.ACT_State == "");
foreach (var item in child)
{
obj.Add(CreateItem(item));
} base.SetIsReadOnly(obj, true);
obj.RaiseListChangedEvents = rlce;
return obj;
} /// <summary>
/// 创建列表子对象
/// </summary>
/// <returns></returns>
public Asset_CategoryInfo CreateItem()
{
var obj = (Asset_CategoryInfo)MethodCaller.CreateInstance(typeof(Asset_CategoryInfo));
LoadProperty(obj, Asset_CategoryInfo._actIdProperty, Util.CommonHelper.GetGuid);
MarkNew(obj);
CheckRules(obj);
return obj;
} /// <summary>
///
/// </summary>
/// <param name="et"></param>
/// <returns></returns>
public Asset_CategoryInfo CreateItem(Entity.CustomDevelop.AssetEntity.Asset_CategoryEntity et)
{
var obj = (Asset_CategoryInfo)MethodCaller.CreateInstance(typeof(Asset_CategoryInfo));
MarkAsChild(obj); LoadProperty(obj, Asset_CategoryInfo._actIdProperty, et.ACT_ID);
LoadProperty(obj, Asset_CategoryInfo._aCTNameProperty, et.ACT_Name);
LoadProperty(obj, Asset_CategoryInfo._aCTParentIDProperty, et.ACT_ParentID);
LoadProperty(obj, Asset_CategoryInfo._aCTRemarkProperty, et.ACT_Remark);
LoadProperty(obj, Asset_CategoryInfo._aCTSalvageRateProperty, et.ACT_SalvageRate);
LoadProperty(obj, Asset_CategoryInfo._aCTServiceLifeProperty, et.ACT_ServiceLife);
LoadProperty(obj, Asset_CategoryInfo._aCTShortNameProperty, et.ACT_ShortName);
LoadProperty(obj, Asset_CategoryInfo._aCTStateProperty, et.ACT_State);
LoadProperty(obj, Asset_CategoryInfo._aCTSysCodeProperty, et.ACT_SysCode);
LoadProperty(obj, Asset_CategoryInfo._aCTUnitProperty, et.ACT_Unit);
LoadProperty(obj, Asset_CategoryInfo._aCTVoucherProperty, et.ACT_Voucher); MarkOld(obj);
return obj;
} #endregion #region 主对象 /// <summary>
/// 创建类别
/// </summary>
/// <returns></returns>
public Asset_CategoryER CreateCategory(string parentID)
{
var obj = (Asset_CategoryER)MethodCaller.CreateInstance(typeof(Asset_CategoryER));
LoadProperty(obj, Asset_CategoryER._actIdProperty, Util.CommonHelper.GetGuid);
LoadProperty(obj, Asset_CategoryER._aCTParentIDProperty, parentID);
MarkNew(obj);
CheckRules(obj);
return obj;
} /// <summary>
/// 加载类别
/// </summary>
/// <param name="id">类别ID</param>
/// <returns></returns>
public Asset_CategoryER FetchCategory(string id)
{
var et = base.BaseRepository().FindEntity(id);
if (et != null)
{
var obj = (Asset_CategoryER)MethodCaller.CreateInstance(typeof(Asset_CategoryER));
LoadProperty(obj, Asset_CategoryER._actIdProperty, et.ACT_ID);
LoadProperty(obj, Asset_CategoryER._aCTNameProperty, et.ACT_Name);
LoadProperty(obj, Asset_CategoryER._aCTParentIDProperty, et.ACT_ParentID);
LoadProperty(obj, Asset_CategoryER._aCTRemarkProperty, et.ACT_Remark);
LoadProperty(obj, Asset_CategoryER._aCTSalvageRateProperty, et.ACT_SalvageRate);
LoadProperty(obj, Asset_CategoryER._aCTServiceLifeProperty, et.ACT_ServiceLife);
LoadProperty(obj, Asset_CategoryER._aCTShortNameProperty, et.ACT_ShortName);
LoadProperty(obj, Asset_CategoryER._aCTStateProperty, et.ACT_State);
LoadProperty(obj, Asset_CategoryER._aCTSysCodeProperty, et.ACT_SysCode);
LoadProperty(obj, Asset_CategoryER._aCTUnitProperty, et.ACT_Unit);
LoadProperty(obj, Asset_CategoryER._aCTVoucherProperty, et.ACT_Voucher); MarkOld(obj);
CheckRules(obj);
return obj;
}
else
{
return null;
}
} /// <summary>
/// 更新类别
/// </summary>
/// <param name="obj">更新对象</param>
/// <returns></returns>
public Asset_CategoryER UpdateCategory(Asset_CategoryER obj)
{
if (obj.IsDeleted)
{
if (!obj.IsNew)
{
//旧对象,删除
DeleteCategory(obj.ActId);
}
//创建新的
MarkNew(obj);
}
else
{
CheckRules(obj);
if (!obj.IsSavable)
{
throw new Csla.Rules.ValidationException(obj.BrokenRulesCollection.ToString());
}
if (obj.IsNew)
{
//创建
Entity.CustomDevelop.AssetEntity.Asset_CategoryEntity entity = new Entity.CustomDevelop.AssetEntity.Asset_CategoryEntity();
entity.ACT_ID = obj.ActId;
entity.ACT_Name = obj.ACTName;
entity.ACT_ParentID = obj.ACTParentID;
entity.ACT_Remark = obj.ACTRemark;
entity.ACT_SalvageRate = obj.ACTSalvageRate;
entity.ACT_ServiceLife = obj.ACTServiceLife;
entity.ACT_ShortName = obj.ACTShortName;
entity.ACT_State = obj.ACTState;
entity.ACT_SysCode = obj.ACTSysCode;
entity.ACT_Unit = obj.ACTUnit;
entity.ACT_Voucher = obj.ACTVoucher; base.BaseRepository().Insert(entity);
}
else
{
//更新
var entity = base.BaseRepository().FindEntity(obj.ActId);
entity.ACT_Name = obj.ACTName;
entity.ACT_ParentID = obj.ACTParentID;
entity.ACT_Remark = obj.ACTRemark;
entity.ACT_SalvageRate = obj.ACTSalvageRate;
entity.ACT_ServiceLife = obj.ACTServiceLife;
entity.ACT_ShortName = obj.ACTShortName;
entity.ACT_State = obj.ACTState;
entity.ACT_SysCode = obj.ACTSysCode;
entity.ACT_Unit = obj.ACTUnit;
entity.ACT_Voucher = obj.ACTVoucher; base.BaseRepository().Update(entity);
}
MarkOld(obj);
}
return obj;
} public void DeleteCategory(string id)
{
base.BaseRepository().Delete(id);
} #endregion
}
数据的访问是通过EF实现,通过定义数据访问基类,承载了EF的访问接口以及csla框架提供的ObjectFactory:
public class RepositoryFactory<T> : Csla.Server.ObjectFactory
where T : class, new()
以上即实现的Csla业务对象及其数据访问对象,其他类型的如列表对象等都可以通过该方式实现。
欢迎转载,请留标记《HFun.net快速开发平台》
csla框架__使用Factory方式实现Csla.BusinessBase对象数据处理的更多相关文章
- CSLA框架的codesmith模板改造
一直有关注CSLA框架,最近闲来无事,折腾了下,在最新的r3054版本基础上修改了一些东西,以备自己用,有兴趣的园友可以下载共同研究 1.添加了默认的授权规则 如果是列表对象则生成列表权限,User的 ...
- [King.yue]关于CSLA框架的一些看法
CSLA.Net 是一个有帮助的成熟开发框架,但不适于初学者.该框架支持在任何地方.任何时间创建对象,值得我们花时间去学习了解这一框架.CSLA.Net 框架设计的业务对象,支持对完全透明的数据源进行 ...
- SSM框架整合的其它方式
---------------------siwuxie095 SSM 框架整合的其它方式 1.主要是整合 Spring ...
- PHP--TP框架----生成验证码的方式
TP框架----生成验证码的方式 xianshi.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...
- Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一)
原文:Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一) 拓展压缩包的使用方式详细介绍 1:将拓展包解压:ThinkPHP3.1.2_Extend.zip --> 将其下的 \ ...
- 使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”
使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”. 原 ...
- SSH框架整合的其它方式
--------------------siwuxie095 SSH 框架整合的其它方式 1.主要是整合 Spring 框架和 Hibernate 框架时,可以不写 Hibernate 核心配置文件: ...
- 1、 Shiro框架:认证,授权(验权 2. Shiro框架实现权限控制方式:
1. Shiro框架:认证,授权(验权) a) 认证逻辑:applicationCode—>通过工具类获取subject对象,调用login方法参数令牌信息->安全管理器------> ...
- spring-oauth-server实践:授权方式1、2、3和授权方式4的token对象.authorities产生方式比较
授权方式1.2.3和授权方式4的token对象.authorities产生方式不同, 前者使用user_privillege构建, 后者直接使用oauth_client_details.authort ...
随机推荐
- 本地Git仓库和Github仓库的关联
1.我们首先创建一个文件夹(用于保存本地仓) 在我们想要创建的路径下右键鼠标打开Git Bash Here(创建一个文件夹,并进入文件夹) 2.通过命令git init把我们刚才创建的文件夹变成Git ...
- go-switch特点
程序中遇到有枚举分支逻辑时,需要用到 switch 代替多个 if else 判断. 学习过程遇到一些与C#不同点,记录下. 语法: switch expr { case x1 : //expr为x1 ...
- 开箱即用的VScode C++环境
对大部分人来说vscode配置c++环境十分繁琐,这里提供简单方法. 1. 下载VSCode 官方网站下载(便携版):VSCode-win32-ia32-1.32.1.zip 解压VSCode-win ...
- React-native完整配置流程
开头敲黑板!! 无论你是RN的新手还是老手,跟着流程走,RN项目搭建起来完全不是问题! 一.网址收集 expo配置网址:https://blog.expo.io/building-a-react- ...
- IE低版本 JSON.parse 和stringify 的兼容 (IE8以下)
低版本的IE不支持JSON,JSON对象解析不是随着javascript产生的,找到一段兼容常用的JSON.parse和JSON.stringify的代码if (!window.JSON) {wind ...
- 雷林鹏分享:jQuery EasyUI 数据网格 - 设置冻结列
jQuery EasyUI 数据网格 - 设置冻结列 本实例演示如何冻结一些列,当用户在网格上移动水平滚动条时,冻结列不能滚动到视图的外部. 为了冻结列,您需要定义 frozenColumns 属性. ...
- postgresql 空间函数 随笔
1. ST_Buffer(geometry, double, [integer])buffer操作一个很有用函数,这个函数的第一个参数是要操作的空间几何数据,第二个参数长度(距离),第三个参数为一个整 ...
- tomcat 中项目配置文件统一目录设置
在tomcat 安装目录中 conf 下的 catalina.properties 文件中 有个 shared.loader= 配置为 shared.loader="${catali ...
- ajax请求多次刷新
难道只能设置定时器每隔一秒通过 Ajax 向后台请求数据来实现吗?1.nodejs的 http://socket.io 支持上述 李宏训 所说的三种方式,另外还支持 Flash Socket.隐藏IF ...
- 微信小程序分享及信息追踪
我就是个搬用工—来源:https://www.jianshu.com/p/87a75ec2fd53 小程序分享群及信息追踪 需求 页面分享 小程序页面分享链接增加source参数,值为用户ID加密 ...