CodeSmith 实体生成模板(C#版)

<%--  
Name: 
Author: Whicet
Description: 
--%>
<% @ 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 = " System.Design "   %>
<% @ Import Namespace = " SchemaExplorer "   %>

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

namespace   <%= NameSpaces %> .EntityModel
{
    [Serializable()]
    
public   class   <%=  CurTable.Name  %> Entity
    {
        
public   <%=  CurTable.Name  %> Entity()
        {
            
// TODO:
        }
        
<%= 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  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 firstLower 
=   "" ;
    
        
foreach (ColumnSchema field  in  CurTable.Columns)
        {
            columnName 
=  field.Name;
            columnType 
=  GetColumnDataType(ML.CS, field);
            firstLower 
=  FirstToLower(columnName);
            
            sb.Append(
" \t "   +   " \t "   +   " private  "   +  columnType   +   "  _ " +  firstLower   +   "   "   +  GetDefaultValue(columnType)  +   " \n " );
            sb.Append(
" \t "   +   " \t "   +   " public   "   +  columnType  +   "   "   +  columnName  +   " \n " );
            sb.Append(
" \t "   +   " \t "   +   " { "   +   " \n " );
            sb.Append(
" \t "   +   " \t "   +   " \t "   +   " get {  "  );
            sb.Append(
"   " +   " return _ "   +  firstLower  +   " ; }\n " );        
            sb.Append(
" \t "   +   " \t "   +   " \t "   +   " set { "  );        
            sb.Append(
"   "   +   " _ "   +  firstLower  +   "  = value; "   +   "  }\n " );            
            sb.Append(
" \t "   +   " \t "   +   " } "   +   " \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 " :
            
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 >


<%-- 
Name: CodeSmith enity  code
Author: Whicet
Description: 
--%>
<%@ 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="System.Design" %>
<%@ Import Namespace="SchemaExplorer" %>

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

namespace <%=NameSpaces%>.EntityModel
{
    [Serializable()]
    
public class <%= CurTable.Name %>Entity
    {
        
public <%= CurTable.Name %>Entity()
        {
            
//TODO:
        }
        
<%=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 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 firstLower 
= "";
    
        
foreach(ColumnSchema field in CurTable.Columns)
        {
            columnName 
= field.Name;
            columnType 
= GetColumnDataType(ML.CS, field);
            firstLower 
= FirstToLower(columnName);
            
            sb.Append(
"\t" + "\t" + "private " + columnType  + " _"+ firstLower  + " " + GetDefaultValue(columnType) + "\n");
            sb.Append(
"\t" + "\t" + "public  " + columnType + " " + columnName + "\n");
            sb.Append(
"\t" + "\t" + "{" + "\n");
            sb.Append(
"\t" + "\t" + "\t" + "get { " );
        
            sb.Append(
" "+ "return _" + firstLower + " };\n");
        
            sb.Append(
"\t" + "\t" + "\t" + "set {" );
        
            sb.Append(
" " + "_" + firstLower + " = value" + " };\n");
            
            sb.Append(
"\t" + "\t" + "}" + "\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":
            
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 实体生成模板(C#版))