入行IT十年了,这是本人第一次网上’献丑‘。迫于工作压力,花了大半年时间写了这个生成器以辅助开发。如有价值请多多给予建议,谢谢

好了,废话少说,开动!

QA.

1.为什么要用代码生成器?

  当然是为了快速开发,且保证代码的一致性,便于维护。

2.代码生成器没缺点吗?

  当然有缺点了,就是它不能代替人,如果鼠标一按就生成整个系统!那我们程序员是在一边喝咖啡还是背包走人?幸好代码生成器不会那么智能..

3.代码生成器生成的代码是一锤子买卖?

  可以不是一锤子买卖,反复修改生成器的元数据,可以持久享受代码生成器的好处。

4.文中这个代码生成器有什么特点?

  1)理论上如果熟悉这个代码生成器的原理,任何人可以定制出自己希望的常用模式代码(为什么叫模式,指个人或团队实现功能的代码编写模式/习惯性代码等等),

目前生成的代码是针对的.NET平台下的ASP.NET MVC3+ EF+WCF+Web代码。

  2)非侵入性,代码可以完全定制以适应当前架构需要,而不是架构适应生成器。

  3)难度系数比较高,需要掌握antlr 文法分析+基础的java控制台编程知识+winform.net.但回报可能会远远大于付出。

5.这个工具,有人在实际中用吗?

  作者和所在开发团队在使用。效果非常好,大大的减少了开发成本和测试维护成本。

6.开源吗?

  计划中,由于个人工作太忙,等有空闲时间进行重构整理,以免仓促而误人子弟。

0.概要介绍

设计阶段:在文本编辑器里面编辑xml模型和数据模型元数据,工具会自动校验和分析,并生成相应代码和sql。

持久阶段:1. 设计阶段生成表后,工具从数据库获取最新表/视图,可以生成相应的增删改查功能也页面等等。

       2. 支持自定义参数化查询(语法采用标准sql,支持单表/多表/高级查询),自数据层到服务层,web层的生成。

     3. 反向工程:支持数据库表到设计阶段的数据模型元数据的同步,使设计永不过时并复用。

       4. 支持生成数据词典。

生成器窗口:

红色区域可以望文生义,不用赘述。

双击错误列表可以打开相应文件并使光标定位到错误点

1.设计阶段

  包括对xml和数据库设计的设计。

  

  1.1 Xml的Dom代码生成

  1.1.1 xml语法规则如下:

  

1.1.2 生成dom操作代码:

