ASP.NET三层架构源码(CodeSmith版)之八:View-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="ViewName" Type="ViewSchema"  DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="" %>
<%@ Property Name="Namespace" Type="String" Category="Context" Description="NameSpace"%>  
<%@ Property Name="Author" Type="String" Category="Context"  Description="Author" Default="chenr"%>  
<%@ Property Name="ViewPrefix" Type="System.String" Default="T" Category="Context" Description="The prefix to remove from table names" %>  
/*------------------------------------------------ 
// File Name:<%=ClearPrefix(ViewName.Name) %>BLL.cs 
// File Description:<%=ClearPrefix(ViewName.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  
{  
    /// <summary>  
    /// <%=ClearPrefix(ViewName.Name) %>  
    /// </summary>  
    public partial class <%=ClearPrefix(ViewName.Name) %>
    {  
        private readonly I<%=ClearPrefix(ViewName.Name) %> dal = <%=Namespace%>DALFactory.Create<%=ClearPrefix(ViewName.Name) %>();
		public <%=ClearPrefix(ViewName.Name) %>() {}
		
        #region  BasicMethod
        
        /// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId(string colName)
		{
			return dal.GetMaxId(colName);
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists()
		{
			return dal.Exists();
		}		

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}        

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

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

		#endregion  BasicMethod
        
		#region  ExtensionMethod

		#endregion  ExtensionMethod
    }  
}  
<script runat="template">  
public string ClearPrefix(string name)  
{  
    /*int mIndex=name.IndexOf(TablePrefix);  
    string strResult=name.Remove(0,TablePrefix.Length);  
    return strResult; */ 
    
    return name;
}  

public string GetCSharpTypeFromDBFieldType(ViewColumnSchema column)  
{ 
    if (column.Name.EndsWith("TypeCode")) return column.Name;
	
	switch (column.DataType)
	{
		case DbType.AnsiString: return "string";
		case DbType.AnsiStringFixedLength: return "string";
		case DbType.Binary: return "byte[]";
		case DbType.Boolean: return "bool";
		case DbType.Byte: return "byte";
		case DbType.Currency: return "decimal";
		case DbType.Date: return "DateTime";
		case DbType.DateTime: return "DateTime";
		case DbType.Decimal: return "decimal";
		case DbType.Double: return "double";
		case DbType.Guid: return "Guid";
		case DbType.Int16: return "short";
		case DbType.Int32: return "int";
		case DbType.Int64: return "long";
		case DbType.Object: return "object";
		case DbType.SByte: return "sbyte";
		case DbType.Single: return "float";
		case DbType.String: return "string";
		case DbType.StringFixedLength: return "string";
        //case DbType.StringFixedLength: return "UniqueIdentifier";
		case DbType.Time: return "TimeSpan";
		case DbType.UInt16: return "ushort";
		case DbType.UInt32: return "uint";
		case DbType.UInt64: return "ulong";
		case DbType.VarNumeric: return "decimal";
		default:
		{
			return "__UNKNOWN__" + column.NativeType;
		}
	}
}  
</script>

 


Comments are closed.