C# CodeSmith 模板

2022-01-26  本文已影响0人  Rinaloving

<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" 
Debug="False" Description="This is a enity class" %>
<%@ Property Name="CurDB" Type="SchemaExplorer.DatabaseSchema"  
Category="Context" Description="Database property." %>
<%@ Property Name="CurTable" Type="SchemaExplorer.TableSchema" 
Category="Context" Description="Table property" %>
<%@ Property Name="MakeLanguage" Type="ML" Default="2" Optional="False"
Category="Other"   Description="Language" %>
<%@ Property Name="NameSpaces" Type="String" Default="MemberCard" Optional="False"
Category="Other"  Description="Namespace" %>

<%@ Assembly Name="System.Data" %>
<%@ Assembly Name="System.Design" %>
<%@ Assembly Name="SchemaExplorer" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="SchemaExplorer" %>

<%------------------------------------conditional content--------------------------------%>
<% if (MakeLanguage == ML.CS) { %>
using System;
using SqlSugar;

namespace WebApi.Models
{
    [SugarTable("<%= CurTable.Name %>")]
    public class <%= CurTable.Name %>Model
    {
        <%=GetMakeCode(ML.CS)%>
    }
}
<% } %>
<%else if(MakeLanguage == ML.VB) { %>
Imports System
Imports System.Data

namespase <%=NameSpaces%>.EntityModel

    [Serializable()]
    Public Class <%= CurTable.Name %>Entity  
        Public Sub New()
        End Sub
        
        <%=GetMakeCode(ML.VB)%>
    End Class '<%= CurTable.Name %>Entity 
End Namespace
<% } %>
<%------------------------------------methods--------------------------------%>

