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

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

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

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

<%@ CodeTemplate Inherits="CodeTemplate" language="C#" TargetLanguage="Text" Description="NetTiers main template."Debug="True" ResponseEncoding="UTF-8"%>
<%@ Assembly Name="SchemaExplorer" %>  
<%@ Import Namespace="SchemaExplorer" %>  
<%@ Import Namespace="System.Text" %>  
<%@ Property Name="Namespace" Type="String" Category="Context" Description="Namespace" Default="CrsNamespace"%>  
<%@ 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:m<%=ClearPrefix(TableName.Name) %>.cs 
// File Description:m<%=ClearPrefix(TableName.Name) %> DataBase Entity 
// Author:<%=Author%> 
// Create Time:<%= DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")%> 
//------------------------------------------------*/  
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;  
  
namespace <%=Namespace%>.Model  
{  
   ///   
   /// Description <%=TableName.Description %>  
   ///   
   [Serializable]  
   public class m<%=ClearPrefix(TableName.Name) %> 
   {  
        #region Private Protery  
        <%for(int i=0;i  
        private <%=CSharpType(TableName.Columns[i])%> _<%=TableName.Columns[i].Name.ToLower()%>;
        <%--<%=CSharpType(TableName.Columns[i])%> _<%=TableName.Columns[i].Name.Substring(0,1).ToLower()+TableName.Columns[i].Name.Substring(1,TableName.Columns[i].Name.Length-1)%>;  --%>
        <%}%>  
        #endregion  
   
        #region Constructor   
        ///   
        /// m<%=ClearPrefix(TableName.Name) %> Constructor  
        ///   
        public m<%=ClearPrefix(TableName.Name) %>()  
        {  
        }  
          
        ///   
        /// m<%=ClearPrefix(TableName.Name) %> Constructor With Parameters  
        ///   
        <%for(int i=0;i 
        /// <%=TableName.Columns[i].Name.ToLower()%>
        <%--///<%if(TableName.Columns[i].Description.Length>0){%><%=TableName.Columns[i].Name+"  "+TableName.Columns[i].Description%><%}else{%><%=TableName.Columns[i].Name%><%}%>  --%>
        <%}%>  
        <%--public m<%=ClearPrefix(TableName.Name) %>(<%for(int i=0;i<%=CSharpType(TableName.Columns[i])%> <%=TableName.Columns[i].Name.Substring(0,1).ToLower()+TableName.Columns[i].Name.Substring(1,TableName.Columns[i].Name.Length-1)%><% if (i < TableName.Columns.Count - 1) { %>,<%}%><%}%>)  --%>
        public m<%=ClearPrefix(TableName.Name) %>(<%for(int i=0;i<%=CSharpType(TableName.Columns[i])%> <%=TableName.Columns[i].Name%><% if (i < TableName.Columns.Count - 1) { %>,<%}%><%}%>)
        {  
        <%for(int i=0;i  
            <%--this._<%=TableName.Columns[i].Name.Substring(0,1).ToLower()+TableName.Columns[i].Name.Substring(1,TableName.Columns[i].Name.Length-1)%> = <%=TableName.Columns[i].Name.Substring(0,1).ToLower()+TableName.Columns[i].Name.Substring(1,TableName.Columns[i].Name.Length-1)%>;  --%>
            this._<%=TableName.Columns[i].Name.ToLower()%> = <%=TableName.Columns[i].Name%>;
        <%}%>  
        }  
        #endregion  
     
        #region Public Property  
        <%for(int i=0;i  
        ///  
        /// <%=TableName.Columns[i].Name%><%if(TableName.Columns[i].Description.Length>0){%>  
        /// <%=TableName.Columns[i].Description%><%}%>  
        ///  
        public <%=CSharpType(TableName.Columns[i]).Trim()%> <%=TableName.Columns[i].Name%> { get; set; }
        
        <%}%> 
        #endregion    
   }  
}