• 分享一个几年前写的代码生成器


    该界面设计部分来自网络。版权归原作者所有。

    该工具没有做完成。我只是学习用的。主要是为了熟悉Devexress控件的使用。

    1.系统主界面

    2.系统代码生成

    3.生成的详细代码查看

     

    4.该工具写的不是很智能. 如下生成WCF服务契约代码:

      1  public class CreateWcfService
      2     {
      3 
      4         public static string GreateWcFserviceContract(List<Model> list)
      5         {
      6             var strsb = new StringBuilder();
      7             strsb.Append("/*
    ");
      8             strsb.Append("服务信息 
    ");
      9             strsb.AppendFormat("创建日期:{0} 
    ", DateTime.Now.ToLocalTime());
     10             strsb.Append("类描述: 
    ");
     11             strsb.Append("修改日期: 
    ");
     12             strsb.Append("版本号:V1.0 
    ");
     13             strsb.Append("作者: 
    ");
     14             strsb.Append("*/
    
    ");
     15             strsb.Append("*/
    
    ");
     16 
     17             #region 服务契约
     18 
     19             strsb.AppendFormat("#region --------------------- " + list[0].TableName + "{0}--------------------- 
     
    ", "服务契约");
     20             strsb.AppendFormat(" ///<summary>
     ///新增操作{0} 
     /// </summary>
     [OperationContract(IsInitiating = true)] 
      bool Insert{1}({2} model); 
     
    ", list[0].TableName, list[0].TableName, list[0].TableName);
     21             strsb.AppendFormat(" ///<summary>
     ///修改操作{0} 
     /// </summary>
     [OperationContract(IsInitiating = true)] 
      bool Update{0}({1} model); 
     
    ", list[0].TableName, list[0].TableName);
     22             strsb.AppendFormat(" ///<summary>
     ///删除操作{0} 
     /// </summary>
     [OperationContract(IsInitiating = true)] 
      bool Delete{0}({1} id);
     
    ", list[0].TableName, DataType.ConvertSqlTypeToCSharp(list[0]));
     23             strsb.AppendFormat(" ///<summary>
     ///获取所有数据{0} 
     /// </summary>
     [OperationContract(IsInitiating = true)] 
      List<{0}> Get{1}ListData(); 
     
    ", list[0].TableName, list[0].TableName);
     24             strsb.Append("#endregion 
    ");
     25 
     26             #endregion
     27             return strsb.ToString();
     28         }
     29 
     30         public static string CreateWcfServiceInfo(List<Model> list, string efName)
     31         {
     32             var strsb = new StringBuilder();
     33         
     34             if (list.Count > 0)
     35             {
     36 
     37                 #region 服务
     38 
     39                 strsb.AppendFormat("#region --------------------- {0}--------------------- 
    ", "服务");
     40 
     41                 #region 新增
     42                 strsb.AppendFormat(" ///<summary>
     ///新增操作{0} 
     /// </summary>
     public bool Insert{1}({2} model) 
    ", list[0].TableName, list[0].TableName, list[0].TableName);
     43                 strsb.Append("   {
    ");
     44                 strsb.Append("         try
       ");
     45                 strsb.Append("         {
     ");
     46                 strsb.AppendFormat("             using(var entity=new {0}())
     ", efName);
     47                 strsb.Append("             { 
     ");
     48                 strsb.AppendFormat("                   entity.{0}.AddObject(model); 
     ", list[0].TableName);
     49                 strsb.Append("                   entity.SaveChanges(); 
     ");
     50                 strsb.Append("             } 
     ");
     51                 strsb.Append("         } 
     ");
     52                 strsb.Append("         catch(EntityException) 
     ");
     53                 strsb.Append("         { 
     ");
     54                 strsb.Append("            return false; 
     ");
     55                 strsb.Append("         } 
     ");
     56                 strsb.Append("         catch(Exception ex) 
     ");
     57                 strsb.Append("         { 
     ");
     58                 strsb.Append("            throw ex.InnerException; false; 
     ");
     59                 strsb.Append("         } 
     ");
     60                 strsb.Append("       return true;
      ");
     61                 strsb.Append("   }
    ");
     62                 #endregion
     63 
     64                 #region 修改
     65                 strsb.AppendFormat(" ///<summary>
     ///修改操作{0} 
     /// </summary>
     public bool Update{0}({1} model) 
     ", list[0].TableName, list[0].TableName);
     66                 strsb.Append("   {
    ");
     67                 strsb.Append("         try
       ");
     68                 strsb.Append("         {
     ");
     69                 strsb.AppendFormat("             using(var entity=new {0}())
     ", efName);
     70                 strsb.Append("             { 
     ");
     71                 strsb.AppendFormat("                  var category=entity.{0}.FirstOrDefault(o=>o.{1}==model.{2}); 
     ", list[0].TableName, list[0].ColumnName, list[0].ColumnName);
     72                 strsb.Append("                   if(category!=null) 
     ");
     73                 strsb.Append("                      { 
     ");
     74                 for (int i = 0; i < list.Count; i++)
     75                 {
     76                     strsb.AppendFormat("                        category.{0}=model.{1}; 
    ", list[i].ColumnName, list[i].ColumnName);
     77                 }
     78                 strsb.Append("                      } 
     ");
     79                 strsb.Append("                     entity.SaveChanges(); 
     ");
     80                 strsb.Append("             } 
     ");
     81                 strsb.Append("         } 
     ");
     82                 strsb.Append("         catch(EntityException) 
     ");
     83                 strsb.Append("         { 
     ");
     84                 strsb.Append("            return false; 
     ");
     85                 strsb.Append("         } 
     ");
     86                 strsb.Append("         catch(Exception) 
     ");
     87                 strsb.Append("         { 
     ");
     88                 strsb.Append("            return false; 
     ");
     89                 strsb.Append("         } 
     ");
     90                 strsb.Append("       return true;
      ");
     91                 strsb.Append("   }
    ");
     92                 #endregion
     93 
     94                 #region 删除
     95 
     96                 strsb.AppendFormat(" ///<summary>
     ///删除操作{0} 
     /// </summary>
     public bool Delete{0}({1} model) 
     ", list[0].TableName, DataType.ConvertSqlTypeToCSharp(list[0]));
     97                 strsb.Append("   {
    ");
     98                 strsb.Append("         try
       ");
     99                 strsb.Append("         {
     ");
    100                 strsb.AppendFormat("             using(var entity=new {0}())
     ", efName);
    101                 strsb.Append("             { 
     ");
    102                 strsb.AppendFormat("                  var category=entity.{0}.Where(o=>o.{1}=={2}); 
     ", list[0].TableName, list[0].ColumnName, list[0].ColumnName);
    103                 strsb.Append("                   if(category.Any()) 
     ");
    104                 strsb.Append("                      { 
     ");
    105                 strsb.Append("                        foreach(var item in category) 
     ");
    106                 strsb.Append("                           { 
     ");
    107                 strsb.AppendFormat("                               entity.{0}.DeleteObject(item); 
     ", list[0].TableName);
    108                 strsb.Append("                           } 
     ");
    109                 strsb.Append("                         entity.SaveChanges(); 
     ");
    110                 strsb.Append("                      } 
     ");
    111                 strsb.Append("             } 
     ");
    112                 strsb.Append("         } 
     ");
    113                 strsb.Append("         catch(EntityException) 
     ");
    114                 strsb.Append("         { 
     ");
    115                 strsb.Append("            return false; 
     ");
    116                 strsb.Append("         } 
     ");
    117                 strsb.Append("         catch(Exception ex) 
     ");
    118                 strsb.Append("         { 
     ");
    119                 strsb.Append("            throw ex.InnerException; 
     ");
    120                 strsb.Append("         } 
     ");
    121                 strsb.Append("       return true;
      ");
    122                 strsb.Append("   }
    ");
    123                 #endregion
    124 
    125                 #region 获取数据
    126                 strsb.AppendFormat(" ///<summary>
     ///获取所有数据{0} 
     /// </summary>
     public List<{0}> Get{1}ListData() 
    ", list[0].TableName, list[0].TableName);
    127                 strsb.Append("   {
    ");
    128                 strsb.Append("      try
       ");
    129                 strsb.Append("        {
     ");
    130                 strsb.AppendFormat("             using(var entity=new {0}())
     ", efName);
    131                 strsb.Append("             { 
     ");
    132                 strsb.AppendFormat("                return(from sel in entity.{0} select sel).ToList(); 
     ", list[0].TableName);
    133                 strsb.Append("             } 
     ");
    134                 strsb.Append("         } 
     ");
    135                 strsb.Append("         catch(EntityException exception) 
     ");
    136                 strsb.Append("         { 
     ");
    137                 strsb.Append("            throw exception.InnerException; 
     ");
    138                 strsb.Append("         } 
     ");
    139                 strsb.Append("         catch(Exception exception) 
     ");
    140                 strsb.Append("         { 
     ");
    141                 strsb.Append("           throw exception.InnerException; 
     ");
    142                 strsb.Append("         } 
     ");
    143                 strsb.Append("    }
    ");
    144                 #endregion
    145 
    146 
    147                 strsb.Append("#endregion 
     
    ");
    148 
    149                 #endregion
    150 
    151      
    152             }
    153             return strsb.ToString();
    154         }
    155 
    156     }

    其实对于现在来说。肯定要以模板的形式去写代码生成器。

    源代码下载:http://download.csdn.net/detail/ozaishuiyifang12/8848099

  • 相关阅读:
    php函数名后冒号(:)+数据类型(返回值类型限制/php新特性)
    qBittorrent 任务数
    TDDFT软件 octopus 编译
    visual studio code, latex workshop, setting.json
    Tex插入图片/插入tikz流程图
    记录一下讨厌的东西,就当黑名单了
    安装mingw-w64
    win7 和 win10窗口的小区别
    lattice, reciprocal lattice, OUTCAR
    v_sim 个人用户编译 无root权限
  • 原文地址:https://www.cnblogs.com/w2011/p/3889451.html
Copyright © 2020-2023  润新知