ASP.NET三层架构源码(CodeSmith版)之七:Table-所有模板生成

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

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

以下是针对表格的所有模板同时生成的源码:

<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" CompilerVersion="v3.5" Description="生成所有模板文件" %>
<%-- SchemaExplorer --%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%-- 添加源数据库属性 --%>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema"  DeepLoad="True" Optional="False" Category="01. Required" Description="Database" %>
<%@ Property Name="tName" Type="TableSchema"  DeepLoad="True" Optional="True" Category="01. Required" Description="TableName" %>

<%@ Property Name="Namespace" Type="String" Category="Context" Description="NameSpace"%>  
<%@ 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="bGenOne" Type="Boolean" Category="Context"  Description="Generate One Template Or All"%>

<%-- 指明Template的来源 --%>
<%@ Register Name="BLLTemplate" Template="BLL_General.cst" MergeProperties="Flase" ExcludeProperties=""%>
<%@ Register Name="IDALTemplate" Template="IDAL_General.cst" MergeProperties="Flase" ExcludeProperties=""%>
<%@ Register Name="SQLServerDALTemplate" Template="SQLServerDAL_General.cst" MergeProperties="Flase" ExcludeProperties=""%>
<%@ Register Name="ModelTemplate" Template="Model_General.cst" MergeProperties="Flase" ExcludeProperties=""%>

<script runat="template">  

//指定输出路径   
private string Directory = String.Empty;        
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]     
[Optional, NotChecked]    
[DefaultValue("")]    
public string OutputDirectory     
{        
    get        
    {   return Directory;        
    }       
    set        
    {            
        if (value.EndsWith("\\")) value = value.Substring(0, value.Length - 1);            
        Directory = value;        }     
    }
</script>
<script runat="template">

/// <summary>
/// 生成类公共代码
/// </summary>
/// <param name="Template"></param>
/// <param name="table"></param>
/// <param name="strDir"></param>
private void CommonCode(CodeTemplate Template,TableSchema table,String strDir)
{
    string FileDirectory = OutputDirectory + "\\" + strDir + table.Name + ".cs";
    Template.SetProperty("TableName",table);
    Template.SetProperty("Namespace",Namespace); 
    Template.SetProperty("Author",Author);
    Template.SetProperty("TablePrefix",TablePrefix);
    Template.RenderToFile(FileDirectory,true); 
}

/// <summary>
/// 生成类
/// </summary>
/// <param name="Template">模板</param>
/// <param name="tname">表名</param>
/// <param name="strDir">导出文件夹</param>
/// <param name="bOne">是否生成一个表</param>   
private void GenerateClasses(CodeTemplate Template, TableSchema tname, String strDir, Boolean bOne)    
{
    try
    {        
        if (bOne)
        { 
            CommonCode(Template, tname, strDir);
           
        }
        else
        {
            foreach(TableSchema table in this.SourceDatabase.Tables)        
            {            
                CommonCode(Template, table, strDir);   
            }    
        }
    }
    catch (Exception ex) {
        Response.Write("Generate error! Detail: " + ex.Message+"\n");
    }
}
</script>

<%
Response.Write("Generating ... ...\n\n");

this.GenerateClasses(new BLLTemplate(), tName, "BLL\\", bGenOne);
Response.Write("Generate BLL Classes Done!\n\n");

this.GenerateClasses(new IDALTemplate(), tName, "IDAL\\I", bGenOne);
Response.Write("Generate IDAL Classes Done!\n\n");

this.GenerateClasses(new ModelTemplate(), tName, "Model\\m", bGenOne);
Response.Write("Generate Model Classes Done!\n\n");

this.GenerateClasses(new SQLServerDALTemplate(), tName, "SQLServerDAL\\", bGenOne);
Response.Write("Generate SQLServerDAL Classes Done!\n\n");

Response.Write("ALL Done!\n\n");
Response.Write("ALL File in : " + OutputDirectory);

Debug.WriteLine("OK");
%>

 


Comments are closed.