zoukankan      html  css  js  c++  java
  • CodeSmith生成SQL Server视图的实体类脚本2


    第一个文件是businessobjctforView.cst,生成C#实体类,此脚本需要引用第二个文件CommonUtility.cs。需将两个文件放在同一目录中。
    <%@CodeTemplateLanguage="C#"TargetLanguage="T-SQL"
       Description="Generatesaupdatestoredprocedure."%>
    <%@AssemblyName="SchemaExplorer"%>
    <%@ImportNamespace="SchemaExplorer"%>
    <%@PropertyName="SourceTable"Type="SchemaExplorer.ViewSchema"
       Category="Context"
       Description="Tablethatthestoredproceduresshouldbebasedon."%>
    <%@AssemblySrc="CommonUtility.cs"%>
    <%@ImportNamespace="Common.Data"%>
    <scriptrunat="template">
      CommonUtilityrule=newCommonUtility();
    </script>
    usingSystem;
    usingSystem.Collections;
    usingSystem.Collections.Generic;
    usingSystem.Data;                                                                            
    namespaceSOA.Model
    {
      ///<summary>
      ///<%=SourceTable.Name%>objectforViewtable'<%=SourceTable.Name%>'.
      ///</summary>
      [Serializable]
      publicclass<%=SourceTable.Name%>
      {
      #regionPrivateMember
        <%for(inti=0;i<SourceTable.Columns.Count;i++){%>
        private<%=rule.GetCSharpVariableType(SourceTable.Columns[i])%>_<%=SourceTable.Columns[i].Name%>;
      <%}%>
      #endregion
        
      #regionConstructor
        public<%=SourceTable.Name%>(){}    
        
        public<%=SourceTable.Name%>(
            <%for(inti=0;i<SourceTable.Columns.Count-1;i++){%>
        <%=rule.GetCSharpVariableType(SourceTable.Columns[i])%><%=SourceTable.Columns[i].Name%>,  
      <%}%>
      <%=rule.GetCSharpVariableType(SourceTable.Columns[SourceTable.Columns.Count-1])%><%=SourceTable.Columns[SourceTable.Columns.Count-1].Name%>
        )
        {
        <%for(inti=0;i<SourceTable.Columns.Count;i++){%>
        _<%=SourceTable.Columns[i].Name%>=<%=SourceTable.Columns[i].Name%>;
      <%}%>
        }
      #endregion
      
      #regionPublicProperties
      <%for(inti=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#的数据类型定义以及输出:
    usingSystem;
    usingSystem.Text;
    usingCodeSmith.Engine;
    usingSchemaExplorer;
    usingSystem.ComponentModel;
    usingSystem.Data;
    namespaceCommon.Data
    {
      /**////<summary>
      ///TemplateRule
      ///</summary>
      publicclassCommonUtility
      {
        //getColumnsinfobyTableName
        publicViewColumnSchemaCollectionGetColumnCollectionByTable(ViewSchematable)
        {
          ViewColumnSchemaCollectioncolumns=newViewColumnSchemaCollection(table.Columns);
          returncolumns;
        }
        //Getcamelcasename,suchasCustomer,
        publicstringGetCamelCaseName(stringstr)
        {
          returnstr.Substring(0,1).ToUpper()+str.Substring(1);
        }
        
       //Get,user,privateconstStringUSER_FIELD="User"
        publicstringGetMemberConstantDeclarationStatement(ColumnSchemacolumn)
        {
          returnGetMemberConstantDeclarationStatement("publicconstString",column);
        }
        
        //suchaspublicconstStringUSER_TABLE="User"
        publicstringGetTableConstantDeclarationStatement(ViewSchematable)
        {
          returnGetMemberConstantDeclarationStatement("publicconstString",table);  
        }
        //suckasUSER_TABLE
        publicstringGetUpperStatement(ViewSchematable)
        {
          return  table.Name.ToUpper()+"_TABLE";
       }
        //suckasUSER_FIELD
        publicstringGetUpperStatement(ColumnSchemacolumn)
       {
         returncolumn.Name.ToUpper()+"_FIELD";
        }
        //suchasUSER_TABLE="User"
        publicstringGetMemberConstantDeclarationStatement(stringprotectionLevel,ViewSchematable)
        {
          returnprotectionLevel+GetUpperStatement(table)+"="+GetCamelCaseName(table.Name)+"";
        }
       
        //suchasUSERID_FIELD="Userid"
        publicstringGetMemberConstantDeclarationStatement(stringprotectionLevel,ColumnSchemacolumn)
        {
          returnprotectionLevel+GetUpperStatement(column)+"="+GetCamelCaseName(column.Name)+"";
        }
        publicstringGetCSharpVariableType(ViewColumnSchemacolumn)
        {
          switch(column.DataType)
          {
            caseDbType.AnsiString:return"string";
            caseDbType.AnsiStringFixedLength:return"string";
            caseDbType.Binary:return"byte[]";
            caseDbType.Boolean:return"bool";
            caseDbType.Byte:return"int";
            caseDbType.Currency:return"decimal";
            caseDbType.Date:return"DataTime";
            caseDbType.DateTime:return"DateTime";
            caseDbType.Decimal:return"decimal";
            caseDbType.Double:return"double";
            caseDbType.Guid:return"Guid";
            caseDbType.Int16:return"short";
            caseDbType.Int32:return"int";
            caseDbType.Int64:return"long";
            caseDbType.Object:return"object";
            caseDbType.SByte:return"sbyte";
            caseDbType.Single:return"float";
            caseDbType.String:return"string";
            caseDbType.StringFixedLength:return"string";
            caseDbType.Time:return"TimeSpan";
            caseDbType.UInt16:return"ushort";
            caseDbType.UInt32:return"uint";
            caseDbType.UInt64:return"ulong";
            caseDbType.VarNumeric:return"decimal";
          }
          
          returnnull;
        }
        
        publicstringGetCSharpBaseType(ViewColumnSchemacolumn)
       {
          switch(column.DataType)
         {
            caseDbType.AnsiString:return"System.String";
            caseDbType.AnsiStringFixedLength:return"System.String";
            caseDbType.Binary:return"System.Byte[]";
            caseDbType.Boolean:return"System.Boolean";
            caseDbType.Byte:return"System.Int32";
            caseDbType.Currency:return"System.Decimal";
            caseDbType.Date:return"System.DataTime";
            caseDbType.DateTime:return"System.DataTime";
            caseDbType.Decimal:return"System.Decimal";
            caseDbType.Double:return"System.Double";
            caseDbType.Guid:return"System.Guid";
            caseDbType.Int16:return"System.Int16";
            caseDbType.Int32:return"System.Int32";
            caseDbType.Int64:return"System.Int64";
            caseDbType.Object:return"System.Object";
            caseDbType.SByte:return"System.SByte";
            caseDbType.Single:return"System.Single";
            caseDbType.String:return"System.String";
            caseDbType.StringFixedLength:return"System.String";
            caseDbType.Time:return"System.TimeSpan";
            caseDbType.UInt16:return"System.UInt16";
            caseDbType.UInt32:return"System.UInt32";
            caseDbType.UInt64:return"System.UInt64";
            caseDbType.VarNumeric:return"System.Decimal";
          }
          returnnull;
        }
      }
    }
  • 相关阅读:
    C++ Primer 学习笔记_104_特殊工具与技术 --嵌套类
    [AngularJS + Webpack] Requiring CSS & Preprocessors
    [AngularJS + Webpack] Requiring Templates
    [AngularJS + Webpack] ES6 with BabelJS
    [Flux] 3. Actions
    [RxJS] Aggregating Streams With Reduce And Scan using RxJS
    [RxJS] map vs flatMap
    [RxJS] Stream Processing With RxJS vs Array Higher-Order Functions
    [MODx] Solve cannot upload large file
    [React + webpack] hjs-webpack
  • 原文地址:https://www.cnblogs.com/luluping/p/1539184.html
Copyright © 2011-2022 走看看