[C#]CodeSmith生成SQL Server视图的实体类脚本

第一个文件是businessobjctforView.cst,生成C#实体类,此脚本需要引用第二个文件CommonUtility.cs。需将两个文件放在同一目录中。

<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
      Description
="Generates a update stored procedure." %>
<%@ Assembly Name="SchemaExplorer" %> 
<%@ Import Namespace="SchemaExplorer" %> 
<%@ Property Name="SourceTable" Type="SchemaExplorer.ViewSchema"
      Category
="Context"
      Description
="Table that the stored procedures should be based on." %>
<%@ Assembly Src="CommonUtility.cs" %>
<%@ Import Namespace="Common.Data" %>
<script runat="template">
    CommonUtility rule
=new CommonUtility();
</script>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;                                                                                                                                                         
namespace SOA.Model
{
    
/// <summary>
    
/// <%= SourceTable.Name %> object for View table '<%= SourceTable.Name %>'.
    
/// </summary>
    [Serializable]
    
public class <%= SourceTable.Name %>
    {
    
#region Private Member
        
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
        
private <%= rule.GetCSharpVariableType(SourceTable.Columns[i]) %> _<%= SourceTable.Columns[i].Name %>;
    
<% } %>
    
#endregion
        
    
#region Constructor
        
public <%= SourceTable.Name %>() {}        
        
        
public <%= SourceTable.Name %>(
                
<% for (int i = 0; i < SourceTable.Columns.Count1; i++) { %>
        
<%= rule.GetCSharpVariableType(SourceTable.Columns[i]) %> <%= SourceTable.Columns[i].Name %>,    
    
<% } %>
    
<%= rule.GetCSharpVariableType(SourceTable.Columns[SourceTable.Columns.Count1]) %> <%= SourceTable.Columns[SourceTable.Columns.Count1].Name %>
        ) 
        {
        
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
         _
<%= SourceTable.Columns[i].Name %> = <%= SourceTable.Columns[i].Name %>;
    
<% } %>
        }
    
#endregion
    
    
#region Public Properties
    
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
        
public <%= rule.GetCSharpVariableType(SourceTable.Columns[i]) %> <%= rule.GetCamelCaseName(SourceTable.Columns[i].Name) %>
        {
            
get { return _<%= SourceTable.Columns[i].Name %>; }
            
set { _<%= SourceTable.Columns[i].Name %> = value; }
        }
    
<% } %>
    
#endregion
    }
}
 
第二个文件是CommonUtility.cs,此文件是包含C#的数据类型定义以及输出:

using System;
using System.Text;
using CodeSmith.Engine;
using SchemaExplorer;
using System.ComponentModel;
using System.Data;
namespace Common.Data
{
    
/**//// <summary>
    
/// TemplateRule
    
/// </summary>
    public class CommonUtility
    {
        
//get Columns info by TableName
        public ViewColumnSchemaCollection GetColumnCollectionByTable(ViewSchema table)
        {
            ViewColumnSchemaCollection columns 
= new ViewColumnSchemaCollection(table.Columns);
            
return columns;
        }
        
//Get camelcase name,such as Customer,
        public string GetCamelCaseName(string str)
        {
            
return str.Substring(0,1).ToUpper()+str.Substring(1);
        }
        
       
//Get ,user,private const String USER_FIELD = "User"
        public string GetMemberConstantDeclarationStatement(ColumnSchema column)
        {
            
return GetMemberConstantDeclarationStatement("public const String ",column);
        }
        
        
//such as public const String USER_TABLE = "User"
        public string GetTableConstantDeclarationStatement(ViewSchema table)
        {
            
return GetMemberConstantDeclarationStatement("public const String ",table);    
        }
        
//suck as USER_TABLE
        public string GetUpperStatement(ViewSchema table)
        {
            
return     table.Name.ToUpper()+"_TABLE";
       }
        
//suck as USER_FIELD
        public string GetUpperStatement(ColumnSchema column)
       {
           
return column.Name.ToUpper()+"_FIELD";
        }
        
// such as USER_TABLE = "User"
        public string GetMemberConstantDeclarationStatement(string protectionLevel, ViewSchema table)
        {
            
return protectionLevel+GetUpperStatement(table)+" = "+GetCamelCaseName(table.Name)+"";
        }
       
        
//such as USERID_FIELD = "Userid"
        public string GetMemberConstantDeclarationStatement(string protectionLevel,ColumnSchema column)
        {
            
return protectionLevel+GetUpperStatement(column)+" = "+GetCamelCaseName(column.Name)+"";
        }
        
public string GetCSharpVariableType(ViewColumnSchema column)
        {
            
switch(column.DataType)
            {
                
case DbType.AnsiString: return "string";
                
case DbType.AnsiStringFixedLength: return "string";
                
case DbType.Binary: return "byte[]";
                
case DbType.Boolean: return "bool";
                
case DbType.Byte: return "int";
                
case DbType.Currency: return "decimal";
                
case DbType.Date: return "DataTime";
                
case DbType.DateTime: return "DateTime";
                
case DbType.Decimal: return "decimal";
                
case DbType.Double: return "double";
                
case DbType.Guid: return "Guid";
                
case DbType.Int16: return "short";
                
case DbType.Int32: return "int";
                
case DbType.Int64: return "long";
                
case DbType.Object: return "object";
                
case DbType.SByte: return "sbyte";
                
case DbType.Single: return "float";
                
case DbType.String: return "string";
                
case DbType.StringFixedLength: return "string";
                
case DbType.Time: return "TimeSpan";
                
case DbType.UInt16: return "ushort";
                
case DbType.UInt32: return "uint";
                
case DbType.UInt64: return "ulong";
                
case DbType.VarNumeric: return "decimal";
            }
            
            
return null;
        }
        
        
public string GetCSharpBaseType(ViewColumnSchema column)
       {
            
switch(column.DataType)
           {
                
case DbType.AnsiString: return "System.String";
                
case DbType.AnsiStringFixedLength: return "System.String";
                
case DbType.Binary: return "System.Byte[]";
                
case DbType.Boolean: return "System.Boolean";
                
case DbType.Byte: return "System.Int32";
                
case DbType.Currency: return "System.Decimal";
                
case DbType.Date: return "System.DataTime";
                
case DbType.DateTime: return "System.DataTime";
                
case DbType.Decimal: return "System.Decimal";
                
case DbType.Double: return "System.Double";
                
case DbType.Guid: return "System.Guid";
                
case DbType.Int16: return "System.Int16";
                
case DbType.Int32: return "System.Int32";
                
case DbType.Int64: return "System.Int64";
                
case DbType.Object: return "System.Object";
                
case DbType.SByte: return "System.SByte";
                
case DbType.Single: return "System.Single";
                
case DbType.String: return "System.String";
                
case DbType.StringFixedLength: return "System.String";
                
case DbType.Time: return "System.TimeSpan";
                
case DbType.UInt16: return "System.UInt16";
                
case DbType.UInt32: return "System.UInt32";
                
case DbType.UInt64: return "System.UInt64";
                
case DbType.VarNumeric: return "System.Decimal";
            }
            
return null;
        }
    }
}
 
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