环境:.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. Python入门经典案例一

    # 有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? sum = 0 values = range(1, 5) for i in values: for j in valu ...

  2. Axios Token验证拦截器

    import axios from 'axios'; // req拦截 axios.interceptors.request.use( //设置头部的token config.headers['tok ...

  3. Python打开新世界的大门-入门篇1

    目录 题记 Python技巧.避坑及心得 八种数据类型 循环 函数 Homework 题外话 之前没有写博客的习惯,现在开始写觉得入门也太晚了吧,看看同龄的大哥都写了十几万字.于是 ...

  4. npm版本安装问题

    问题一 描述 运行npm install之后,前端页面console控制台报错,invalid props. 排查 1. 排除了代码问题,完全一样的代码,其他人的运行无误. 2.猜想可能是版本号问题, ...

  5. python转义符

  6. Spring框架IOC和AOP的实现原理(概念)

    IoC(Inversion of Control) (1). IoC(Inversion of Control)是指容器控制程序对象之间的关系,而不是传统实现中,由程序代码直接操控.控制权由应用代码中 ...

  7. vi编程技巧:

    h #向上j #向左k #向右l #向下a #插入o #插入一行,并在行首开始O #在当前行前插入一行,并在行首开始dd #删除当前行x #删除当前字符yy #复制当前行p #在当前行后面粘贴P #在 ...

  8. .Net Core使用视图组件(ViewComponent)封装表单文本框控件

    实例程序的界面效果如下图所示: 在表单中的搜索条件有姓名,学号,成绩.他们在一行中按照水平三等分排列. 在cshtml中用html实现上述表单效果的的代码如下: <form class=&quo ...

  9. RecyclerView嵌套ScrollView导致RecyclerView内容显示不全

    我们在使用RecyclerView嵌套至ScrollView内的时候 RecyclerView不在屏幕内的数据会不显示出来,这里是一个坑,我们需要重写RecyclerView /** * Create ...

  10. MongDB 批量更新

    最近公司在使用mongodb.  批量更新的语句为: db.table.update(  {'wo': {$in: [ "123", "456"]}}, {$s ...