可以针对任意数据库,根据模板生成代码,支持插件(个人认为比较好玩的插件方式)。
  1. 1 <%TempletName%>=TEntity
  2. 2 <%FolderName%>=
  3. 3 <%FileName%>=<%ns%>.<%FixTblName%>.cs
  4. 4 <%Foreach%>=true
  5. 5 <%IgnoreColumns%>=ID
  6. 6 <%TMacro%>:<%FixTblName%>=CodeGenHelper.Test1(<%tn%>)
  7. 7 <%CMacro%>:<%FixColName%>=CodeGenHelper.Test1(<%cn%>)
  8. 8 <%Begin%>
  9. 9
  10. 10 using System;
  11. 11 using System.Collections.Generic;
  12. 12 using System.Text;
  13. 13 using System.Data;
  14. 14 using Fx.Common.Data;
  15. 15 using Fx.Common.DataAccess;
  16. 16 using Fx.Common.Rule;
  17. 17 using Fx.Common.Exceptions;
  18. 18
  19. 19 using Cy.Common.Pos.Data;
  20. 20 using Cy.Common.Pos.DataAccess;
  21. 21 using Cy.Common.Card;
  22. 22 using Cy.Common.Ticket;
  23. 23 using Fx.Common.Context;
  24. 24 using Fx.Common.Base;
  25. 25 using Cy.Common.Shift;
  26. 26
  27. 27 namespace <%ns%>
  28. 28 { 
  29. 29    [TableMapping("<%tn%>", CoIdPrefix = "<%tn%>")]
  30. 30    public partial class T<%FixTblName%> : TEntity
  31. 31    {
  32. 32        //---------------------------------------------------------------------
  33. 33        static T<%FixTblName%>()
  34. 34        {
  35. 35            Type type = typeof(T<%FixTblName%>);
  36. 36            TEntity.RegisterClass(type);
  37. 37        }       
  38. 38        //---------------------------------------------------------------------
  39. 39        public T<%FixTblName%>()
  40. 40        {
  41. 41        }
  42. 42        //---------------------------------------------------------------------
  43. 43        public T<%FixTblName%>(TContext ctx):base(ctx)
  44. 44        {
  45. 45        }
  46. 46        //---------------------------------------------------------
  47. 47        public override TDataAccess CreateDataAccess()
  48. 48        {
  49. 49            return new T<%FixTblName%>Access ();
  50. 50        }
  51. 51        //---------------------------------------------------------
  52. 52        public new T<%FixTblName%>Access DataAccess
  53. 53        {
  54. 54            get { return base.DataAccess as T<%FixTblName%>Access;}
  55. 55        }
  56. 56        //---------------------------------------------------------------
  57. 57        public override void Save()
  58. 58        {
  59. 59            if(IsDirty)
  60. 60                base.Save();
  61. 61        }
  62. 62        //---------------------------------------------------------------
  63. 63        #region Properties
  64. 64        <%cr%>       
  65. 65        //---------------------------------------------------------------------
  66. 66        protected <%clsDT%> m_<%FixColName%>;
  67. 67        [Mapping("<%cn%>")]
  68. 68        [Assign]
  69. 69        [Compare]
  70. 70        public <%clsDT%> <%FixColName%>
  71. 71        {
  72. 72            get { return m_<%FixColName%>; }
  73. 73            set { m_<%FixColName%> = value; }
  74. 74        }
  75. 75        <%ecr%>
  76. 76        //---------------------------------------------------------------------
  77. 77        #endregion
  78. 78    }
  79. 79 }
