首页 > 代码库 > 分享一个几年前写的代码生成器
分享一个几年前写的代码生成器
该界面设计部分来自网络。版权归原作者所有。
该工具没有做完成。我只是学习用的。主要是为了熟悉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("/*\r\n"); 8 strsb.Append("服务信息 \r\n"); 9 strsb.AppendFormat("创建日期:{0} \r\n", DateTime.Now.ToLocalTime()); 10 strsb.Append("类描述: \r\n"); 11 strsb.Append("修改日期: \r\n"); 12 strsb.Append("版本号:V1.0 \r\n"); 13 strsb.Append("作者: \r\n"); 14 strsb.Append("*/\r\n\r\n"); 15 strsb.Append("*/\r\n\r\n"); 16 17 #region 服务契约 18 19 strsb.AppendFormat("#region --------------------- " + list[0].TableName + "{0}--------------------- \r\n \r\n", "服务契约"); 20 strsb.AppendFormat(" ///<summary>\r\n ///新增操作{0} \r\n /// </summary>\r\n [OperationContract(IsInitiating = true)] \r\n bool Insert{1}({2} model); \r\n \r\n", list[0].TableName, list[0].TableName, list[0].TableName); 21 strsb.AppendFormat(" ///<summary>\r\n ///修改操作{0} \r\n /// </summary>\r\n [OperationContract(IsInitiating = true)] \r\n bool Update{0}({1} model); \r\n \r\n", list[0].TableName, list[0].TableName); 22 strsb.AppendFormat(" ///<summary>\r\n ///删除操作{0} \r\n /// </summary>\r\n [OperationContract(IsInitiating = true)] \r\n bool Delete{0}({1} id);\r\n \r\n", list[0].TableName, DataType.ConvertSqlTypeToCSharp(list[0])); 23 strsb.AppendFormat(" ///<summary>\r\n ///获取所有数据{0} \r\n /// </summary>\r\n [OperationContract(IsInitiating = true)] \r\n List<{0}> Get{1}ListData(); \r\n \r\n", list[0].TableName, list[0].TableName); 24 strsb.Append("#endregion \r\n"); 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}--------------------- \r\n", "服务"); 40 41 #region 新增 42 strsb.AppendFormat(" ///<summary>\r\n ///新增操作{0} \r\n /// </summary>\r\n public bool Insert{1}({2} model) \r\n", list[0].TableName, list[0].TableName, list[0].TableName); 43 strsb.Append(" {\r\n"); 44 strsb.Append(" try\r\n "); 45 strsb.Append(" {\r\n "); 46 strsb.AppendFormat(" using(var entity=new {0}())\r\n ", efName); 47 strsb.Append(" { \r\n "); 48 strsb.AppendFormat(" entity.{0}.AddObject(model); \r\n ", list[0].TableName); 49 strsb.Append(" entity.SaveChanges(); \r\n "); 50 strsb.Append(" } \r\n "); 51 strsb.Append(" } \r\n "); 52 strsb.Append(" catch(EntityException) \r\n "); 53 strsb.Append(" { \r\n "); 54 strsb.Append(" return false; \r\n "); 55 strsb.Append(" } \r\n "); 56 strsb.Append(" catch(Exception ex) \r\n "); 57 strsb.Append(" { \r\n "); 58 strsb.Append(" throw ex.InnerException; false; \r\n "); 59 strsb.Append(" } \r\n "); 60 strsb.Append(" return true;\r\n "); 61 strsb.Append(" }\r\n"); 62 #endregion 63 64 #region 修改 65 strsb.AppendFormat(" ///<summary>\r\n ///修改操作{0} \r\n /// </summary>\r\n public bool Update{0}({1} model) \r\n ", list[0].TableName, list[0].TableName); 66 strsb.Append(" {\r\n"); 67 strsb.Append(" try\r\n "); 68 strsb.Append(" {\r\n "); 69 strsb.AppendFormat(" using(var entity=new {0}())\r\n ", efName); 70 strsb.Append(" { \r\n "); 71 strsb.AppendFormat(" var category=entity.{0}.FirstOrDefault(o=>o.{1}==model.{2}); \r\n ", list[0].TableName, list[0].ColumnName, list[0].ColumnName); 72 strsb.Append(" if(category!=null) \r\n "); 73 strsb.Append(" { \r\n "); 74 for (int i = 0; i < list.Count; i++) 75 { 76 strsb.AppendFormat(" category.{0}=model.{1}; \r\n", list[i].ColumnName, list[i].ColumnName); 77 } 78 strsb.Append(" } \r\n "); 79 strsb.Append(" entity.SaveChanges(); \r\n "); 80 strsb.Append(" } \r\n "); 81 strsb.Append(" } \r\n "); 82 strsb.Append(" catch(EntityException) \r\n "); 83 strsb.Append(" { \r\n "); 84 strsb.Append(" return false; \r\n "); 85 strsb.Append(" } \r\n "); 86 strsb.Append(" catch(Exception) \r\n "); 87 strsb.Append(" { \r\n "); 88 strsb.Append(" return false; \r\n "); 89 strsb.Append(" } \r\n "); 90 strsb.Append(" return true;\r\n "); 91 strsb.Append(" }\r\n"); 92 #endregion 93 94 #region 删除 95 96 strsb.AppendFormat(" ///<summary>\r\n ///删除操作{0} \r\n /// </summary>\r\n public bool Delete{0}({1} model) \r\n ", list[0].TableName, DataType.ConvertSqlTypeToCSharp(list[0])); 97 strsb.Append(" {\r\n"); 98 strsb.Append(" try\r\n "); 99 strsb.Append(" {\r\n ");100 strsb.AppendFormat(" using(var entity=new {0}())\r\n ", efName);101 strsb.Append(" { \r\n ");102 strsb.AppendFormat(" var category=entity.{0}.Where(o=>o.{1}=={2}); \r\n ", list[0].TableName, list[0].ColumnName, list[0].ColumnName);103 strsb.Append(" if(category.Any()) \r\n ");104 strsb.Append(" { \r\n ");105 strsb.Append(" foreach(var item in category) \r\n ");106 strsb.Append(" { \r\n ");107 strsb.AppendFormat(" entity.{0}.DeleteObject(item); \r\n ", list[0].TableName);108 strsb.Append(" } \r\n ");109 strsb.Append(" entity.SaveChanges(); \r\n ");110 strsb.Append(" } \r\n ");111 strsb.Append(" } \r\n ");112 strsb.Append(" } \r\n ");113 strsb.Append(" catch(EntityException) \r\n ");114 strsb.Append(" { \r\n ");115 strsb.Append(" return false; \r\n ");116 strsb.Append(" } \r\n ");117 strsb.Append(" catch(Exception ex) \r\n ");118 strsb.Append(" { \r\n ");119 strsb.Append(" throw ex.InnerException; \r\n ");120 strsb.Append(" } \r\n ");121 strsb.Append(" return true;\r\n ");122 strsb.Append(" }\r\n");123 #endregion124 125 #region 获取数据126 strsb.AppendFormat(" ///<summary>\r\n ///获取所有数据{0} \r\n /// </summary>\r\n public List<{0}> Get{1}ListData() \r\n", list[0].TableName, list[0].TableName);127 strsb.Append(" {\r\n");128 strsb.Append(" try\r\n ");129 strsb.Append(" {\r\n ");130 strsb.AppendFormat(" using(var entity=new {0}())\r\n ", efName);131 strsb.Append(" { \r\n ");132 strsb.AppendFormat(" return(from sel in entity.{0} select sel).ToList(); \r\n ", list[0].TableName);133 strsb.Append(" } \r\n ");134 strsb.Append(" } \r\n ");135 strsb.Append(" catch(EntityException exception) \r\n ");136 strsb.Append(" { \r\n ");137 strsb.Append(" throw exception.InnerException; \r\n ");138 strsb.Append(" } \r\n ");139 strsb.Append(" catch(Exception exception) \r\n ");140 strsb.Append(" { \r\n ");141 strsb.Append(" throw exception.InnerException; \r\n ");142 strsb.Append(" } \r\n ");143 strsb.Append(" }\r\n");144 #endregion145 146 147 strsb.Append("#endregion \r\n \r\n");148 149 #endregion150 151 152 }153 return strsb.ToString();154 }155 156 }
其实对于现在来说。肯定要以模板的形式去写代码生成器。
代码我将上传在百度网盘。又需要人朋友留言获取。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。