ASP.NET三层架构源码(CodeSmith版)之十:View-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"%>  
<%@ 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" %>  
<%@ Property Name="ViewName" Type="ViewSchema"  DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description=""%>
/*------------------------------------------------ 
// File Name:m<%=ClearPrefix(ViewName.Name) %>.cs 
// File Description:<%=ClearPrefix(ViewName.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  
{  
   /// <summary>  
   /// Description <%=ViewName.Description %>  
   /// </summary>  
   [Serializable]  
   public class m<%=ClearPrefix(ViewName.Name) %> 
   {  
        #region Private Protery  
        <%for(int i=0;i<ViewName.Columns.Count;i++){%>  
        private <%=CSharpType(ViewName.Columns[i])%> _<%=ViewName.Columns[i].Name.ToLower()%>;
        <%--<%=CSharpType(ViewName.Columns[i])%> _<%=ViewName.Columns[i].Name.Substring(0,1).ToLower()+ViewName.Columns[i].Name.Substring(1,ViewName.Columns[i].Name.Length-1)%>;  --%>
        <%}%>  
        #endregion  
   
        #region Constructor   
        /// <summary>  
        /// m<%=ClearPrefix(ViewName.Name) %> Constructor  
        /// </summary>  
        public m<%=ClearPrefix(ViewName.Name) %>()  
        {  
        }  
          
        /// <summary>  
        /// m<%=ClearPrefix(ViewName.Name) %> Constructor With Parameters  
        /// </summary>  
        <%for(int i=0;i<ViewName.Columns.Count;i++){%> 
        /// <param name="<%=ViewName.Columns[i].Name.ToLower()%>"><%=ViewName.Columns[i].Name.ToLower()%></param>
        <%--///<param name="<%=ViewName.Columns[i].Name.Substring(0,1).ToLower()+ViewName.Columns[i].Name.Substring(1,ViewName.Columns[i].Name.Length-1)%>"><%if(ViewName.Columns[i].Description.Length>0){%><%=ViewName.Columns[i].Name+"  "+ViewName.Columns[i].Description%><%}else{%><%=ViewName.Columns[i].Name%><%}%></param>  --%>
        <%}%>  
        <%--public m<%=ClearPrefix(ViewName.Name) %>(<%for(int i=0;i<ViewName.Columns.Count;i++){%><%=CSharpType(ViewName.Columns[i])%> <%=ViewName.Columns[i].Name.Substring(0,1).ToLower()+ViewName.Columns[i].Name.Substring(1,ViewName.Columns[i].Name.Length-1)%><% if (i < ViewName.Columns.Count - 1) { %>,<%}%><%}%>)  --%>
        public m<%=ClearPrefix(ViewName.Name) %>(<%for(int i=0;i<ViewName.Columns.Count;i++){%><%=CSharpType(ViewName.Columns[i])%> <%=ViewName.Columns[i].Name%><% if (i < ViewName.Columns.Count - 1) { %>,<%}%><%}%>)
        {  
        <%for(int i=0;i<ViewName.Columns.Count;i++){%>  
            <%--this._<%=ViewName.Columns[i].Name.Substring(0,1).ToLower()+ViewName.Columns[i].Name.Substring(1,ViewName.Columns[i].Name.Length-1)%> = <%=ViewName.Columns[i].Name.Substring(0,1).ToLower()+ViewName.Columns[i].Name.Substring(1,ViewName.Columns[i].Name.Length-1)%>;  --%>
            this._<%=ViewName.Columns[i].Name.ToLower()%> = <%=ViewName.Columns[i].Name%>;
        <%}%>  
        }  
        #endregion  
     
        #region Public Property  
        <%for(int i=0;i<ViewName.Columns.Count;i++){%>  
        ///<Summary>  
        /// <%=ViewName.Columns[i].Name%><%if(ViewName.Columns[i].Description.Length>0){%>  
        /// <%=ViewName.Columns[i].Description%><%}%>  
        ///</Summary>  
        public <%=CSharpType(ViewName.Columns[i]).Trim()%> <%=ViewName.Columns[i].Name%> { get; set; }
        <%}%>  
        #endregion      
   }  
}  

<script runat="template">  
public string CSharpType(ViewColumnSchema column)  
{  
 if (column.Name.EndsWith("TypeCode")) return column.Name;  
 switch (column.DataType)  
 {  
  case DbType.AnsiString:  
  case DbType.String:  
  case DbType.StringFixedLength:  
  case DbType.AnsiStringFixedLength: return "string";  
  
  case DbType.VarNumeric:  
  case DbType.Currency:  
  case DbType.Decimal: return "decimal";  
  
  case DbType.Binary: return "byte[]";  
  case DbType.Boolean: return "bool";  
  case DbType.Byte: return "byte";  
  case DbType.Date: return "DateTime";  
  case DbType.DateTime: return "DateTime";  
  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.Time: return "TimeSpan";  
  case DbType.UInt16: return "ushort";  
  case DbType.UInt32: return "uint";  
  case DbType.UInt64: return "ulong";  
  default:  
  {  
   return "__UNKNOWN__" + column.NativeType;  
  }  
 }  
}  

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

 


Comments are closed.