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快速开发平台》

上一篇:我的一个关于RFID的项目总结


下一篇:重装系统之win10不能进入bios界面