<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="C#" Description="生成千牛星所有模板" Debug="True" CompilerVersion="v4.0" ResponseEncoding="UTF-8"%>
<%@ Register Name="EntityTemplate" Template="QNX_Entity.cst" MergeProperties="Flase" ExcludeProperties=""%>
<%@ Register Name="DALTemplate" Template="QNX_DAL.cst" MergeProperties="Flase" ExcludeProperties=""%>
<%@ Register Name="StoreProcedureTemplate" Template="QNX_StoreProc.cst" MergeProperties="Flase" ExcludeProperties=""%>
<%@ Register Name="LogicTemplate" Template="QNX_Logic.cst" MergeProperties="Flase" ExcludeProperties=""%>
 
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated." %>
 
<%
if(this.OutputDirectory=="" || !System.IO.Directory.Exists(this.OutputDirectory))
{
    Debug.WriteLine("----------------------Error: 请核实OutputDirectory是否存在!----------------------");
    return;
}
  
this.GenerateClasses(); 
 
Debug.WriteLine("Create Over!");
%>
 
 
<script runat="template">
 
  
    //生成实体Entity类
    private void GenerateClasses()
    {
        Debug.WriteLine("----------------------实体Entity类 生成Start----------------------");
         
         
        foreach(TableSchema table in this.SourceDatabase.Tables)
        {
            CodeTemplate Template = new EntityTemplate();
            string FileDirectory = this.GetFileDirectory(table.Name,"Entity",".cs");
            Template.SetProperty("SourceTable",table); 
            //文件输出
            Template.RenderToFile(FileDirectory,true);
            Debug.WriteLine(FileDirectory  " 创建成功.");
        }
        Debug.WriteLine("----------------------实体Entity类 生成End----------------------");
         
         
         
        Debug.WriteLine("----------------------DAL类 生成Start----------------------");
        
         
        foreach(TableSchema table in this.SourceDatabase.Tables)
        {
            CodeTemplate dal = new DALTemplate();
            string FileDirectory = this.GetFileDirectory(table.Name,"DAL",".cs");
            dal.SetProperty("SourceTable",table); 
            //文件输出
            dal.RenderToFile(FileDirectory,true);
            Debug.WriteLine(FileDirectory  " 创建成功.");
        }
        Debug.WriteLine("----------------------DAL类 生成End----------------------");
         
         
        Debug.WriteLine("----------------------StoreProcedure 生成Start----------------------");
         
         
        foreach(TableSchema table in this.SourceDatabase.Tables)
        {
            CodeTemplate sql = new StoreProcedureTemplate();
            string FileDirectory = this.GetFileDirectory(table.Name,"SQL",".sql");
            sql.SetProperty("SourceTable",table); 
            //文件输出
            sql.RenderToFile(FileDirectory,true);
            Debug.WriteLine(FileDirectory  " 创建成功.");
        }
        Debug.WriteLine("----------------------StoreProcedure 生成End----------------------");
         
        Debug.WriteLine("----------------------Logic 生成Start----------------------");
         
         
        foreach(TableSchema table in this.SourceDatabase.Tables)
        {
            CodeTemplate sql = new LogicTemplate();
            string FileDirectory = this.GetFileDirectory(table.Name,"Logic",".cs");
            sql.SetProperty("SourceTable",table); 
            //文件输出
            sql.RenderToFile(FileDirectory,true);
            Debug.WriteLine(FileDirectory  " 创建成功.");
        }
        Debug.WriteLine("----------------------Logic 生成End----------------------");
         
    }
     //将字符串首字母转换为大写  
    private string MakeCamel(string value)
    {
        return value.Substring(0, 1).ToUpper()   value.Substring(1);
    }
     
    private string GetFileDirectory(string flolderName,string tabName,string surfix)
    {
        return string.Format("{0}\\{1}\\{2}{3}",OutputDirectory,flolderName,MakeCamel(tabName),surfix);
    }
     
     
    //解决方案输出路径
    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>



Leave a Reply