1.1.3 生成的代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using CodeHelper.Xml.Extension;
  7. using CodeHelper.Xml;
  8.  
  9. namespace CodeHelper.Workflow.Designer.Core.Config
  10. {
  11.  
  12. public class WorkflowDefine
  13. : DataNode
  14. {
  15. public WorkflowDefine
  16. ()
  17. : base()
  18. {
  19. }
  20.  
  21. public WorkflowDefine
  22. (XmlNode dom)
  23. : base(dom)
  24. {
  25. }
  26.  
  27. public WorkflowDefine
  28. (Document document)
  29. : base(document)
  30. {
  31. }
  32.  
  33. public override string XML_TAG_NAME
  34. {
  35. get
  36. {
  37. return "WorkflowDefine";
  38. }
  39. set
  40. {
  41. throw new Exception("cannot set");
  42. }
  43. }
  44.  
  45. public event ProperyChanged<string> OnId_ProperyChanged;
  46. public override string Id
  47. {
  48. get
  49. {
  50. if (this.Dom.Attributes["Id"] == null)
  51. return default(string);
  52. return this.Dom.Attributes["Id"].Value.ToT<string>();
  53. }
  54. set
  55. {
  56. var attr = this.Dom.Attributes.OfType<XmlAttribute>()
  57. .FirstOrDefault(x => x.Name == "Id");
  58. var oldValue = default(string);
  59. var newValue = value;
  60. if (attr == null)
  61. {
  62. attr = this.Dom.Attributes.Append(this.Dom.OwnerDocument.CreateAttribute("Id"));
  63. }
  64. else
  65. {
  66. oldValue = this.Dom.Attributes["Id"].Value.ToT<string>();
  67. }
  68.  
  69. if (value != null)
  70. attr.Value = value.ToString();
  71.  
  72. if (OnId_ProperyChanged != null && oldValue != newValue)
  73. {
  74. OnId_ProperyChanged(oldValue, newValue);
  75. }
  76. this.FireAnyProperyChanged("Id", oldValue, newValue);
  77. }
  78. }
  79. public event ProperyChanged<string> OnName_ProperyChanged;
  80. public string Name
  81. {
  82. get
  83. {
  84. if (this.Dom.Attributes["Name"] == null)
  85. return default(string);
  86. return this.Dom.Attributes["Name"].Value.ToT<string>();
  87. }
  88. set
  89. {
  90. var attr = this.Dom.Attributes.OfType<XmlAttribute>()
  91. .FirstOrDefault(x => x.Name == "Name");
  92. var oldValue = default(string);
  93. var newValue = value;
  94. if (attr == null)
  95. {
  96. attr = this.Dom.Attributes.Append(this.Dom.OwnerDocument.CreateAttribute("Name"));
  97. }
  98. else
  99. {
  100. oldValue = this.Dom.Attributes["Name"].Value.ToT<string>();
  101. }
  102.  
  103. if (value != null)
  104. attr.Value = value.ToString();
  105.  
  106. if (OnName_ProperyChanged != null && oldValue != newValue)
  107. {
  108. OnName_ProperyChanged(oldValue, newValue);
  109. }
  110. this.FireAnyProperyChanged("Name", oldValue, newValue);
  111. }
  112. }
  113. public event ProperyChanged<string> OnDescription_ProperyChanged;
  114. public string Description
  115. {
  116. get
  117. {
  118. if (this.Dom.Attributes["Description"] == null)
  119. return default(string);
  120. return this.Dom.Attributes["Description"].Value.ToT<string>();
  121. }
  122. set
  123. {
  124. var attr = this.Dom.Attributes.OfType<XmlAttribute>()
  125. .FirstOrDefault(x => x.Name == "Description");
  126. var oldValue = default(string);
  127. var newValue = value;
  128. if (attr == null)
  129. {
  130. attr = this.Dom.Attributes.Append(this.Dom.OwnerDocument.CreateAttribute("Description"));
  131. }
  132. else
  133. {
  134. oldValue = this.Dom.Attributes["Description"].Value.ToT<string>();
  135. }
  136.  
  137. if (value != null)
  138. attr.Value = value.ToString();
  139.  
  140. if (OnDescription_ProperyChanged != null && oldValue != newValue)
  141. {
  142. OnDescription_ProperyChanged(oldValue, newValue);
  143. }
  144. this.FireAnyProperyChanged("Description", oldValue, newValue);
  145. }
  146. }
  147. public event ProperyChanged<string> OnVariable_ProperyChanged;
  148. public string Variable
  149. {
  150. get
  151. {
  152. if (this.Dom.Attributes["Variable"] == null)
  153. return default(string);
  154. return this.Dom.Attributes["Variable"].Value.ToT<string>();
  155. }
  156. set
  157. {
  158. var attr = this.Dom.Attributes.OfType<XmlAttribute>()
  159. .FirstOrDefault(x => x.Name == "Variable");
  160. var oldValue = default(string);
  161. var newValue = value;
  162. if (attr == null)
  163. {
  164. attr = this.Dom.Attributes.Append(this.Dom.OwnerDocument.CreateAttribute("Variable"));
  165. }
  166. else
  167. {
  168. oldValue = this.Dom.Attributes["Variable"].Value.ToT<string>();
  169. }
  170.  
  171. if (value != null)
  172. attr.Value = value.ToString();
  173.  
  174. if (OnVariable_ProperyChanged != null && oldValue != newValue)
  175. {
  176. OnVariable_ProperyChanged(oldValue, newValue);
  177. }
  178. this.FireAnyProperyChanged("Variable", oldValue, newValue);
  179. }
  180. }
  181.  
  182. public FieldDef CreateFieldDef()
  183. {
  184. return this.Dom.CreateNode<FieldDef>();
  185. }
  186.  
  187. public NodeList<FieldDef> Fields
  188. {
  189. get
  190. {
  191. NodeList<FieldDef> fields = null;
  192. XmlNode fields_node = null;
  193.  
  194. foreach (XmlNode node in this.Dom.ChildNodes)
  195. {
  196. if (node.Name == "Fields")
  197. {
  198. fields_node = node;
  199. fields = new NodeList<FieldDef>(node);
  200. break;
  201. }
  202. }
  203.  
  204. if (fields_node != null)
  205. {
  206. foreach (XmlNode node in fields_node.ChildNodes)
  207. {
  208. fields.AddChild(new FieldDef(node));
  209. }
  210. }
  211. else
  212. {
  213. fields = this.Dom.CreateNode<NodeList<FieldDef>>("Fields");
  214.  
  215. this.AddChild(fields);
  216. }
  217. return fields;
  218. }
  219. set
  220. {
  221. throw new Exception("cannot set");
  222. }
  223. }
  224. public ClassDef CreateClassDef()
  225. {
  226. return this.Dom.CreateNode<ClassDef>();
  227. }
  228.  
  229. public NodeList<ClassDef> Classes
  230. {
  231. get
  232. {
  233. NodeList<ClassDef> classes = null;
  234. XmlNode classes_node = null;
  235.  
  236. foreach (XmlNode node in this.Dom.ChildNodes)
  237. {
  238. if (node.Name == "Classes")
  239. {
  240. classes_node = node;
  241. classes = new NodeList<ClassDef>(node);
  242. break;
  243. }
  244. }
  245.  
  246. if (classes_node != null)
  247. {
  248. foreach (XmlNode node in classes_node.ChildNodes)
  249. {
  250. classes.AddChild(new ClassDef(node));
  251. }
  252. }
  253. else
  254. {
  255. classes = this.Dom.CreateNode<NodeList<ClassDef>>("Classes");
  256.  
  257. this.AddChild(classes);
  258. }
  259. return classes;
  260. }
  261. set
  262. {
  263. throw new Exception("cannot set");
  264. }
  265. }
  266. public InitDef CreateInitDef()
  267. {
  268. return this.Dom.CreateNode<InitDef>();
  269. }
  270.  
  271. public InitDef Init
  272. {
  273. get
  274. {
  275. foreach (XmlNode node in this.Dom.ChildNodes)
  276. {
  277. if (node.Name == "InitDef" &&
  278. node.Attributes["variable"] != null &&
  279. node.Attributes["variable"].Value == "Init")
  280. {
  281. return new InitDef(node);
  282. }
  283. }
  284. return null;
  285. }
  286. set
  287. {
  288. if (this.Init != null)
  289. {
  290. this.Init.RemoveSelf();
  291. }
  292.  
  293. var attr = this.Dom.OwnerDocument.CreateAttribute("variable");
  294. attr.Value = "Init";
  295. value.Dom.Attributes.Append(attr);
  296.  
  297. this.Dom.AppendChild(value.Dom);
  298. }
  299. }
  300. public StartStateDef CreateStartStateDef()
  301. {
  302. return this.Dom.CreateNode<StartStateDef>();
  303. }
  304.  
  305. public StartStateDef StartState
  306. {
  307. get
  308. {
  309. foreach (XmlNode node in this.Dom.ChildNodes)
  310. {
  311. if (node.Name == "StartStateDef" &&
  312. node.Attributes["variable"] != null &&
  313. node.Attributes["variable"].Value == "StartState")
  314. {
  315. return new StartStateDef(node);
  316. }
  317. }
  318. return null;
  319. }
  320. set
  321. {
  322. if (this.StartState != null)
  323. {
  324. this.StartState.RemoveSelf();
  325. }
  326.  
  327. var attr = this.Dom.OwnerDocument.CreateAttribute("variable");
  328. attr.Value = "StartState";
  329. value.Dom.Attributes.Append(attr);
  330.  
  331. this.Dom.AppendChild(value.Dom);
  332. }
  333. }
  334. public TerminateStateDef CreateTerminateStateDef()
  335. {
  336. return this.Dom.CreateNode<TerminateStateDef>();
  337. }
  338.  
  339. public TerminateStateDef TerminateState
  340. {
  341. get
  342. {
  343. foreach (XmlNode node in this.Dom.ChildNodes)
  344. {
  345. if (node.Name == "TerminateStateDef" &&
  346. node.Attributes["variable"] != null &&
  347. node.Attributes["variable"].Value == "TerminateState")
  348. {
  349. return new TerminateStateDef(node);
  350. }
  351. }
  352. return null;
  353. }
  354. set
  355. {
  356. if (this.TerminateState != null)
  357. {
  358. this.TerminateState.RemoveSelf();
  359. }
  360.  
  361. var attr = this.Dom.OwnerDocument.CreateAttribute("variable");
  362. attr.Value = "TerminateState";
  363. value.Dom.Attributes.Append(attr);
  364.  
  365. this.Dom.AppendChild(value.Dom);
  366. }
  367. }
  368. public StateDef CreateStateDef()
  369. {
  370. return this.Dom.CreateNode<StateDef>();
  371. }
  372.  
  373. public NodeList<StateDef> States
  374. {
  375. get
  376. {
  377. NodeList<StateDef> states = null;
  378. XmlNode states_node = null;
  379.  
  380. foreach (XmlNode node in this.Dom.ChildNodes)
  381. {
  382. if (node.Name == "States")
  383. {
  384. states_node = node;
  385. states = new NodeList<StateDef>(node);
  386. break;
  387. }
  388. }
  389.  
  390. if (states_node != null)
  391. {
  392. foreach (XmlNode node in states_node.ChildNodes)
  393. {
  394. states.AddChild(new StateDef(node));
  395. }
  396. }
  397. else
  398. {
  399. states = this.Dom.CreateNode<NodeList<StateDef>>("States");
  400.  
  401. this.AddChild(states);
  402. }
  403. return states;
  404. }
  405. set
  406. {
  407. throw new Exception("cannot set");
  408. }
  409. }
  410. public ParallelDef CreateParallelDef()
  411. {
  412. return this.Dom.CreateNode<ParallelDef>();
  413. }
  414.  
  415. public NodeList<ParallelDef> Parallels
  416. {
  417. get
  418. {
  419. NodeList<ParallelDef> parallels = null;
  420. XmlNode parallels_node = null;
  421.  
  422. foreach (XmlNode node in this.Dom.ChildNodes)
  423. {
  424. if (node.Name == "Parallels")
  425. {
  426. parallels_node = node;
  427. parallels = new NodeList<ParallelDef>(node);
  428. break;
  429. }
  430. }
  431.  
  432. if (parallels_node != null)
  433. {
  434. foreach (XmlNode node in parallels_node.ChildNodes)
  435. {
  436. parallels.AddChild(new ParallelDef(node));
  437. }
  438. }
  439. else
  440. {
  441. parallels = this.Dom.CreateNode<NodeList<ParallelDef>>("Parallels");
  442.  
  443. this.AddChild(parallels);
  444. }
  445. return parallels;
  446. }
  447. set
  448. {
  449. throw new Exception("cannot set");
  450. }
  451. }
  452. public SubFlowStateDef CreateSubFlowStateDef()
  453. {
  454. return this.Dom.CreateNode<SubFlowStateDef>();
  455. }
  456.  
  457. public NodeList<SubFlowStateDef> SubFlowStates
  458. {
  459. get
  460. {
  461. NodeList<SubFlowStateDef> subFlowStates = null;
  462. XmlNode subFlowStates_node = null;
  463.  
  464. foreach (XmlNode node in this.Dom.ChildNodes)
  465. {
  466. if (node.Name == "SubFlowStates")
  467. {
  468. subFlowStates_node = node;
  469. subFlowStates = new NodeList<SubFlowStateDef>(node);
  470. break;
  471. }
  472. }
  473.  
  474. if (subFlowStates_node != null)
  475. {
  476. foreach (XmlNode node in subFlowStates_node.ChildNodes)
  477. {
  478. subFlowStates.AddChild(new SubFlowStateDef(node));
  479. }
  480. }
  481. else
  482. {
  483. subFlowStates = this.Dom.CreateNode<NodeList<SubFlowStateDef>>("SubFlowStates");
  484.  
  485. this.AddChild(subFlowStates);
  486. }
  487. return subFlowStates;
  488. }
  489. set
  490. {
  491. throw new Exception("cannot set");
  492. }
  493. }
  494. }
  495.  
  496. public class StartStateDef
  497. : DataNode
  498. {
  499. public StartStateDef
  500. ()
  501. : base()
  502. {
  503. }
  504.  
  505. public StartStateDef
  506. (XmlNode dom)
  507. : base(dom)
  508. {
  509. }
  510.  
  511. public StartStateDef
  512. (Document document)
  513. : base(document)
  514. {
  515. }
  516.  
  517. public override string XML_TAG_NAME
  518. {
  519. get
  520. {
  521. return "StartStateDef";
  522. }
  523. set
  524. {
  525. throw new Exception("cannot set");
  526. }
  527. }
  528.  
  529. public event ProperyChanged<string> OnId_ProperyChanged;
  530. public override string Id
  531. {
  532. get
  533. {
  534. if (this.Dom.Attributes["Id"] == null)
  535. return default(string);
  536. return this.Dom.Attributes["Id"].Value.ToT<string>();
  537. }
  538. set
  539. {
  540. var attr = this.Dom.Attributes.OfType<XmlAttribute>()
  541. .FirstOrDefault(x => x.Name == "Id");
  542. var oldValue = default(string);
  543. var newValue = value;
  544. if (attr == null)
  545. {
  546. attr = this.Dom.Attributes.Append(this.Dom.OwnerDocument.CreateAttribute("Id"));
  547. }
  548. else
  549. {
  550. oldValue = this.Dom.Attributes["Id"].Value.ToT<string>();
  551. }
  552.  
  553. if (value != null)
  554. attr.Value = value.ToString();
  555.  
  556. if (OnId_ProperyChanged != null && oldValue != newValue)
  557. {
  558. OnId_ProperyChanged(oldValue, newValue);
  559. }
  560. this.FireAnyProperyChanged("Id", oldValue, newValue);
  561. }
  562. }
  563. public event ProperyChanged<string> OnName_ProperyChanged;
  564. public string Name
  565. {
  566. get
  567. {
  568. if (this.Dom.Attributes["Name"] == null)
  569. return default(string);
  570. return this.Dom.Attributes["Name"].Value.ToT<string>();
  571. }
  572. set
  573. {
  574. var attr = this.Dom.Attributes.OfType<XmlAttribute>()
  575. .FirstOrDefault(x => x.Name == "Name");
  576. var oldValue = default(string);
  577. var newValue = value;
  578. if (attr == null)
  579. {
  580. attr = this.Dom.Attributes.Append(this.Dom.OwnerDocument.CreateAttribute("Name"));
  581. }
  582. else
  583. {
  584. oldValue = this.Dom.Attributes["Name"].Value.ToT<string>();
  585. }
  586.  
  587. if (value != null)
  588. attr.Value = value.ToString();
  589.  
  590. if (OnName_ProperyChanged != null && oldValue != newValue)
  591. {
  592. OnName_ProperyChanged(oldValue, newValue);
  593. }
  594. this.FireAnyProperyChanged("Name", oldValue, newValue);
  595. }
  596. }
  597.  
  598. }
  599.  
  600. //此处省略3000行...
  601. }