<script runat="template">
 #region normal
  public enum ML
    {
        VB=0,
        CS=2
    }
    
    public string GetMakeCode(ML MakeLang)
    {
        String result = "";
        if(MakeLang == ML.VB)
        {
            result = GetVBCode();
        }
        else if(MakeLang == ML.CS)
        {
            result = GetCSCode();
        }
        return result;
    }
  #endregion

  #region FirstToLower
    public string FirstToLower(string Str)
    {
        String s = "";
        s = Str.Substring(0,1).ToLower() + Str.Substring(1);
        return s;
    }
    #endregion
    #region FirstToUpper
    public string FirstToUpper(string Str)
    {
        String s = "";
        s = Str.Substring(0,1).ToUpper() + Str.Substring(1);
        return s;
    }
    #endregion

    #region Get VBCode and CSCode
    public string GetVBCode()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder(100);
        String columnName = "";
        String columnType = "";
        String firstLower = "";
    
        foreach(ColumnSchema field in CurTable.Columns)
        {
            columnName = field.Name;
            columnType = GetColumnDataType(ML.VB, field);
            firstLower = FirstToLower(columnName);
            
            sb.Append("\t" + "Private _" + firstLower + " As " + columnType + GetDefaultValue(columnType) + "\n");
            sb.Append("\t" + "<ColumnName()> Public Property " + columnName + "() As " + columnType + "\n");
            sb.Append("\t" + "\t" + "Get" + "\n");
            sb.Append("\t" + "\t" + "\t" + "Return _" + firstLower + "\n");
            sb.Append("\t" + "\t" + "End Get" + "\n");
            sb.Append("\t" + "\t" + "Set(ByVal Value As " + columnType + ")" + "\n");
            sb.Append("\t" + "\t" + "\t" + "_" + firstLower + " = Value" + "\n");
            sb.Append("\t" + "\t" + "End Set" + "\n");
            sb.Append("\t" + "End Property" + "\n" + "\n");        
        }
        return sb.ToString();
    }
    public string GetCSCode()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder(100);
        String columnName = "";
        String columnType = "";
        String firstUpper = "";
        String discrib = "";
    
        foreach(ColumnSchema field in CurTable.Columns)
        {
            if(field.Name.Equals("pkCode")){
                 columnName = "Code";
            }else{
                 columnName = field.Name;
            }
            discrib = field.Description;
            columnType = GetColumnDataType(ML.CS, field);
            firstUpper = FirstToUpper(columnName);
            sb.Append("/// <summary>"+"\n");
            sb.Append("///"+discrib+"\n");
            sb.Append("/// <summary>"+"\n");
            if(field.Name.Equals("pkCode")){
                sb.Append("\t"+"[SugarColumn(ColumnName = "+"\""+field.Name+"\""+", IsPrimaryKey = true "+")]"+"\n");
            }else{
               sb.Append("\t"+"[SugarColumn(ColumnName = "+"\""+field.Name+"\""+")]"+"\n");
            }
            sb.Append("\t" + "\t" + "public  " + columnType + " " + firstUpper );
            sb.Append("{");
            sb.Append("\t" + "get;" );   
            sb.Append("\t" + "set;" );             
            sb.Append("}" + "\n" );        
        }
        return sb.ToString();
    }
    #endregion
    #region Get Default Valu.
    public string GetDefaultValue(string columnType)
    {
        String result = "";
        switch(columnType)
        {
            case "Integer":
            case "Double":
                result = "= 0 ";
                break;
            case "int":
            case "double":
                result = "= 0; ";
                break;
            case "String":
                result = "= \"\"";
                break;
            case "string":
                result = "= \"\";";
                break;
            case "DateTime":
                if(MakeLanguage == ML.VB)
                {
                    result = "= System.DateTime.Now ";
                }
                else if(MakeLanguage == ML.CS)
                {
                    result = "= System.DateTime.Now;";
                }    
                break;
            case "Boolean":
                result = "= False ";
                break;
            case "bool":
                result = "= false; ";
                break;
            default:
                if(MakeLanguage == ML.VB)
                {
                    result = " = new " + columnType;
                }
                else if(MakeLanguage == ML.CS)
                {
                    result = " = new " + columnType + ";";
                }    
                break;
        }
        return result;
    }
    #endregion
    #region Get ColumnData Type.
    public string GetColumnDataType(ML makeLang, SchemaExplorer.ColumnSchema columnField)
    {
        String result = "";
        switch(columnField.NativeType.ToLower())
        {
            case "int":
            case "tinyint":
            case "smallint":
            case "bigint":
                if(MakeLanguage == ML.VB)
                {
                    result = "Integer";
                }
                else if(MakeLanguage == ML.CS)
                {
                    result = "int";
                }    
                break;
            case "decimal":
            case "float":
            case "money":
            case "numeric":
            case "smallmoney":
                if(MakeLanguage == ML.VB)
                {
                    result = "Double";
                }
                else if(MakeLanguage == ML.CS)
                {
                    result = "double";
                }    
                break;
            case "char":
            case "nchar":
            case "ntext":
            if(MakeLanguage == ML.VB)
            {
                result = "String";
            }
            else if(MakeLanguage == ML.CS)
            {
                result = "string";
            }    
            break;
            case "text":
            if(MakeLanguage == ML.VB)
            {
                result = "String";
            }
            else if(MakeLanguage == ML.CS)
            {
                result = "string";
            }    
            break;
            
            case "varchar":
            case "nvarchar":
                if(MakeLanguage == ML.VB)
                {
                    result = "String";
                }
                else if(MakeLanguage == ML.CS)
                {
                    result = "string";
                }    
                break;
            case "smalldatetime":
            case "timestamp":
            case "datetime":
                result = "DateTime";
                break;
            case "bit":
                if(MakeLanguage == ML.VB)
                {
                    result = "Boolean";
                }
                else if(MakeLanguage == ML.CS)
                {
                    result = "bool";
                }    
                break;
            case "binary":
            case "image":
            case "varbinary":
                if(MakeLanguage == ML.VB)
                {
                    result = "Byte()";
                }
                else if(MakeLanguage == ML.CS)
                {
                    result = "byte[]";
                }    
                break;
        }
        return result;
    }
    #endregion
</script>

结果:
CodeSmith.png
上一篇 下一篇

猜你喜欢

热点阅读