复制代码
根据上面模板生成的代码如下:
  1.   1
  2.   2 using System;
  3.   3 using System.Collections.Generic;
  4.   4 using System.Text;
  5.   5 using System.Data;
  6.   6 using Fx.Common.Data;
  7.   7 using Fx.Common.DataAccess;
  8.   8 using Fx.Common.Rule;
  9.   9 using Fx.Common.Exceptions;
  10. 10
  11. 11 using Cy.Common.Pos.Data;
  12. 12 using Cy.Common.Pos.DataAccess;
  13. 13 using Cy.Common.Card;
  14. 14 using Cy.Common.Ticket;
  15. 15 using Fx.Common.Context;
  16. 16 using Fx.Common.Base;
  17. 17 using Cy.Common.Shift;
  18. 18
  19. 19 namespace xxx
  20. 20 { 
  21. 21    [TableMapping("ADJUST", CoIdPrefix = "ADJUST")]
  22. 22    public partial class TAdjust : TEntity
  23. 23    {
  24. 24        //---------------------------------------------------------------------
  25. 25        static TAdjust()
  26. 26        {
  27. 27            Type type = typeof(TAdjust);
  28. 28            TEntity.RegisterClass(type);
  29. 29        }       
  30. 30        //---------------------------------------------------------------------
  31. 31        public TAdjust()
  32. 32        {
  33. 33        }
  34. 34        //---------------------------------------------------------------------
  35. 35        public TAdjust(TContext ctx):base(ctx)
  36. 36        {
  37. 37        }
  38. 38        //---------------------------------------------------------
  39. 39        public override TDataAccess CreateDataAccess()
  40. 40        {
  41. 41            return new TAdjustAccess ();
  42. 42        }
  43. 43        //---------------------------------------------------------
  44. 44        public new TAdjustAccess DataAccess
  45. 45        {
  46. 46            get { return base.DataAccess as TAdjustAccess;}
  47. 47        }
  48. 48        //---------------------------------------------------------------
  49. 49        public override void Save()
  50. 50        {
  51. 51            if(IsDirty)
  52. 52                base.Save();
  53. 53        }
  54. 54        //---------------------------------------------------------------
  55. 55        #region Properties
  56. 56                 
  57. 57        //---------------------------------------------------------------------
  58. 58        protected System.Int32? m_Aindex;
  59. 59        [Mapping("AINDEX")]
  60. 60        [Assign]
  61. 61        [Compare]
  62. 62        public System.Int32? Aindex
  63. 63        {
  64. 64            get { return m_Aindex; }
  65. 65            set { m_Aindex = value; }
  66. 66        }
  67. 67                 
  68. 68        //---------------------------------------------------------------------
  69. 69        protected System.String m_Title;
  70. 70        [Mapping("TITLE")]
  71. 71        [Assign]
  72. 72        [Compare]
  73. 73        public System.String Title
  74. 74        {
  75. 75            get { return m_Title; }
  76. 76            set { m_Title = value; }
  77. 77        }
  78. 78                 
  79. 79        //---------------------------------------------------------------------
  80. 80        protected System.Int32? m_BillTypeNo;
  81. 81        [Mapping("BILL_TYPE_NO")]
  82. 82        [Assign]
  83. 83        [Compare]
  84. 84        public System.Int32? BillTypeNo
  85. 85        {
  86. 86            get { return m_BillTypeNo; }
  87. 87            set { m_BillTypeNo = value; }
  88. 88        }
  89. 89                 
  90. 90        //---------------------------------------------------------------------
  91. 91        protected System.String m_BillCode;
  92. 92        [Mapping("BILL_CODE")]
  93. 93        [Assign]
  94. 94        [Compare]
  95. 95        public System.String BillCode
  96. 96        {
  97. 97            get { return m_BillCode; }
  98. 98            set { m_BillCode = value; }
  99. 99        }
  100. 100                 
  101. 101        //---------------------------------------------------------------------
  102. 102        protected System.String m_StoreId;
  103. 103        [Mapping("STORE_ID")]
  104. 104        [Assign]
  105. 105        [Compare]
  106. 106        public System.String StoreId
  107. 107        {
  108. 108            get { return m_StoreId; }
  109. 109            set { m_StoreId = value; }
  110. 110        }
  111. 111                 
  112. 112        //---------------------------------------------------------------------
  113. 113        protected System.DateTime? m_AdjustDate;
  114. 114        [Mapping("ADJUST_DATE")]
  115. 115        [Assign]
  116. 116        [Compare]
  117. 117        public System.DateTime? AdjustDate
  118. 118        {
  119. 119            get { return m_AdjustDate; }
  120. 120            set { m_AdjustDate = value; }
  121. 121        }
  122. 122                 
  123. 123        //---------------------------------------------------------------------
  124. 124        protected System.Int32? m_StateNo;
  125. 125        [Mapping("STATE_NO")]
  126. 126        [Assign]
  127. 127        [Compare]
  128. 128        public System.Int32? StateNo
  129. 129        {
  130. 130            get { return m_StateNo; }
  131. 131            set { m_StateNo = value; }
  132. 132        }
  133. 133                 
  134. 134        //---------------------------------------------------------------------
  135. 135        protected System.Int32? m_AdjustDbTarget;
  136. 136        [Mapping("ADJUST_DB_TARGET")]
  137. 137        [Assign]
  138. 138        [Compare]
  139. 139        public System.Int32? AdjustDbTarget
  140. 140        {
  141. 141            get { return m_AdjustDbTarget; }
  142. 142            set { m_AdjustDbTarget = value; }
  143. 143        }
  144. 144                 
  145. 145        //---------------------------------------------------------------------
  146. 146        protected System.String m_Amemo;
  147. 147        [Mapping("AMEMO")]
  148. 148        [Assign]
  149. 149        [Compare]
  150. 150        public System.String Amemo
  151. 151        {
  152. 152            get { return m_Amemo; }
  153. 153            set { m_Amemo = value; }
  154. 154        }
  155. 155                 
  156. 156        //---------------------------------------------------------------------
  157. 157        protected System.Int32? m_SychorStateNo;
  158. 158        [Mapping("SYCHOR_STATE_NO")]
  159. 159        [Assign]
  160. 160        [Compare]
  161. 161        public System.Int32? SychorStateNo
  162. 162        {
  163. 163            get { return m_SychorStateNo; }
  164. 164            set { m_SychorStateNo = value; }
  165. 165        }
  166. 166                 
  167. 167        //---------------------------------------------------------------------
  168. 168        protected System.String m_BillorId;
  169. 169        [Mapping("BILLOR_ID")]
  170. 170        [Assign]
  171. 171        [Compare]
  172. 172        public System.String BillorId
  173. 173        {
  174. 174            get { return m_BillorId; }
  175. 175            set { m_BillorId = value; }
  176. 176        }
  177. 177                 
  178. 178        //---------------------------------------------------------------------
  179. 179        protected System.DateTime? m_BillTime;
  180. 180        [Mapping("BILL_TIME")]
  181. 181        [Assign]
  182. 182        [Compare]
  183. 183        public System.DateTime? BillTime
  184. 184        {
  185. 185            get { return m_BillTime; }
  186. 186            set { m_BillTime = value; }
  187. 187        }
  188. 188                 
  189. 189        //---------------------------------------------------------------------
  190. 190        protected System.String m_CheckerId;
  191. 191        [Mapping("CHECKER_ID")]
  192. 192        [Assign]
  193. 193        [Compare]
  194. 194        public System.String CheckerId
  195. 195        {
  196. 196            get { return m_CheckerId; }
  197. 197            set { m_CheckerId = value; }
  198. 198        }
  199. 199                 
  200. 200        //---------------------------------------------------------------------
  201. 201        protected System.DateTime? m_CheckTime;
  202. 202        [Mapping("CHECK_TIME")]
  203. 203        [Assign]
  204. 204        [Compare]
  205. 205        public System.DateTime? CheckTime
  206. 206        {
  207. 207            get { return m_CheckTime; }
  208. 208            set { m_CheckTime = value; }
  209. 209        }
  210. 210                 
  211. 211        //---------------------------------------------------------------------
  212. 212        protected System.String m_CreatorId;
  213. 213        [Mapping("CREATOR_ID")]
  214. 214        [Assign]
  215. 215        [Compare]
  216. 216        public System.String CreatorId
  217. 217        {
  218. 218            get { return m_CreatorId; }
  219. 219            set { m_CreatorId = value; }
  220. 220        }
  221. 221                 
  222. 222        //---------------------------------------------------------------------
  223. 223        protected System.DateTime? m_CreateTime;
  224. 224        [Mapping("CREATE_TIME")]
  225. 225        [Assign]
  226. 226        [Compare]
  227. 227        public System.DateTime? CreateTime
  228. 228        {
  229. 229            get { return m_CreateTime; }
  230. 230            set { m_CreateTime = value; }
  231. 231        }
  232. 232                 
  233. 233        //---------------------------------------------------------------------
  234. 234        protected System.String m_ModifierId;
  235. 235        [Mapping("MODIFIER_ID")]
  236. 236        [Assign]
  237. 237        [Compare]
  238. 238        public System.String ModifierId
  239. 239        {
  240. 240            get { return m_ModifierId; }
  241. 241            set { m_ModifierId = value; }
  242. 242        }
  243. 243                 
  244. 244        //---------------------------------------------------------------------
  245. 245        protected System.DateTime? m_ModifyTime;
  246. 246        [Mapping("MODIFY_TIME")]
  247. 247        [Assign]
  248. 248        [Compare]
  249. 249        public System.DateTime? ModifyTime
  250. 250        {
  251. 251            get { return m_ModifyTime; }
  252. 252            set { m_ModifyTime = value; }
  253. 253        }
  254. 254                 
  255. 255        //---------------------------------------------------------------------
  256. 256        protected System.String m_UnCheckerId;
  257. 257        [Mapping("UN_CHECKER_ID")]
  258. 258        [Assign]
  259. 259        [Compare]
  260. 260        public System.String UnCheckerId
  261. 261        {
  262. 262            get { return m_UnCheckerId; }
  263. 263            set { m_UnCheckerId = value; }
  264. 264        }
  265. 265                 
  266. 266        //---------------------------------------------------------------------
  267. 267        protected System.DateTime? m_UnCheckTime;
  268. 268        [Mapping("UN_CHECK_TIME")]
  269. 269        [Assign]
  270. 270        [Compare]
  271. 271        public System.DateTime? UnCheckTime
  272. 272        {
  273. 273            get { return m_UnCheckTime; }
  274. 274            set { m_UnCheckTime = value; }
  275. 275        }
  276. 276         
  277. 277        //---------------------------------------------------------------------
  278. 278        #endregion
  279. 279    }
  280. 280 }
复制代码
生成这个代码片段的模板是按照目前正在进行的项目的代码风格所定一的,可能有些人会不习惯。


目前可支持ADO.net所支持的所有数据库。选择数据库后,选择需要生成代码的表和模板即可。模板的定义也比较简单、直观,不需要复杂的学习过程,关键是在适当的地方加入表和列的循环标记。标记的语法请参考帮助文件。


在Demo的rar包里有模板和生成后的代码,同时Demo里还有实现插件的例子代码。 由于是Demo就不提供生成器源代码了。欢迎大家多提意见,我的目标是实现一个方便、快捷但功能强大的代码生成器。后续会加入工程管理和for循环标记的支持,其他的想法也将慢慢加入其中。

Demo下载:
 附件: 您所在的用户组无法下载或查看附件



文/陈鹏(偶是坏人)
赞助商广告:

TOP