环境:.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对象数据处理的更多相关文章

  1. CSLA框架的codesmith模板改造

    一直有关注CSLA框架,最近闲来无事,折腾了下,在最新的r3054版本基础上修改了一些东西,以备自己用,有兴趣的园友可以下载共同研究 1.添加了默认的授权规则 如果是列表对象则生成列表权限,User的 ...

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

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

  3. SSM框架整合的其它方式

    ---------------------siwuxie095                                 SSM 框架整合的其它方式         1.主要是整合 Spring ...

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

    TP框架----生成验证码的方式 xianshi.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...

  5. Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一)

    原文:Thinkphp框架拓展包使用方式详细介绍--验证码实例(十一) 拓展压缩包的使用方式详细介绍 1:将拓展包解压:ThinkPHP3.1.2_Extend.zip   --> 将其下的 \ ...

  6. 使用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...”. 原 ...

  7. SSH框架整合的其它方式

    --------------------siwuxie095 SSH 框架整合的其它方式 1.主要是整合 Spring 框架和 Hibernate 框架时,可以不写 Hibernate 核心配置文件: ...

  8. 1、 Shiro框架:认证,授权(验权 2. Shiro框架实现权限控制方式:

    1. Shiro框架:认证,授权(验权) a) 认证逻辑:applicationCode—>通过工具类获取subject对象,调用login方法参数令牌信息->安全管理器------> ...

  9. spring-oauth-server实践:授权方式1、2、3和授权方式4的token对象.authorities产生方式比较

    授权方式1.2.3和授权方式4的token对象.authorities产生方式不同, 前者使用user_privillege构建, 后者直接使用oauth_client_details.authort ...

随机推荐

  1. linux清理缓存的命令

    查看缓存的命令 free -m 清理缓存的命令  echo 1 > /proc/sys/vm/drop_caches echo 2 > /proc/sys/vm/drop_caches e ...

  2. linux下 如何切换到root用户

    默认安装完成之后并不知道root用户的密码,那么如何应用root权限呢? (1)sudo 命令 xzm@ubuntu:~$  sudo 这样输入当前管理员用户密码就可以得到超级用户的权限.但默认的情况 ...

  3. 每天一套题打卡|河南省第十一届ACM/ICPC

    A 计划日 题意:已知李明在YYYY年MM月DD日星期W订了学习计划,现在想看看李明N天后的完成情况和个人总结,你能告诉我那天的日期和星期几吗? 模拟日期计算: 计算星期可以用基姆拉尔森公式 //中国 ...

  4. 20175312 2018-2019-2 《Java程序设计》第9周学习总结

    20175312 2018-2019-2 <Java程序设计>第9周学习总结 教材学习内容总结 已依照蓝墨云班课的要求完成了第九章的学习,主要的学习渠道是PPT,和书的课后习题. 总结如下 ...

  5. BMIP002协议介绍

    比原BMIP002协议 概述 比原链技术社区最近提出了一套资产规范提议,该提议允许在issue类型的交易中实现标准资产token.该标准定义资产在链上的基本功能,以及发行人通过智能合约管理资产的规范. ...

  6. Qt如何实现不规则弹窗

    1.无边框: 2.放图: 3.重写鼠标函数(问题是:怎么确定不规则弹窗的响应范围?挖坑待填)

  7. C++第三章复习与总结(思维导图分享)

    在完成了第三章的学习后,为了便于日后的复习整理,我制作了一张思维导图,有需要的可以自取. 函数的定义与使用 带默认值的函数 在C++中我们可以为函数添加默认的参数值,在调用时可不传入或部分传入参数,为 ...

  8. 垂直对齐:vertical-align:super属性

    <p style=”vertical-align:super;”>垂直对齐<span>上标</span></p> <p>元素默认为块级元素, ...

  9. react native 5.54 出ios版本遇到的坑(应该是在xcode10下才会有的吧)记录。。。。。。 据说5.7已经修复了

    1. config.h找不到 rm -r ~/.rncache/cd node_modules/react-native/third-party/glog-0.3.4/./configure --ho ...

  10. conda环境复制

    配置环境是一个很烦的事,有时候用到服务器需要一遍又一遍的配..太麻烦了,这时候就要用到conda,直接复制已有的环境.事半功倍. 第一种方法:地址复制 首先找到要复制的环境的路径:conda info ...