zoukankan      html  css  js  c++  java
  • 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>

    作者:SCPlatform
    Email:SCPlatform@outlook.com
    本文旨在学习、交流使用,任何对文章内容的出版、印刷,对文章中提到的技术的商业使用时,请注意可能存在的法律风险。

  • 相关阅读:
    【VS开发】【C++开发】const在函数前与函数后的区别
    【VS开发】【C++开发】正确使用auto_ptr智能指针
    【VS开发】【C++开发】正确使用auto_ptr智能指针
    【数据库开发】Redis数据库设置密码
    【数据库开发】Redis数据库设置密码
    【数据库开发】Redis数据库服务器启动配置
    【数据库开发】Redis数据库服务器启动配置
    【数据库开发】windows下hiredis的编译(主要是包括一些异步编程的错误)
    【数据库开发】windows下hiredis的编译(主要是包括一些异步编程的错误)
    【VS开发】Windows平台下Makefile学习笔记
  • 原文地址:https://www.cnblogs.com/SCPlatform/p/992460.html
Copyright © 2011-2022 走看看