1.2 数据表及关系设计

1.2.1 数据语法如下:

1.2.2 生成数据库sql

1.2.3 生成的sql

  1. IF OBJECT_ID(N'People',N'U') IS NOT NULL
  2. DROP TABLE [dbo].[People];
  3. CREATE TABLE [dbo].[People](
  4. [Id] int NOT NULL,
  5. [Name] nvarchar(200) ,
  6. [UserId] int ,
  7. [SchoolId] int ,
  8. [Status] int ,
  9. CONSTRAINT [PK_People] PRIMARY KEY CLUSTERED
  10. (
  11. [Id] ASC
  12. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF
  13. , ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  14. ) ON [PRIMARY]
  15.  
  16. GO

1.3 持久化阶段

1.3.1 新建连接串

1.3.2 刷新链接,获取数据库所有的表和视图信息

刷新结果:

1.3.3 弹出生成操作

1.3.4 实体层生成

1.3.4.1 生成的EF实体类:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Domain.Entities;
  6. using Domain.AggregateRoot;
  7.  
  8. namespace AAA
  9. {
  10.  
  11. public class People : Entity, IAggregateRoot
  12. {
  13. public People()
  14. {
  15. }
  16. public People(System.Int32 id)
  17. : base(id)
  18. {
  19. }
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. public DateTime? CreateTime
  24. { get; set; }
  25.  
  26. /// <summary>
  27. ///
  28. /// </summary>
  29. public String Name
  30. { get; set; }
  31.  
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. public Int32? SchoolId
  36. { get; set; }
  37.  
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. public Int32? Status
  42. { get; set; }
  43.  
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. public DateTime? UpdateTime
  48. { get; set; }
  49.  
  50. /// <summary>
  51. ///
  52. /// </summary>
  53. public Int32? UserId
  54. { get; set; }
  55.  
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. public virtual User User
  60. { get; set; }
  61.  
  62. /// <summary>
  63. ///
  64. /// </summary>
  65. public virtual School School
  66. { get; set; }
  67.  
  68. public void AssignFrom(People other)
  69. {
  70. this.CreateTime = other.CreateTime;
  71. this.Name = other.Name;
  72. this.SchoolId = other.SchoolId;
  73. this.Status = other.Status;
  74. this.UpdateTime = other.UpdateTime;
  75. this.UserId = other.UserId;
  76. }
  77. }
  78. }

1.3.4.2 生成的EF映射类:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Domain.Repository;
  6.  
  7. namespace AAA
  8. {
  9.  
  10. public interface IPeopleRepository : IRepository<People>, IDisposable
  11. {
  12. List<People> GetList(List<Guid> id_list);
  13. }
  14. }

弹出生成Repository操作:

1.3.4.3 Repository接口代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Domain.Repository;
  6.  
  7. namespace AAA
  8. {
  9.  
  10. public interface IPeopleRepository : IRepository<People>, IDisposable
  11. {
  12. List<People> GetList(List<Guid> id_list);
  13. }
  14. }

1.3.4.4 Specification规约代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Domain.Specification;
  6.  
  7. namespace AAA
  8. {
  9.  
  10. public static class PeopleSpecification
  11. {
  12. public static ISpecification<People> GetList(List<Guid> id_list)
  13. {
  14. return new DirectSpecification<People>(x => id_list.Contains(x.Id));
  15. }
  16. }
  17. }

1.3.4.5 Repository实现代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Data;
  6. using Data.Repository;
  7.  
  8. namespace AAA
  9. {
  10.  
  11. public class PeopleRepository : Repository<People>, IPeopleRepository
  12. {
  13. public PeopleRepository(IQueryableUnitOfWork unitOfWork)
  14. : base(unitOfWork)
  15. {
  16. }
  17.  
  18. public List<People> GetList(List<Guid> id_list)
  19. {
  20. return this.GetAll(PeopleSpecification.GetList(id_list)).ToList();
  21. }
  22.  
  23. public void Dispose()
  24. {
  25. //throw new NotImplementedException();
  26. }
  27. }
  28. }

1.3.5 Service层生成

1.3.5.1 弹出生成WCF操作:

1.3.5.2 生成DTO代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Serialization;
  6.  
  7. namespace AAA
  8. {
  9.  
  10. [DataContract]
  11. public class PeopleDto
  12. {
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. [DataMember]
  17. public DateTime? CreateTime
  18. { get; set; }
  19.  
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. [DataMember]
  24. public Int32 Id
  25. { get; set; }
  26.  
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. [DataMember]
  31. public String Name
  32. { get; set; }
  33.  
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. [DataMember]
  38. public Int32? SchoolId
  39. { get; set; }
  40.  
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. [DataMember]
  45. public Int32? Status
  46. { get; set; }
  47.  
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. [DataMember]
  52. public DateTime? UpdateTime
  53. { get; set; }
  54.  
  55. /// <summary>
  56. ///
  57. /// </summary>
  58. [DataMember]
  59. public Int32? UserId
  60. { get; set; }
  61.  
  62. }
  63. }

1.3.5.3 生成实体转DTO代码:(建议用automap替代)

  1. public static PeopleDto ConvertToPeopleDto(People entity)
  2. {
  3. return new PeopleDto{
  4. Id = entity.Id,
  5. Name = entity.Name,
  6. SchoolId = entity.SchoolId,
  7. Status = entity.Status,
  8. UserId = entity.UserId,
  9. };
  10. }

1.3.5.4 生成DTO转实体代码:(建议用automap替代)

  1. public static People ConvertToPeople(PeopleDto entityDto)
  2. {
  3. return new People(entityDto.Id){
  4. Name = entityDto.Name,
  5. SchoolId = entityDto.SchoolId,
  6. Status = entityDto.Status,
  7. UserId = entityDto.UserId,
  8. };
  9. }

1.3.5.5 生成基本Servie代码:(基础增删改查)

  1. #region App Service Interface
  2.  
  3. ReturnInfoDto AddPeople(PeopleDto peopleDto);
  4.  
  5. ReturnInfoDto UpdatePeople(PeopleDto peopleDto);
  6.  
  7. ReturnInfoDto<PeopleDto> GetPeople(Guid id);
  8.  
  9. ReturnInfoDto<List<PeopleDto>> GetPeopleList(List<Guid> id_list);
  10.  
  11. ReturnInfoDto DeletePeople(Guid id);
  12.  
  13. ReturnInfoDto DeletePeople(List<Guid> id_list);
  14.  
  15. #endregion
  16.  
  17. #region App Service Implement
  18.  
  19. public ReturnInfoDto AddPeople(PeopleDto peopleDto)
  20. {
  21. var rtnInfo = new ReturnInfoDto();
  22. rtnInfo.Message = string.Empty;
  23. rtnInfo.IsSuccess = false;
  24. try
  25. {
  26. using (var unitOfWork = UnitOfWorkFactory.Create(DbOption.OA))
  27. {
  28. var peopleDB = new PeopleRepository(unitOfWork);
  29. var people = DataConverter.ConvertToPeople(peopleDto);
  30. peopleDB.Add(people);
  31. unitOfWork.Commit();
  32. }
  33. }
  34. catch (Exception ex)
  35. {
  36. rtnInfo.Message = ex.Message;
  37. return rtnInfo;
  38. }
  39.  
  40. rtnInfo.IsSuccess = true;
  41. return rtnInfo;
  42. }
  43.  
  44. public ReturnInfoDto UpdatePeople(PeopleDto peopleDto)
  45. {
  46. var rtnInfo = new ReturnInfoDto();
  47. rtnInfo.Message = string.Empty;
  48. rtnInfo.IsSuccess = false;
  49. try
  50. {
  51. using (var unitOfWork = UnitOfWorkFactory.Create(DbOption.OA))
  52. {
  53. var peopleDB = new PeopleRepository(unitOfWork);
  54. var info = peopleDB.Get(peopleDto.Id);
  55. if (info == null)
  56. {
  57. rtnInfo.Message = "the data is not in system.";
  58. return rtnInfo;
  59. }
  60. var other = DataConverter.ConvertToPeople(peopleDto);
  61. info.AssignFrom(other);
  62. peopleDB.Modify(info);
  63. unitOfWork.Commit();
  64. }
  65. }
  66. catch (Exception ex)
  67. {
  68. rtnInfo.Message = ex.Message;
  69. return rtnInfo;
  70. }
  71.  
  72. rtnInfo.IsSuccess = true;
  73. return rtnInfo;
  74. }
  75.  
  76. public ReturnInfoDto<PeopleDto> GetPeople(Guid id)
  77. {
  78. var rtnInfo = new ReturnInfoDto<PeopleDto>();
  79. rtnInfo.Message = string.Empty;
  80. rtnInfo.IsSuccess = false;
  81. try
  82. {
  83. using (var unitOfWork = UnitOfWorkFactory.Create(DbOption.OA))
  84. {
  85. var peopleDB = new PeopleRepository(unitOfWork);
  86. var people = peopleDB.Get(id);
  87. if(people != null)
  88. {
  89. rtnInfo.Data = DataConverter.ConvertToPeopleDto(people);
  90. }
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. rtnInfo.Message = ex.Message;
  96. return rtnInfo;
  97. }
  98.  
  99. rtnInfo.IsSuccess = true;
  100. return rtnInfo;
  101. }
  102.  
  103. public ReturnInfoDto<List<PeopleDto>> GetPeopleList(List<Guid> id_list)
  104. {
  105. var rtnInfo = new ReturnInfoDto<List<PeopleDto>>();
  106. rtnInfo.Message = string.Empty;
  107. rtnInfo.IsSuccess = false;
  108. rtnInfo.Data = new List<PeopleDto>();
  109. try
  110. {
  111. using (var unitOfWork = UnitOfWorkFactory.Create(DbOption.OA))
  112. {
  113. var peopleDB = new PeopleRepository(unitOfWork);
  114. var list = peopleDB.GetList(id_list);
  115. if(list != null && list.Count > )
  116. {
  117. list.ForEach( x => rtnInfo.Data.Add(DataConverter.ConvertToPeopleDto(x)));
  118. }
  119. }
  120. }
  121. catch (Exception ex)
  122. {
  123. rtnInfo.Message = ex.Message;
  124. return rtnInfo;
  125. }
  126.  
  127. rtnInfo.IsSuccess = true;
  128. return rtnInfo;
  129. }
  130.  
  131. public ReturnInfoDto DeletePeople(Guid id)
  132. {
  133. var rtnInfo = new ReturnInfoDto();
  134. rtnInfo.Message = string.Empty;
  135. rtnInfo.IsSuccess = false;
  136. try
  137. {
  138. using (var unitOfWork = UnitOfWorkFactory.Create(DbOption.OA))
  139. {
  140. var peopleDB = new PeopleRepository(unitOfWork);
  141. var people = peopleDB.Get(id);
  142. if(people == null )
  143. {
  144. rtnInfo.Message = "the data is not in system.";
  145. return rtnInfo;
  146. }
  147. //people.Available = false;
  148. //peopleDB.Modify(people);
  149. peopleDB.Remove(people);
  150. unitOfWork.Commit();
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. rtnInfo.Message = ex.Message;
  156. return rtnInfo;
  157. }
  158.  
  159. rtnInfo.IsSuccess = true;
  160. return rtnInfo;
  161. }
  162.  
  163. public ReturnInfoDto DeletePeople(List<Guid> id_list)
  164. {
  165. var rtnInfo = new ReturnInfoDto();
  166. rtnInfo.Message = string.Empty;
  167. rtnInfo.IsSuccess = false;
  168. try
  169. {
  170. using (var unitOfWork = UnitOfWorkFactory.Create(DbOption.OA))
  171. {
  172. var peopleDB = new PeopleRepository(unitOfWork);
  173. var people_list = peopleDB.GetList(id_list);
  174. foreach (var people in people_list)
  175. {
  176. //people.Available = false;
  177. //peopleDB.Modify(people);
  178. peopleDB.Remove(people);
  179. }
  180. unitOfWork.Commit();
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. rtnInfo.Message = ex.Message;
  186. return rtnInfo;
  187. }
  188.  
  189. rtnInfo.IsSuccess = true;
  190. return rtnInfo;
  191. }
  192.  
  193. #endregion
  194.  
  195. #region Distribute Service Interface
  196.  
  197. [OperationContract]
  198. [WithoutAuthorization]
  199. [FaultContractAttribute(typeof(UnAuthorization))]
  200. ReturnInfoDto AddPeople(PeopleDto peopleDto);
  201.  
  202. [OperationContract]
  203. [WithoutAuthorization]
  204. [FaultContractAttribute(typeof(UnAuthorization))]
  205. ReturnInfoDto UpdatePeople(PeopleDto peopleDto);
  206.  
  207. [OperationContract]
  208. [WithoutAuthorization]
  209. [FaultContractAttribute(typeof(UnAuthorization))]
  210. ReturnInfoDto<PeopleDto> GetPeople(Guid id);
  211.  
  212. [OperationContract]
  213. [WithoutAuthorization]
  214. [FaultContractAttribute(typeof(UnAuthorization))]
  215. ReturnInfoDto<List<PeopleDto>> GetPeopleList(List<Guid> id_list);
  216.  
  217. [OperationContract]
  218. [WithoutAuthorization]
  219. [FaultContractAttribute(typeof(UnAuthorization))]
  220. ReturnInfoDto DeletePeople(Guid id);
  221.  
  222. [OperationContract]
  223. [WithoutAuthorization]
  224. [FaultContractAttribute(typeof(UnAuthorization))]
  225. ReturnInfoDto DeletePeopleList(List<Guid> id_list);
  226.  
  227. #endregion
  228.  
  229. #region Distribute Service Implement
  230.  
  231. public ReturnInfoDto AddPeople(PeopleDto peopleDto)
  232. {
  233. return _appService.AddPeople(peopleDto);
  234. }
  235.  
  236. public ReturnInfoDto UpdatePeople(PeopleDto peopleDto)
  237. {
  238. return _appService.UpdatePeople(peopleDto);
  239. }
  240.  
  241. public ReturnInfoDto<PeopleDto> GetPeople(Guid id)
  242. {
  243. return _appService.GetPeople( id );
  244. }
  245.  
  246. public ReturnInfoDto<List<PeopleDto>> GetPeopleList(List<Guid> id_list)
  247. {
  248. return _appService.GetPeopleList( id_list );
  249. }
  250.  
  251. public ReturnInfoDto DeletePeople(Guid id)
  252. {
  253. return _appService.DeletePeople( id );
  254. }
  255.  
  256. public ReturnInfoDto DeletePeopleList(List<Guid> id_list)
  257. {
  258. return _appService.DeletePeople( id_list );
  259. }
  260.  
  261. #endregion
  262.  
  263. #region Service Layer
  264.  
  265. public ReturnInfo AddPeople(PeopleInfo peopleInfo)
  266. {
  267. var result = new ReturnInfo();
  268.  
  269. try
  270. {
  271. var dto = DataConverter.ConvertToPeopleDto(peopleInfo);
  272. var rslt = serviceClient.Invoke<ReturnInfoDto>(x => x.AddPeople(dto));
  273. result.IsSuccess = rslt.IsSuccess;
  274. result.Message = rslt.Message;
  275.  
  276. }
  277. catch (Exception e)
  278. {
  279. LogHelper.Error(e);
  280. result.IsSuccess = false;
  281. result.Message = "call service error";
  282. }
  283.  
  284. return result;
  285. }
  286.  
  287. public ReturnInfo UpdatePeople(PeopleInfo peopleInfo)
  288. {
  289. var result = new ReturnInfo();
  290.  
  291. try
  292. {
  293. var dto = DataConverter.ConvertToPeopleDto(peopleInfo);
  294. var rslt = serviceClient.Invoke<ReturnInfoDto>(x => x.UpdatePeople(dto));
  295. result.IsSuccess = rslt.IsSuccess;
  296. result.Message = rslt.Message;
  297.  
  298. }
  299. catch (Exception e)
  300. {
  301. LogHelper.Error(e);
  302. result.IsSuccess = false;
  303. result.Message = "call service error";
  304. }
  305.  
  306. return result;
  307. }
  308.  
  309. public ReturnInfo<PeopleInfo> GetPeople(Guid id)
  310. {
  311. var result = new ReturnInfo<PeopleInfo>();
  312.  
  313. try
  314. {
  315. var rslt = serviceClient.Invoke<ReturnInfoDto<PeopleDto>>(x => x.GetPeople(id));
  316. result.Data = DataConverter.ConvertToPeopleInfo(rslt.Data);
  317. result.IsSuccess = rslt.IsSuccess;
  318. result.Message = rslt.Message;
  319.  
  320. }
  321. catch (Exception e)
  322. {
  323. LogHelper.Error(e);
  324. result.IsSuccess = false;
  325. result.Message = "call service error";
  326. }
  327.  
  328. return result;
  329. }
  330.  
  331. public ReturnInfo<List<PeopleInfo>> GetPeopleList(List<Guid> id_list)
  332. {
  333. var result = new ReturnInfo<List<PeopleInfo>>();
  334. result.Data = new List<PeopleInfo>();
  335.  
  336. try
  337. {
  338. var rslt = serviceClient.Invoke<ReturnInfoDto<List<PeopleDto>>>(x => x.GetPeopleList(id_list));
  339. result.IsSuccess = rslt.IsSuccess;
  340. result.Message = rslt.Message;
  341. if (rslt.IsSuccess )
  342. {
  343. if (rslt.Data != null && rslt.Data.Count > )
  344. {
  345. rslt.Data.ForEach(x => result.Data.Add(DataConverter.ConvertToPeopleInfo(x)));
  346. }
  347. }
  348. }
  349. catch (Exception e)
  350. {
  351. LogHelper.Error(e);
  352. result.IsSuccess = false;
  353. result.Message = "call service error";
  354. }
  355.  
  356. return result;
  357. }
  358.  
  359. public ReturnInfo DeletePeople(Guid id)
  360. {
  361. var result = new ReturnInfo();
  362.  
  363. try
  364. {
  365. var rslt = serviceClient.Invoke<ReturnInfoDto>(x => x.DeletePeople(id));
  366. result.IsSuccess = rslt.IsSuccess;
  367. result.Message = rslt.Message;
  368.  
  369. }
  370. catch (Exception e)
  371. {
  372. LogHelper.Error(e);
  373. result.IsSuccess = false;
  374. result.Message = "call service error";
  375. }
  376.  
  377. return result;
  378. }
  379.  
  380. public ReturnInfo DeletePeople(List<Guid> id_list)
  381. {
  382. var result = new ReturnInfo();
  383.  
  384. try
  385. {
  386. var rslt = serviceClient.Invoke<ReturnInfoDto>(x => x.DeletePeopleList(id_list));
  387. result.IsSuccess = rslt.IsSuccess;
  388. result.Message = rslt.Message;
  389.  
  390. }
  391. catch (Exception e)
  392. {
  393. LogHelper.Error(e);
  394. result.IsSuccess = false;
  395. result.Message = "call service error";
  396. }
  397.  
  398. return result;
  399. }
  400.  
  401. #endregion

1.3.5.6 生成CCFlowService:此处省略(比较私有)

1.3.6 Web层生成

1.3.6.1 弹出ViewMode生成操作:

1.3.6.2 生成ViewModel代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Serialization;
  6.  
  7. namespace AAA
  8. {
  9.  
  10. public class PeopleInfo
  11. {
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. public DateTime? CreateTime
  16. { get; set; }
  17.  
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. public Int32 Id
  22. { get; set; }
  23.  
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. public String Name
  28. { get; set; }
  29.  
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. public Int32? SchoolId
  34. { get; set; }
  35.  
  36. /// <summary>
  37. ///
  38. /// </summary>
  39. public Int32? Status
  40. { get; set; }
  41.  
  42. /// <summary>
  43. ///
  44. /// </summary>
  45. public DateTime? UpdateTime
  46. { get; set; }
  47.  
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. public Int32? UserId
  52. { get; set; }
  53.  
  54. }
  55. }

1.3.6.3 生成ViewModel转WCF DTO代码:(建议用automapper替代)

  1. public static PeopleDto ConvertToPeopleDto(PeopleInfo info)
  2. {
  3. return new PeopleDto{
  4. Id = info.Id,
  5. Name = info.Name,
  6. SchoolId = info.SchoolId,
  7. Status = info.Status,
  8. UserId = info.UserId,
  9. };
  10. }

1.3.6.4 生成WCF DTO转ViewModel代码:(建议用automapper替代)

  1. public static PeopleInfo ConvertToPeopleInfo(PeopleDto dto )
  2. {
  3. return new PeopleInfo(){
  4. Id = dto.Id,
  5. Name = dto.Name,
  6. SchoolId = dto.SchoolId,
  7. Status = dto.Status,
  8. UserId = dto.UserId,
  9. };
  10. }

1.3.6.5 弹出生成View操作:

1.3.6.6 生成编辑页面Model代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Serialization;
  6. namespace AAA
  7. {
  8.  
  9. public class PeopleInfo
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public DateTime? CreateTime
  15. { get; set; }
  16.  
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. public Int32 Id
  21. { get; set; }
  22.  
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. public String Name
  27. { get; set; }
  28.  
  29. /// <summary>
  30. ///
  31. /// </summary>
  32. public Int32? SchoolId
  33. { get; set; }
  34.  
  35. /// <summary>
  36. ///
  37. /// </summary>
  38. public Int32? Status
  39. { get; set; }
  40.  
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. public DateTime? UpdateTime
  45. { get; set; }
  46.  
  47. /// <summary>
  48. ///
  49. /// </summary>
  50. public Int32? UserId
  51. { get; set; }
  52.  
  53. }
  54. }

1.3.6.7 生成编辑页面代码:

  1. @model EditPeopleModel
  2. @{
  3. Layout = "~/Views/Shared/_SimpleLayout.cshtml";
  4.  
  5. if(this.Model.Id.HasValue){
  6. ViewBag.Title = "Edit Item";
  7. }
  8. else{
  9. ViewBag.Title = "Add Item";
  10. }
  11. }
  12.  
  13. @section js{
  14. <script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
  15. <script language = "javascript" type="text/javascript">
  16. function OnClose(refresh) {
  17. if (parent.MsgBox != undefined) {
  18.  
  19. if (refresh) {
  20. if (parent.MsgBox.Callback != null) {
  21. parent.MsgBox.Callback();
  22. parent.MsgBox.Callback = null;
  23. }
  24. }
  25. parent.MsgBox.Callback = null;
  26.  
  27. parent.MsgBox.Close();
  28.  
  29. }
  30. else {
  31. window.close();
  32. }
  33. }
  34.  
  35. $(function () {
  36.  
  37. if (parent.MsgBox != undefined) {
  38. parent.MsgBox.CancelCallback = true;
  39. }
  40.  
  41. $('#btnSaveAndClose').click(function () {
  42.  
  43. $('#IsSave').val(true);
  44.  
  45. $('#editForm').submit();
  46.  
  47. });
  48.  
  49. $('#btnCancel').click(function () {
  50.  
  51. OnClose();
  52.  
  53. });
  54.  
  55. if ($('#IsSave').val() == 'True') {
  56. var msg = $('#ErrorMsg').val();
  57. if (msg == '') {
  58. jAlert('Save Success','Tip',function(){
  59. OnClose(true);
  60. });
  61. }
  62. else {
  63. jAlert(msg);
  64. }
  65. }
  66.  
  67. $('#somecontrolId').attr('readonly','true').addClass('readonly');
  68.  
  69. $('select').each(function(){
  70. $(this).val($(this).attr('_value'));
  71. });
  72.  
  73. var validator = $('#editForm').validate({
  74. debug: true, //调试模式取消submit的默认提交功能
  75. errorClass: 'error', //默认为错误的样式类为:error
  76. focusInvalid: false,
  77. onkeyup: false,
  78. submitHandler: function (form) { //表单提交句柄,为一回调函数,带一个参数:form
  79. //alert('提交表单');
  80. form.submit(); //提交表单
  81. },
  82. rules: { //定义验证规则,其中属性名为表单的name属性
  83. Name:{
  84. },
  85. SchoolId:{
  86. },
  87. Status:{
  88. },
  89. UserId:{
  90. }
  91.  
  92. },
  93.  
  94. messages: { //自定义验证消息
  95. PurchasePlan: {
  96. //required: 'required !',
  97. //minlength: $.format('用户名至少要{0}个字符!'),
  98. //remote: $.format('{0}已经被占用')
  99. },
  100. },
  101. errorPlacement: function (error, element) {
  102. var noHtml = '<div class="notification error" for="' + error.attr('for') + '">' +
  103. "<a class=\"close\" ><img alt=\"close\" title=\"Close this notification\" src=\"/Content/images/cross_grey_small.png\"></a>" +
  104. '<div>' + error.html() + '</div>' + '</div>';
  105. var notification = element.parent().find('div');
  106. if (notification.length > 0) {
  107. notification.remove();
  108. }
  109. element.parent().find('span').hide();
  110. $(noHtml).appendTo(element.parent());
  111. error.html('');
  112. error.appendTo(element.parent());
  113. },
  114. highlight: function (element, errorClass) { //针对验证的表单设置高亮
  115. //$(element).addClass(errorClass);
  116. },
  117. success: function (element) {
  118. element.parent().find('span').show();
  119. element.parent().find('div.error,label.validateError').remove();
  120. }});
  121. });
  122. </script>
  123.  
  124. }
  125.  
  126. @section css{
  127.  
  128. <style type="text/css">
  129. body
  130. {
  131. background: none;
  132. }
  133. .field
  134. {
  135. vertical-align: middle;
  136. clear: both;
  137. padding-left: 5px;
  138. padding-top: 5px;
  139. padding-bottom: 5px;
  140. }
  141. .field_name
  142. {
  143. width: 160px;
  144. float: left;
  145. padding-right: 10px;
  146. padding-top: 10px;
  147. margin: 0 auto;
  148. }
  149. .field_value
  150. {
  151. width: 260px;
  152. float: left;
  153. }
  154. custom-combobox input
  155. {
  156. width: 120px;
  157. }
  158. .operation
  159. {
  160. margin-top: 20px;
  161. padding-top: 20px;
  162. clear: both;
  163. }
  164. input
  165. {
  166. width: 206px;
  167. }
  168. select
  169. {
  170. width: 220px;
  171. }
  172. .readonly
  173. {
  174. background-color: #e6e6e6;
  175. }
  176. </style>
  177.  
  178. }
  179.  
  180. <div class="tabmini-content">
  181. <form method="post" id="editForm" class="form" action="/xxx/EditPeopleItem">
  182.  
  183. <input type='hidden' id='Id' name='Id' value='@Model.Id' />
  184. <input type='hidden' id='IsPostBack' name='IsPostBack' value='true' />
  185. <input type='hidden' id='IsSave' name='IsSave' value='@Model.IsSave' />
  186. <input type='hidden' id='ErrorMsg' name='ErrorMsg' value='@Model.ErrorMsg' />
  187.  
  188. <fieldset class="ConfigArea">
  189. <div class="field">
  190. <div class="field_name">
  191. <label>Name:</label>
  192. </div>
  193. <div class="field_value">
  194. <input type="text" name="Name" id="Name" value="@Model.Name"/>
  195. </div>
  196. </div>
  197. <div class="field">
  198. <div class="field_name">
  199. <label>SchoolId:</label>
  200. </div>
  201. <div class="field_value">
  202. <input type="text" name="SchoolId" id="SchoolId" value="@Model.SchoolId"/>
  203. </div>
  204. </div>
  205. <div class="field">
  206. <div class="field_name">
  207. <label>Status:</label>
  208. </div>
  209. <div class="field_value">
  210. <input type="text" name="Status" id="Status" value="@Model.Status"/>
  211. </div>
  212. </div>
  213. <div class="field">
  214. <div class="field_name">
  215. <label>UserId:</label>
  216. </div>
  217. <div class="field_value">
  218. <input type="text" name="UserId" id="UserId" value="@Model.UserId"/>
  219. </div>
  220. </div>
  221. <div class='operation'>
  222. <a class='button blue' id='btnSaveAndClose'>Save&Close</a> <a class='button blue'
  223. id='btnCancel'>Cancel</a>
  224. </div>
  225. </fieldset>
  226. </form>
  227. </div>

1.3.6.8 弹出生成MVC代码操作:

1.3.6.9 生成Action代码:

  1. public ActionResult EditPeople(EditPeopleModel model)
  2. {
  3.  
  4. //model.UserId = Motorola.ZCH49.OaSystem.WebSite.Models.User.GetCurrentUser(this).Id;
  5. model.Action();
  6. return View(model);
  7.  
  8. }
  9.  
  10. [HttpPost]
  11. public ActionResult DeletePeople(Guid id)
  12. {
  13. var model = new ReturnInfo();
  14.  
  15. if (id == default(Guid))
  16. {
  17. model.Message = "no id";
  18. }
  19. else
  20. {
  21. model = EditPeopleModel.DeletePeople(id);
  22. }
  23. return Json(model, JsonRequestBehavior.AllowGet);
  24.  
  25. }
  26.  
  27. [HttpPost]
  28. public ActionResult DeletePeople(List<Guid> idList)
  29. {
  30. var model = new ReturnInfo();
  31.  
  32. if (idList == null || idList.Count == )
  33. {
  34. model.Message = "no id";
  35. }
  36. else
  37. {
  38. model = EditPeopleModel.DeletePeople(idList);
  39. }
  40. return Json(model, JsonRequestBehavior.AllowGet);
  41.  
  42. }

1.3.7 视图的相关操作和表操作一样

1.4 自定义查询(解决常见查询动态参数化问题)

1.4.1 创建查询集(类似文件夹/目录概念,对查询进行管理)

1.4.2 创建查询

1.4.3 编辑查询

语法和语义同标准sql,主要用于动态的参数化查询,一下为一个常用查询的参数化(分页,各字段的排序,参数提取)

实例代码主要是EF查相关,其他的代码生成省略。

1.4.3.1 生成的查询条件类:

 using System;
using System.Collections.Generic; namespace xxx
{
public class People_Query_Condition
{
public enum OrderByType
{
CreateTime_Asc,
CreateTime_Desc,
Id_Asc,
Id_Desc,
Name_Asc,
Name_Desc,
SchoolId_Asc,
SchoolId_Desc,
Status_Asc,
Status_Desc,
UpdateTime_Asc,
UpdateTime_Desc,
UserId_Asc,
UserId_Desc, } public OrderByType? OrderBy { get; set; } public DateTime? CreateTime_Begin
{ get; set; } public DateTime? CreateTime_End
{ get; set; } public int? Id
{ get; set; } public List<int> Ids
{ get; set; } public string Name
{ get; set; } public int? SchoolId
{ get; set; } public int? Status
{ get; set; } public int? UserId
{ get; set; } public int PageIndex { get; set; }
public int PageSize { get; set; }
public int RecordCount { get; set; }
public People_Query_Condition()
{
this.Ids = new List<int>();
}
}
}

1.4.3.2 生成的查询DTO类:

 using System;
using System.Collections.Generic;
using System.Runtime.Serialization; namespace xxx
{
[DataContract]
public class People_Query_ConditionDto
{
[DataContract]
public enum OrderByType
{
[EnumMember]
CreateTime_Asc,
[EnumMember]
CreateTime_Desc,
[EnumMember]
Id_Asc,
[EnumMember]
Id_Desc,
[EnumMember]
Name_Asc,
[EnumMember]
Name_Desc,
[EnumMember]
SchoolId_Asc,
[EnumMember]
SchoolId_Desc,
[EnumMember]
Status_Asc,
[EnumMember]
Status_Desc,
[EnumMember]
UpdateTime_Asc,
[EnumMember]
UpdateTime_Desc,
[EnumMember]
UserId_Asc,
[EnumMember]
UserId_Desc, } [DataMember]
public OrderByType? OrderBy { get; set; } [DataMember]
public DateTime? CreateTime_Begin
{ get; set; } [DataMember]
public DateTime? CreateTime_End
{ get; set; } [DataMember]
public int? Id
{ get; set; } [DataMember]
public List<int> Ids
{ get; set; } [DataMember]
public string Name
{ get; set; } [DataMember]
public int? SchoolId
{ get; set; } [DataMember]
public int? Status
{ get; set; } [DataMember]
public int? UserId
{ get; set; } [DataMember]
public int PageIndex { get; set; }
[DataMember]
public int PageSize { get; set; }
[DataMember]
public int RecordCount { get; set; }
public People_Query_ConditionDto()
{
this.Ids = new List<int>();
}
}
}

1.4.3.3 生成的EF规约代码:

 public static ISpecification<People> People_Query(People_Query_Condition condition)
{
ISpecification<People> id_spec = null;
if (!condition.Id.HasValue)
{
id_spec = new DirectSpecification<People>(x=> true);
}
else{
id_spec = new DirectSpecification<People>(x=>
x.Id == condition.Id
);
} ISpecification<People> ids_spec = null;
if (condition.Ids == null || condition.Ids.Count == )
{
ids_spec = new DirectSpecification<People>(x=> true);
}
else{
} ISpecification<People> name_spec = null;
if (string.IsNullOrWhiteSpace(condition.Name))
{
name_spec = new DirectSpecification<People>(x=> true);
}
else{
name_spec = new DirectSpecification<People>(x=>
x.Name.Contains(condition.Name)
);
} ISpecification<People> schoolId_spec = null;
if (!condition.SchoolId.HasValue)
{
schoolId_spec = new DirectSpecification<People>(x=> true);
}
else{
schoolId_spec = new DirectSpecification<People>(x=>
x.SchoolId == condition.SchoolId
);
} ISpecification<People> status_spec = null;
if (!condition.Status.HasValue)
{
status_spec = new DirectSpecification<People>(x=> true);
}
else{
status_spec = new DirectSpecification<People>(x=>
x.Status == condition.Status
);
} ISpecification<People> userId_spec = null;
if (!condition.UserId.HasValue)
{
userId_spec = new DirectSpecification<People>(x=> true);
}
else{
userId_spec = new DirectSpecification<People>(x=>
x.UserId == condition.UserId
);
} ISpecification<People> createTime_Begin_spec = null;
if (!condition.CreateTime_Begin.HasValue)
{
createTime_Begin_spec = new DirectSpecification<People>(x=> true);
}
else{
createTime_Begin_spec = new DirectSpecification<People>(x=>
x.CreateTime >= condition.CreateTime_Begin
);
} ISpecification<People> createTime_End_spec = null;
if (!condition.CreateTime_End.HasValue)
{
createTime_End_spec = new DirectSpecification<People>(x=> true);
}
else{
createTime_End_spec = new DirectSpecification<People>(x=>
x.CreateTime <= condition.CreateTime_End
);
} var name_spec = new DirectSpecification<People>(x=>
(x.Name??"") != ""
); ISpecification<People> id_ids = new AndSpecification<People>(id_spec,ids_spec);
ISpecification<People> id_ids_name = new AndSpecification<People>(id_ids,name_spec);
ISpecification<People> id_ids_name_schoolId = new AndSpecification<People>(id_ids_name,schoolId_spec);
ISpecification<People> id_ids_name_schoolId_status = new AndSpecification<People>(id_ids_name_schoolId,status_spec);
ISpecification<People> id_ids_name_schoolId_status_userId = new AndSpecification<People>(id_ids_name_schoolId_status,userId_spec);
ISpecification<People> id_ids_name_schoolId_status_userId_createTime_Begin = new AndSpecification<People>(id_ids_name_schoolId_status_userId,createTime_Begin_spec);
ISpecification<People> id_ids_name_schoolId_status_userId_createTime_Begin_createTime_End = new AndSpecification<People>(id_ids_name_schoolId_status_userId_createTime_Begin,createTime_End_spec);
ISpecification<People> id_ids_name_schoolId_status_userId_createTime_Begin_createTime_End_name = new AndSpecification<People>(id_ids_name_schoolId_status_userId_createTime_Begin_createTime_End,name_spec);
return id_ids_name_schoolId_status_userId_createTime_Begin_createTime_End_name;
}

1.4.3.4 生成的EF存取代码:

 public PageOfReturn<People> People_Query( People_Query_Condition condition )
{
var data = this.GetAll( PeopleSpecification.People_Query(condition ));
if( condition.OrderBy.HasValue )
{
switch(condition.OrderBy)
{
case People_Query_Condition.OrderByType.CreateTime_Asc:
data = data.OrderBy(x=>x.CreateTime);
break;
case People_Query_Condition.OrderByType.CreateTime_Desc:
data = data.OrderByDescending(x=>x.CreateTime);
break;
case People_Query_Condition.OrderByType.Id_Asc:
data = data.OrderBy(x=>x.Id);
break;
case People_Query_Condition.OrderByType.Id_Desc:
data = data.OrderByDescending(x=>x.Id);
break;
case People_Query_Condition.OrderByType.Name_Asc:
data = data.OrderBy(x=>x.Name);
break;
case People_Query_Condition.OrderByType.Name_Desc:
data = data.OrderByDescending(x=>x.Name);
break;
case People_Query_Condition.OrderByType.SchoolId_Asc:
data = data.OrderBy(x=>x.SchoolId);
break;
case People_Query_Condition.OrderByType.SchoolId_Desc:
data = data.OrderByDescending(x=>x.SchoolId);
break;
case People_Query_Condition.OrderByType.Status_Asc:
data = data.OrderBy(x=>x.Status);
break;
case People_Query_Condition.OrderByType.Status_Desc:
data = data.OrderByDescending(x=>x.Status);
break;
case People_Query_Condition.OrderByType.UpdateTime_Asc:
data = data.OrderBy(x=>x.UpdateTime);
break;
case People_Query_Condition.OrderByType.UpdateTime_Desc:
data = data.OrderByDescending(x=>x.UpdateTime);
break;
case People_Query_Condition.OrderByType.UserId_Asc:
data = data.OrderBy(x=>x.UserId);
break;
case People_Query_Condition.OrderByType.UserId_Desc:
data = data.OrderByDescending(x=>x.UserId);
break;
default:
break;
}
}
var result = new PageOfReturn<People>();
result.RecordCount = data.Count();
result.PageIndex = condition.PageIndex;
result.PageSize = condition.PageSize;
result.PageRecords = data.Skip(condition.PageSize * (condition.PageIndex - )).Take(condition.PageSize).ToList();
return result;
}

1.4.3.5 其他层如Service,Web层。基本上代码少量改动一下就可以实现一个列表页面。实例代码省略

1.4.4 多表查询

1.4.4.1 生成的查询条件类:

 using System;
using System.Collections.Generic; namespace xxx
{
public class GetPeopleInfo_Condition
{
public string SchoolName
{ get; set; } public int? Status
{ get; set; } public GetPeopleInfo_Condition()
{
}
}
}

1.4.4.2 生成的查询条件DTO类:

 using System;
using System.Collections.Generic;
using System.Runtime.Serialization; namespace xxx
{
[DataContract]
public class GetPeopleInfo_ConditionDto
{
[DataMember]
public string SchoolName
{ get; set; } [DataMember]
public int? Status
{ get; set; } public GetPeopleInfo_ConditionDto()
{
}
}
}

1.4.4.3 生成的结果类:

 using System;
using System.Linq;
using System.Data;
using System.Collections.Generic; namespace xxx
{
public class PeopleInfo
{
public DateTime? CreateTime
{ get; set; } public int Id
{ get; set; } public string Name
{ get; set; } public int? SchoolId
{ get; set; } public int? Status
{ get; set; } public DateTime? UpdateTime
{ get; set; } public int? UserId
{ get; set; } public string SchoolName//来自表 School
{ get; set; } public string UserEmail //来自表 C_Users
{ get; set; } }
}

1.4.4.4 生成的EF代码(多表关联不再需要规约):

 public List<PeopleInfo> GetPeopleInfo( GetPeopleInfo_Condition condition )
{
using (var unitOfWork = UnitOfWorkFactory.Create(DbOption.OA))
{
var p_q = ctx.Set<People>().AsQueryable();
if(condition.Status != null )
{
p_q = p_q.Where(x=>x.Status == condition.Status);
} var s_q = ctx.Set<School>().AsQueryable();
if(condition.SchoolName != null )
{
s_q = s_q.Where(x=>x.Name == condition.SchoolName);
} var u_q = ctx.Set<User>().AsQueryable();
var q = p_q; var q_1 = q.GroupJoin(s_q,p=>p.SchoolId,s=>s.Id,
(p,s)=>new { p,s = s.DefaultIfEmpty() })
.SelectMany(item=>item.s.Select(s=> new {item.p,s})); var q_2 = q_1.GroupJoin(u_q,p=>p.p.UserId,u=>u.UserId,
(p,u)=>new { p,u = u.DefaultIfEmpty() })
.SelectMany(item=>item.u.Select(u=> new {item.p.p,item.p.s,u})); var q_final = q_2.Where(x=>
true); var list = q_final.Select(x=>new PeopleInfo(){
SchoolId = x.p.SchoolId,
Name = x.p.Name,
CreateTime = x.p.CreateTime,
UpdateTime = x.p.UpdateTime,
Id = x.p.Id,
Status = x.p.Status,
UserId = x.p.UserId,
SchoolName = x.SchoolName,
UserEmail = x.UserEmail,
});
return list.ToList();
}
}

1.4.5 高级查询

1.4.5.1 分组查询

1.4.5.2 选项查询

1.4.5.2.1 {'op','all'} 一个字段产生多个查询条件

1.4.5.2.2 对条件查询进行条件:在某些参数非空下区域内参数才有效

写这么多,感觉需要控制篇幅,查询告一段落~

1.5 反向工程(持久阶段元数据更新设计阶段元数据)

使设计永不过期

 ----------->

1.6 生成数据词典

  ----------->

终于码完了。

2. 总结:

  生成器基本上涉及了数据系统资源管理的方方面面(数据操作,应用层,服务层,展现层),能节约大量的开发时间。使工程师更注重业务知识,

举例来说,用来这套开发工具后,我们团队加班变得非常少,项目进度和预期的基本吻合。拙笔仓促,如有笔误和不妥请谅解,希望听到大家的意见和建议,thanks。

分享原创可复用且非侵入性代码生成工具(for .net)的更多相关文章

  1. 网友微笑分享原创Jquery实现瀑布流特效

    首先非常感谢网友微笑的无私分享,此Jquery特效是一款非常流行和实用的瀑布流布局,核心代码只有几十行,是我见过代码量最少的瀑布流布局,非常适合网友们学习哦,希望大家好好看一下这个Jquery特效的原 ...

  2. C#反射实现 C# 反射 判断类的延伸类型 使用代码生成工具Database2Sharp快速生成工作流模块控制器和视图代码 C# ADO.NET的SqlDataReader对象,判断是否包含指定字段 页面中添加锚点的几种方式 .net 简单实用Log4net(多个日志配置文件) C# 常用小点

    C#反射实现   一.反射概念: 1.概念: 反射,通俗的讲就是我们在只知道一个对象的内部而不了解内部结构的情况下,通过反射这个技术可以使我们明确这个对象的内部实现. 在.NET中,反射是重要的机制, ...

  3. [CSS工具推荐]0001.推荐 10 个超棒的 CSS3 代码生成工具

    引言:新的在线工具和 WebApp 帮助开发者快速地创建网站而不用写代码.前端开发已经在框架和代码库方面有了很大的进展. 现在许多开发者已经忘记了代码生成器在构建网站时的价值.下面的资源是完全免费的 ...

  4. 翻译:Laravel-4-Generators 使用自己定义代码生成工具高速进行Laravel开发

    使用自己定义代码生成工具高速进行Laravel开发 这个Laravle包提供了一种代码生成器,使得你能够加速你的开发进程.这些生成器包含: generate:model – 模型生成器 generat ...

  5. 突破CRUD | 简单优雅的代码生成工具诞生记(万字长文慎入)

    0.学习本文你或许可以收获 1.一个需求从产生.分析到解决的全过程思考2.简单的面向对象分析实践3.UML类图实践4.设计模式的实践应用5.最后收获一款还算不错的代码生成工具实现思路和源代码 本文将从 ...

  6. Go - 代码生成工具

    分享两个常用的代码生成工具: gormgen handlergen gormgen 基于 MySQL 数据表结构进行生成 3 个文件: 生成表的 struct 结构体 生成表的 Markdown 文档 ...

  7. 使用代码生成工具快速开发ABP框架项目

    在一般系统开发中,我们一般要借助于高度定制化的代码生成工具,用于统一代码风,节省开发时间,提高开发效率.不同的项目,它的项目不同分层的基类定义不同,我们需要在框架基类的基础上扩展我们的业务类代码,尽量 ...

  8. 基于SqlSugar的开发框架循序渐进介绍(3)-- 实现代码生成工具Database2Sharp的整合开发

    我喜欢在一个项目开发模式成熟的时候,使用代码生成工具Database2Sharp来配套相关的代码生成,对于我介绍的基于SqlSugar的开发框架,从整体架构确定下来后,我就着手为它们量身定做相关的代码 ...

  9. Map工具系列-01-Map代码生成工具说明

    所有cs端工具集成了一个工具面板 -打开(IE) Map工具系列-01-Map代码生成工具说明 Map工具系列-02-数据迁移工具使用说明 Map工具系列-03-代码生成BySQl工具使用说明 Map ...

随机推荐

  1. 其实没那么复杂!探究react-native通信机制

    近段时间来Android上最火的框架非react native莫属了,这里我不去评价这个框架的好坏,毕竟只有用过的人才会有深刻的体会.但是我个人有一个习惯,在使用一个开源库之前,一定要看过它的源码,不 ...

  2. 中文Win7下成功安装calabash-android步骤

    Calabash-android是支持android的UI自动化测试框架,网上看见很多同学说,安装calabash比较费劲,特别是Windows下安装,也没有一个详细的安装手册可供参考.正好,今天在W ...

  3. SqlServer将日期格式DateTime转换成varchar类型

    Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CONVERT(varchar(100), GETDATE( ...

  4. NGUI如何创建自己的精灵图集

    说实话其实很简单,但是在不知道的情况下真的不好弄啊. 1. 选择你要制作精灵图集的图片,可以选择多张 2. 提倡使用快捷键Alt + Shift + M 会有如下窗口弹出,也可以NGUI --> ...

  5. 设计模式_C++源码+总结

    C++源码下载: http://yunpan.cn/Q7fadjGgEJxib  提取码 63cb 总结:

  6. python 网络编程(三)---TCP 服务器端客户端实现

    客户端 客户端主要有4个步骤: 1)创建一个socket以连接服务器. socket = socket.socket(family, type),family参数代表地址家族,可为AF_INET(包括 ...

  7. 命令mv

    mv 文件 目标目录如果目标目录改成文件名,mv命令可用于重命名文件.

  8. hadoop源码导入eclipse

    1,下载hadoop源码 下载链接 http://svn.apache.org/repos/asf/hadoop/common/tags/release-2.2.0/   为2.2.0的源码, 也可以 ...

  9. BNUOJ-26474 Bread Sorting 逆序对

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26474 题意:给一个数列,可以对三个数操作:把最后一个数放到第一个,前两个数后移一位.问最 ...

  10. 关于JavaScripting API您不知道的5件事

    现在,许多 Java 开发人员都喜欢在 Java 平台中使用脚本语言,但是使用编译到 Java 字节码中的动态语言有时是不可行的.在某些情况中,直接编写一个 Java 应用程序的脚本 部分 或者在一个 ...