我用类似PetShop的方式实现三层架构,数据访问层有一个基类DataAccessObjectBase,实现思路如下:
1、两个实体类OrderInfo和OrderDatailInfo。(Model,作为在各层之间传递的数据对象)
2、数据访问层的基类:
public abstract class DataAccessObjectBase<TDomainObject> where TDomainObject : new()
{
public string ObjectId;
public string ParentObjectId;
public string ChildObjectId;
#region Common StoredProcedure Name List
public string SelectByIdStoredProcedureName;
public string SelectDetailObjectStoredProcedureName;
public string SelectMasterObjectStoredProcedureName;
#endregion
public abstract void LoadDomainObjectInfo(TDomainObject domainObject, System.Data.IDataReader dr);
public TDomainObject SelectById(System.Guid id)
{
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetStoredProcCommand(SelectByIdStoredProcedureName);
DataAccessHelper.PopulateIdParamter(db, cmd, id);
TDomainObject domainObject = new TDomainObject();
try
{
using (IDataReader dr = db.ExecuteReader(cmd))
{
if (dr.Read())
{
LoadDomainObjectInfo(domainObject, dr);
}
}
}
catch (InvalidOperationException ex)
{
//if (ExceptionPolicy.HandleException(ex, SR.ExceptionReplacePolicy)) throw;
}
catch (DataException ex)
{
//if (ExceptionPolicy.HandleException(ex, SR.ExceptionWrapPolicy)) throw;
}
return domainObject;
}
public virtual void LoadDetailObjectInfo<TDetailObject>(TDetailObject detailObject, System.Data.IDataReader dr)
{
throw new Exception("The method or operation is not implemented.");
}
/// <summary>
/// 返回当前实体类的子对象,如订单类,则返回的是订单的订单明细
/// </summary>
/// <typeparam name="TDetailObject"></typeparam>
/// <param name="id"></param>
/// <returns></returns>
public List<TDetailObject> SelectDetailObject<TDetailObject>(System.Guid id) where TDetailObject : new()
{
if( String.IsNullOrEmpty(SelectDetailObjectStoredProcedureName ) ) return null;
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetStoredProcCommand(SelectDetailObjectStoredProcedureName);
DataAccessHelper.PopulateIdParamter(db, cmd, id, ObjectId);
List<TDetailObject> domainObjectList = new List<TDetailObject>();
try
{
using (IDataReader dr = db.ExecuteReader(cmd))
{
while (dr.Read())
{
TDetailObject detailObject = new TDetailObject();
LoadDetailObjectInfo<TDetailObject>((TDetailObject)detailObject, dr);
domainObjectList.Add(detailObject);
}
}
}
catch (InvalidOperationException ex)
{
//if (ExceptionPolicy.HandleException(ex, SR.ExceptionReplacePolicy)) throw;
}
catch (DataException ex)
{
//if (ExceptionPolicy.HandleException(ex, SR.ExceptionWrapPolicy)) throw;
}
return domainObjectList;
}
订单数据访问类:继承于DataAccessObjectBase,
重载了抽象的和虚拟的方法,分别如下:
//////////////////////////////////
public override void LoadDomainObjectInfo(OrderInfo order, System.Data.IDataReader dr)
{
LoadAdminObjectInfo.LoadOrderInfo(order, dr);
}
// 请教:这个方法应该如何重载?<=====================
public override void LoadDetailObjectInfo<TDetailObject>(TDetailObject detailObject, IDataReader dr)
{
LoadAdminObjectInfo.LoadOrderItemInfo(detailObject, dr);
}
其中LoadAdminObjectInfo,是一个辅助类,有两个方法如下:
public static void LoadOrderInfo(OrderInfo order, System.Data.IDataReader dr)
{
order.OrderId = (System.Guid)dr["OrderId"];
order.SequenceNumber = (System.Int32)dr["SequenceNumber"];
order.Name = (System.String)dr["Name"];
}
public static void LoadOrderItemInfo(OrderItemInfo orderItem, System.Data.IDataReader dr)
{
orderItem.OrderItemId = (System.Guid)dr["OrderItemId"];
orderItem.OrderId = (System.Guid)dr["OrderId"];
orderItem.LocalId = (System.String)dr["LocalId"];
}
对于具体的数据访问对象,LoadDetailObjectInfo应该如何重载?