• 使用codeSmit动态生成orm实体类


    • codesmith环境安装跳过(流程界面见下图)
    • 生成实体的模板代码
      <%-- 
      Name: SqlSugerEntity 
      Author: cyao
      Description: 生成SqlSuger实体的codeSmith模板
      --%>
      <%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Template description here." %>
      <%@ Assembly Name="SchemaExplorer" %>
      <%@ Import Namespace="SchemaExplorer" %>
      <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" %>
      <%@ Property Name="NameSpace" Type="String" Category="参数" Description="命名空间" Default="" Optional="True"%>
      <%@ Import Namespace="System.Text.RegularExpressions" %>
      using SqlSugar;
      using System;
       
      namespace <%=NameSpace%>
      {
          <%=GetTableDescription(SourceTable)%>
          [SugarTable("<%=GetClassName(SourceTable)%>")]
          public class <%=GetClassNameStr(GetClass(GetClassName(SourceTable)))%>
          {
              <% foreach (ColumnSchema column in SourceTable.Columns) {%>
              <% if (column.IsPrimaryKeyMember) {%>
              /// <summary>
              /// <%=GetDescription(column)%>
              /// </summary>
              [SugarColumn(ColumnName = "<%=GetColumnName(column)%>", IsPrimaryKey = true)]
              public <%=GetCSharpVariableType(column) %> <%=GetPascalName(column)%> { get; set;}
           
              <% } else {%>
              /// <summary>
              /// <%=GetDescription(column)%>
              /// </summary>
              [SugarColumn(ColumnName = "<%=GetColumnName(column)%>")]
              public <%=GetCSharpVariableType(column) %> <%=GetPascalName(column) %> { get; set;}
            
              <% }%>
              <% }%>
          }
      }
      <script runat="template">
      /// <summary>
      /// 获取字段说明
      /// </summary>
      /// <param name="column"></param>
      /// <returns></returns>
      public string GetDescription(ColumnSchema column){
          return  column.Description;
      }        
      /// <summary>
      /// 获取表格说明
      /// </summary>
      /// <param name="table"></param>
      /// <returns></returns>       
      public string GetTableDescription(TableSchema table)
      {
          return table.Description;
      }
      /// <summary>
      /// 获取列名
      /// </summary>
      /// <param name="column"></param>
      /// <returns></returns>
      public string  GetColumnName(ColumnSchema column)
      {
          return column.Name;
      }
      /// <summary>
      /// 首字母大写
      /// </summary>
      /// <param name="column"></param>
      /// <returns></returns>
      public string GetCamelName(ColumnSchema column)
      {
          return column.Name.Substring(0, 1).ToLower() + column.Name.Substring(1);
      }
      /// <summary>
      /// 首字母大写
      /// </summary>
      /// <param name="column"></param>
      /// <returns></returns>
      public string GetCamelName(string value)
      {
          return value.Substring(0, 1).ToLower() + value.Substring(1);
      }
      
      /// <summary>
      /// 获取类名 
      /// </summary>
      /// <param name="columnName"></param>
      /// <returns></returns>
       public string GetClassNameStr(string columnName)
      {
           string newColumn = "";
            string[] temp=columnName.Split('_');
                  for (int i = 0; i < temp.Length; i++)
                  {               
                     newColumn+=System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(temp[i]); //设置首字母大写
                  }   
          return newColumn;
      }
      /// <summary>
      /// 获取表明
      /// </summary>
      /// <param name="table"></param>
      /// <returns></returns>
      public string GetClassName(TableSchema table)
      {
          return table.Name;
      }   
      /// <summary>
      /// 获取列名
      /// </summary>
      /// <param name="column"></param>
      /// <returns></returns>
       public string GetPascalName(ColumnSchema column)
      {
           string columnName = column.Name;
           string newColumn = "";
            string[] temp=columnName.Split('_');
                  for (int i = 0; i < temp.Length; i++)
                  {               
                     newColumn+=System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(temp[i]); //设置首字母大写
                  }   
          return newColumn;
      }
      
      /// <summary>
      /// 去除tb_前缀首字母大写
      /// </summary>
      /// <param name="table"></param>
      /// <returns></returns>
      public string GetClass(string table){
         table= table.Replace("tb_", string.Empty);
         return  table.Substring(0,1).ToUpper()+table.Substring(1);
      }
       
      /// <summary>
      /// db类型转换
      /// </summary>
      /// <param name="column"></param>
      /// <returns></returns>
      public string GetCSharpVariableType(ColumnSchema column)
      {
          if (column.Name.EndsWith("TypeCode")) return column.Name;
          
          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 "byte";
              case DbType.Currency: return "decimal";
              case DbType.Date: return "DateTime";
              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";
              default:
              {
                  return "__UNKNOWN__" + column.NativeType;
              }
          }
      }    
      public override string GetFileName()
      {
          return GetClassName(SourceTable) + ".cs";
      }
      </script>
    • 生成Service的模板代码
      <%-- 
      Name: ServiceTemplate
      Author: cyao
      Description: 生成Service的codeSmith模板
      --%>
      <%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Template description here." %>
      <%@ Assembly Name="SchemaExplorer" %>
      <%@ Import Namespace="SchemaExplorer" %>
      <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" %>
      <%@ Property Name="NameSpace" Type="String" Category="参数" Description="命名空间" Default="" Optional="True"%>
      <%@ Import Namespace="System.Text.RegularExpressions" %>
      using BBSS.Basic.Service.DataModel;
      using BBSS.Basic.Service.Interface;
      using BBSS.DataAccess;
       
      namespace <%=NameSpace%>
      {
          public class <%=GetClassNameStr(GetClass(GetClassName(SourceTable)))%>Service : BasicDbContext< <%=GetClassNameStr(GetClass(GetClassName(SourceTable)))%>> ,I<%=GetClassNameStr(GetClass(GetClassName(SourceTable)))%>Service
          {
              public <%=GetClassNameStr(GetClass(GetClassName(SourceTable)))%>Service(DbEntranceUtil dbEntranceUtil) : base(dbEntranceUtil)
              {
      
              }
          }
      }
      <script runat="template">
      
      /// <summary>
      /// 获取类名 
      /// </summary>
      /// <param name="columnName"></param>
      /// <returns></returns>
       public string GetClassNameStr(string columnName)
      {
           string newColumn = "";
            string[] temp=columnName.Split('_');
                  for (int i = 0; i < temp.Length; i++)
                  {               
                     newColumn+=System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(temp[i]); //设置首字母大写
                  }   
          return newColumn;
      }
      /// <summary>
      /// 获取表明
      /// </summary>
      /// <param name="table"></param>
      /// <returns></returns>
      public string GetClassName(TableSchema table)
      {
          return table.Name;
      }   
      /// <summary>
      /// 获取列名
      /// </summary>
      /// <param name="column"></param>
      /// <returns></returns>
       public string GetPascalName(ColumnSchema column)
      {
           string columnName = column.Name;
           string newColumn = "";
            string[] temp=columnName.Split('_');
                  for (int i = 0; i < temp.Length; i++)
                  {               
                     newColumn+=System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(temp[i]); //设置首字母大写
                  }   
          return newColumn;
      }
      
      /// <summary>
      /// 去除tb_前缀首字母大写
      /// </summary>
      /// <param name="table"></param>
      /// <returns></returns>
      public string GetClass(string table){
         table= table.Replace("tb_", string.Empty);
         return  table.Substring(0,1).ToUpper()+table.Substring(1);
      }
       
      public override string GetFileName()
      {
          return GetClassName(SourceTable) + ".cs";
      }
      </script>
    • 生成IService的模板代码
      <%-- 
      Name: IServiceTemplate
      Author: cyao
      Description: 生成IService的codeSmith模板
      --%>
      <%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Template description here." %>
      <%@ Assembly Name="SchemaExplorer" %>
      <%@ Import Namespace="SchemaExplorer" %>
      <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" %>
      <%@ Property Name="NameSpace" Type="String" Category="参数" Description="命名空间" Default="" Optional="True"%>
      <%@ Import Namespace="System.Text.RegularExpressions" %>
      using BBSS.Basic.Service.DataModel;
      using BBSS.DataAccess; 
       
      namespace <%=NameSpace%>
      {
          public interface I<%=GetClassNameStr(GetClass(GetClassName(SourceTable)))%>Service : IBaseService< <%=GetClassNameStr(GetClass(GetClassName(SourceTable)))%>>
          {
              
          }
      }
      <script runat="template">
      
      /// <summary>
      /// 获取类名 
      /// </summary>
      /// <param name="columnName"></param>
      /// <returns></returns>
       public string GetClassNameStr(string columnName)
      {
           string newColumn = "";
            string[] temp=columnName.Split('_');
                  for (int i = 0; i < temp.Length; i++)
                  {               
                     newColumn+=System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(temp[i]); //设置首字母大写
                  }   
          return newColumn;
      }
      /// <summary>
      /// 获取表明
      /// </summary>
      /// <param name="table"></param>
      /// <returns></returns>
      public string GetClassName(TableSchema table)
      {
          return table.Name;
      }   
      /// <summary>
      /// 获取列名
      /// </summary>
      /// <param name="column"></param>
      /// <returns></returns>
       public string GetPascalName(ColumnSchema column)
      {
           string columnName = column.Name;
           string newColumn = "";
            string[] temp=columnName.Split('_');
                  for (int i = 0; i < temp.Length; i++)
                  {               
                     newColumn+=System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(temp[i]); //设置首字母大写
                  }   
          return newColumn;
      }
      
      /// <summary>
      /// 去除tb_前缀首字母大写
      /// </summary>
      /// <param name="table"></param>
      /// <returns></returns>
      public string GetClass(string table){
         table= table.Replace("tb_", string.Empty);
         return  table.Substring(0,1).ToUpper()+table.Substring(1);
      }
       
      public override string GetFileName()
      {
          return GetClassName(SourceTable) + ".cs";
      }
      </script>
    • 最终批量生成的模板代码
      <%-- 
      Name: CodeTemplate
      Author: cyao
      Description: 批量生成SqlSuger实体的codeSmith模板
      --%>
      <%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Description="生成整个表" Debug="True" ResponseEncoding="UTF-8"%>
      <%-- 注册实体层Entity模板 --%>
      <%@ Register Name="EntityTemplate" Template="SqlSugerEntity.cst" MergeProperties="Flase" ExcludeProperties=""%>
      <%@ Register Name="InterfaceServiceTemplate" Template="IServiceTemplate.cst" MergeProperties="Flase" ExcludeProperties=""%> 
      <%@ Register Name="ServiceTemplate" Template="ServiceTemplate.cst" MergeProperties="Flase" ExcludeProperties=""%> 
      <%@ Register Name="ViewModelTemplate" Template="ViewModelTemplate.cst" MergeProperties="Flase" ExcludeProperties=""%> 
      
      <%-- 获取整个数据库对象 --%>
      <%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="01. 获取数据库对象" Description="获取整个数据库对象"%>
      
      
      <%
      //创建实体层Entity类
      this.GenerateEntityClasses();
      //Debug模式下的信息[Debug="True"]
      Debug.WriteLine("Success");
      %>
      
      <script runat="template">
          //生成实体Entity类
      private void GenerateEntityClasses()
      {
              //获取模板对象
              CodeTemplate Template =new EntityTemplate();
              foreach(TableSchema table in this.SourceDatabase.Tables)
              {
                  string FileDirectory = OutputDirectory +"\DataModel\"+ GetPascalName(GetClass(table.Name))  +".cs";
                  //设置模板的相关内容(Table名称 ,作者名称)
                  Template.SetProperty("SourceTable",table);
                  Template.SetProperty("NameSpace","BBSS.Basic.Service.DataModel");
                  //文件输出
                  Template.RenderToFile(FileDirectory,true);
                  Debug.WriteLine(FileDirectory +" 创建成功.");
              }
              //获取viewmodel实体
              ViewModelTemplate ViewModelTemplate =new ViewModelTemplate();
              foreach(TableSchema table in this.SourceDatabase.Tables)
              {
                  string FileDirectory = OutputDirectory +"\ViewModel\"+ GetPascalName(GetClass(table.Name))  +"VModel.cs";
                  //设置模板的相关内容(Table名称 ,作者名称)
                  ViewModelTemplate.SetProperty("SourceTable",table);
                  ViewModelTemplate.SetProperty("NameSpace","BBSS.Basic.Service.ViewModel");
                  //文件输出
                  ViewModelTemplate.RenderToFile(FileDirectory,true);
                  Debug.WriteLine(FileDirectory +" 创建成功.");
              }
              //批量生成IService接口
              InterfaceServiceTemplate iservicetemplate=new InterfaceServiceTemplate();
              foreach(TableSchema table in this.SourceDatabase.Tables)
              {
                  string FileDirectory = OutputDirectory +"\Interface\I"+ GetPascalName(GetClass(table.Name))  +"Service.cs";
                  //设置模板的相关内容(Table名称 ,作者名称)
                  iservicetemplate.SetProperty("SourceTable",table);
                  iservicetemplate.SetProperty("NameSpace","BBSS.Basic.Service.Interface");
                  //文件输出
                  iservicetemplate.RenderToFile(FileDirectory,true);
                  Debug.WriteLine(FileDirectory +" 创建成功.");
              }
              //批量生成Service
               ServiceTemplate servicetemplate=new ServiceTemplate();
              foreach(TableSchema table in this.SourceDatabase.Tables)
              {
                  string FileDirectory = OutputDirectory +"\Impl\"+ GetPascalName(GetClass(table.Name))  +"Service.cs";
                  //设置模板的相关内容(Table名称 ,作者名称)
                  servicetemplate.SetProperty("SourceTable",table);
                  servicetemplate.SetProperty("NameSpace","BBSS.Basic.Service.Impl");
                  //文件输出
                  servicetemplate.RenderToFile(FileDirectory,true);
                  Debug.WriteLine(FileDirectory +" 创建成功.");
              }
             
      }
      
      
      public string GetClass(string table){
         table= table.Replace("tb_", string.Empty);
         return  table.Substring(0,1).ToUpper()+table.Substring(1);
      }
      
       public string GetPascalName(string columnName)
      {
           string newColumn = "";
            string[] temp=columnName.Split('_');
                  for (int i = 0; i < temp.Length; i++)
                  {               
                     newColumn+=System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(temp[i]); //设置首字母大写
                  }   
          return newColumn;
      }
      /// <summary>
      /// 获取新的TableName(首字母大写,去掉下划线)
      /// </summary>
      /// <param name="name"></param>
      /// <returns></returns>
      public string GetNewTableName(string name)
      {
            string table=name.Substring(1).ToLower();
            string tempTableName=string.Empty;
            if(table.IndexOf('_')>0)
            {          
                  string[] temp=table.Split('_');
                  for (int i = 0; i < temp.Length; i++)
                  {               
                     tempTableName+=System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(temp[i]); //设置首字母大写
                  }   
            }
            else
            {
                tempTableName=System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(table); //设置首字母大写          
            }
          return tempTableName;
      }
      
          //解决方案输出路径
          private string Directory = String.Empty;
          
          [Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] 
          [Optional, NotChecked]
          [DefaultValue("")]
          public string OutputDirectory 
          { 
              get
              {
                  return Directory;
              }
              set
              {
                  if (value.EndsWith("\")) value = value.Substring(0, value.Length -1);
                  Directory = value;
              } 
          }
      </script>

    • 实施过程中可能会遇到两个问题
  • 相关阅读:
    黑客技术训练场
    常用安全测试用例
    Burp suite 一款Web渗透测试的集成套件
    soapUI安全测试教程
    黑客视频动画
    Cookie安全测试
    安全测试资源列表【持续收集中】
    软件安全测试系列视频
    软件安全测试辅助工具列表【持续收集中】
    WEB安全问题的层次
  • 原文地址:https://www.cnblogs.com/chongyao/p/12212432.html
Copyright © 2020-2023  润新知