ASP.NET三层架构源码(CodeSmith版)之二:Table-IDAL层(数据访问抽象层)

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

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

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

<%@ 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="Namespace" Type="String" Category="Context" Description="The namespace to use for this class" 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" %>  
<%@ Property Name="TableName" Type="TableSchema"  DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="" %>
/*------------------------------------------------ 
// File Name:I<%=ClearPrefix(TableName.Name) %>.cs 
// File Description:<%=ClearPrefix(TableName.Name) %> Interface 
// Author:<%=Author%> 
// Create Time:<%= DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")%> 
//------------------------------------------------*/  
  
using System;  
using System.Text;  
using System.Collections.Generic;  
using System.Data;

using <%=Namespace%>.Model;  
  
namespace <%=Namespace%>.IDAL  
{  
    /// <summary>  
    /// <%=ClearPrefix(TableName.Name) %> Interface  
    /// </summary>  
    public interface I<%=ClearPrefix(TableName.Name) %>  
    {  
        #region  成员方法
        /// <summary>
		/// 得到最大ID
		/// </summary>
		int GetMaxId();
        
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		bool Exists(<%= GetInParameter(TableName) %>);
        
		/// <summary>
		/// 增加一条数据
		/// </summary>
		bool Add(m<%=ClearPrefix(TableName.Name) %> model);
        
		/// <summary>
		/// 更新一条数据
		/// </summary>
		bool Update(m<%=ClearPrefix(TableName.Name) %> model);
        
		/// <summary>
		/// 删除一条数据
		/// </summary>
		bool Delete(<%= GetInParameter(TableName) %>);
        <% if (TableName.PrimaryKey.MemberColumns.Count == 1) {%>        
		bool DeleteList(string <%=TableName.PrimaryKey.MemberColumns[0].Name%>list);
        <%}%>
        
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		m<%=ClearPrefix(TableName.Name) %> GetModel(<%= GetInParameter(TableName) %>);
		m<%=ClearPrefix(TableName.Name) %> DataRowToModel(DataRow row);
        
		/// <summary>
		/// 获得数据列表
		/// </summary>
		DataSet GetList(string strWhere);
        
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		DataSet GetList(int Top,string strWhere,string filedOrder);
		int GetRecordCount(string strWhere);
		DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex);
        
		/// <summary>
		/// 根据分页获得数据列表
		/// </summary>
		//DataSet GetList(int PageSize,int PageIndex,string strWhere);
        
		#endregion  成员方法

		#region  MethodEx

		#endregion  MethodEx
	}  
}  

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

public string GetCSharpTypeFromDBFieldType(ColumnSchema 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;
		}
	}
}

public string GetInParameter(TableSchema TableName)
{
    string strReturn = "";
    
    for (int i=0; i<TableName.PrimaryKey.MemberColumns.Count; i++)
    {                
        strReturn += GetCSharpTypeFromDBFieldType(TableName.PrimaryKey.MemberColumns[i])+" "+TableName.PrimaryKey.MemberColumns[i].Name+",";
    }
    
    return strReturn.TrimEnd(',');
}
</script>

 


Comments are closed.