ASP.NET三层架构源码(CodeSmith版)之一:Table-BLL层

动软代码生成器生成的ASP.NET三层架构代码比较规范,是学习ASP.NET的好例子

此三层架构改造自动软的工厂模式模板,使用CodeSmith进行重写,以方便大家修改模板文件

以下是针对表格的BLL层源码:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a class including a special informational header" %>  
<%@ Assembly Name="SchemaExplorer" %>  
<%@ Import Namespace="SchemaExplorer" %>  
<%@ Import Namespace="System.Text" %>  

<%@ Property Name="TableName" Type="TableSchema"  DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="" %>
<%@ Property Name="Namespace" Type="String" Category="Context" Description="NameSpace" Default="Crs811NameSpace"%>  
<%@ Property Name="Author" Type="String" Category="Context"  Description="Author" Default="chenr"%>  
<%@ Property Name="TablePrefix" Type="System.String" Default="T" Category="Context" Description="The prefix to remove from table names" %>  
/*------------------------------------------------ 
// File Name:<%=ClearPrefix(TableName.Name) %>.cs 
// File Description:<%=ClearPrefix(TableName.Name) %> Business Logic 
// Author:<%=Author%> 
// Create Time:<%= DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")%> 
//------------------------------------------------*/  
  
using System;  
using System.Text;  
using System.Data;
using System.Collections.Generic;  

using <%=Namespace%>.DBUtility;
using <%=Namespace%>.Model; 
using <%=Namespace%>.DALFactory;
using <%=Namespace%>.IDAL;  
  
namespace <%=Namespace%>.BLL  
{  
    ///   
    /// <%=ClearPrefix(TableName.Name) %>  
    ///   
    public partial class <%=ClearPrefix(TableName.Name) %>
    {  
        private readonly I<%=ClearPrefix(TableName.Name) %> dal = <%=Namespace%>DALFactory.Create<%=ClearPrefix(TableName.Name) %>();
		public <%=ClearPrefix(TableName.Name) %>() {}
		
        #region  BasicMethod
        /// 
		/// 得到最大ID
		/// 
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// 
		/// 是否存在该记录
		/// 
		public bool Exists(<%= GetInParameter(TableName) %>)
		{
			return dal.Exists(<%= GetPK(TableName) %>);
		}

		/// 
		/// 增加一条数据
		/// 
		public bool Add(m<%=ClearPrefix(TableName.Name) %> model)
		{
			return dal.Add(model);
		}

		/// 
		/// 更新一条数据
		/// 
		public bool Update(m<%=ClearPrefix(TableName.Name) %> model)
		{
			return dal.Update(model);
		}

		/// 
		/// 删除一条数据
		/// 
		public bool Delete(<%= GetInParameter(TableName) %>)
		{
			
			return dal.Delete(<%= GetPK(TableName) %>);
		}

		/// 
		/// 得到一个对象实体
		/// 
		public m<%=ClearPrefix(TableName.Name) %> GetModel(<%= GetInParameter(TableName) %>)
		{
			
			return dal.GetModel(<%= GetPK(TableName) %>);
		}

		/// 
		/// 得到一个对象实体,从缓存中
		/// 
		/*public m<%=ClearPrefix(TableName.Name) %> GetModelByCache(<%= GetInParameter(TableName) %>)
		{
			
			string CacheKey = "tTestModel-" + c1+C5+C8;
			object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(<%= GetPK(TableName) %>);
					if (objModel != null)
					{
						int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
						Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (<%=ClearPrefix(TableName.Name) %>)objModel;
		}*/

		/// 
		/// 获得数据列表
		/// 
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// 
		/// 获得前几行数据
		/// 
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// 
		/// 获得数据列表
		/// 
		public List> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// 
		/// 获得数据列表
		/// 
		public List> DataTableToList(DataTable dt)
		{
			List> modelList = new List>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				m<%=ClearPrefix(TableName.Name) %> model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// 
		/// 获得数据列表
		/// 
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// 
		/// 分页获取数据列表
		/// 
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// 
		/// 分页获取数据列表
		/// 
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// 
		/// 分页获取数据列表
		/// 
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod
        
		#region  ExtensionMethod

		#endregion  ExtensionMethod
    }  
}