原來在上一家公司,整整一年都在做工具,提高生產性,那個項目特別巨大,所以總共為老東家節約了500K左右的美金。
(除了表揚之外,我個人什么好處都沒有,領導們都升官發財了,郁悶)
到了新公司,也准備開發一些提高生產性的工具。最近在看NET MVC和Spring MVC的資料。所以想開發一個Model代碼生成工具。
公司不能上Git,代碼也剛開始寫,所以暫時不拿出來了。邏輯很簡單,博客園高手如雲,看一下就知道我怎么寫的了。
功能現在還很簡單,以后完善。
軟件的界面大概是這個樣子的,驗證這塊只是開了個頭罷了。
由於公司未來不知道做NET還是JAVA(或許都做),所以,這里NET和JAVA都對應了.
自動生成的代碼也很簡單,以后會擴展的。
C#的
using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace model.Models { public class AccountModel { #region "model" [DisplayName("用戶名")] [Required(ErrorMessage = "項目名稱不能缺少")] public string username { get; set; } [DisplayName("密碼")] [Required(ErrorMessage = "項目名稱不能缺少")] public string password { get; set; } #endregion } }
JAVA的
package model; public class AccountModel { //用戶名 private String username ; public void setusername(String username){ this.username = username; } public String getusername(){ return this.username; } //密碼 private String password ; public void setpassword(String password){ this.password = password; } public String getpassword(){ return this.password; } }
注意點:
關於MetaData,Java和C#有一些區別,例如 C# 字符 string ,Java 字符 String
這里有懂Java的人嗎,Java有自動屬性嗎,set,get一大堆,看着不爽阿。好像Spring框架一定要這么寫。。。
代碼生成也很簡單,就是字符串的操作,StringBuilder里面不停的AppendLine
using System; using System.IO; using System.Text; namespace DevKit.MVCTool { public static partial class ModelCodeGenerator { /// <summary> /// 縮進 /// </summary> private static int indent; private static string UsingSystem = "using System;"; private static bool NeedComponentModel = false; private static string UsingComponentModel = "using System.ComponentModel;"; private static bool NeedDataAnnotations = false; private static string UsingDataAnnotations = "using System.ComponentModel.DataAnnotations;"; /// <summary> /// 生成ModelCode /// </summary> /// <param name="filename"></param> /// <param name="model"></param> internal static void GenerateCSharp(string filename, ModelInfo model) { StreamWriter codeWriter = new StreamWriter(filename, false); StringBuilder code = new StringBuilder(); //縮進用空格 char space = " ".ToCharArray()[0]; indent = 0; code.AppendLine(String.Empty); code.AppendLine("namespace " + model.ProjectName + ".Models"); code.AppendLine("{"); indent += 4; code.AppendLine(new string(space, indent) + "public class " + model.ModelName); code.AppendLine(new string(space, indent) + "{"); indent += 4; code.AppendLine(new string(space, indent) + "#region \"model\""); code.AppendLine(String.Empty); indent += 4; foreach (var item in model.Items) { if (!string.IsNullOrEmpty(item.DisplayName)) { //DisplayNameAsComment if (item.DisplayNameAsComment) { code.AppendLine(new string(space, indent) + "/// <summary>"); code.AppendLine(new string(space, indent) + "/// " + item.DisplayName); code.AppendLine(new string(space, indent) + "/// </summary>"); } //DisplayName code.AppendLine(new string(space, indent) + "[DisplayName(\"" + item.DisplayName + "\")]"); NeedComponentModel = true; } if (item.Required) { //Required Without Error Message if (!String.IsNullOrEmpty(item.ErrorMsgForRequired)) { code.AppendLine(new string(space, indent) + "[Required(ErrorMessage = \"" + item.ErrorMsgForRequired + "\")]"); } else { code.AppendLine(new string(space, indent) + "[Required]"); } NeedDataAnnotations = true; } code.AppendLine(new string(space, indent) + "public @type @name { get; set; }" .Replace("@type", Common.DotNet.MetaData[item.DataType]) .Replace("@name", item.Name) ); code.AppendLine(String.Empty); } indent -= 4; code.AppendLine(new string(space, indent) + "#endregion"); indent -= 4; code.AppendLine(new string(space, indent) + "}"); indent -= 4; code.AppendLine("}"); codeWriter.WriteLine(UsingSystem); if (NeedComponentModel) codeWriter.WriteLine(UsingComponentModel); if (NeedDataAnnotations) codeWriter.WriteLine(UsingDataAnnotations); codeWriter.Write(code); codeWriter.Close(); } } }
這種工具,小項目里面其實真心沒有什么作用,大項目,需要很嚴格的編碼規范,然后項目非常多的,價值就體現出來了。
所有代碼編碼規則可以統一了,做設計的人,直接用工具作設計即可,然后支持從Excel導入導出,又是一個提高生產性的措施。
設計人員完成這樣的表格,然后,工具直接讀取Excel文件,生成代碼,這個是我的目標.
關注點分離,讓設計模型規則的人,做設計,開發者關注BL的開發.
歡迎大家提需求。。。
更新:
我還希望能做這樣一個路由配置功能,這些功能,其實更對的想給設計人員使用,或者能夠將大家的時間從記憶代碼寫法中解放出來.
我喜歡可視化的東西,目標是做一個設計書到代碼的自動工具.當然,我承認,不可能做到完全不靠手工編碼,但是希望這個比例能夠減少很多.
人,應該去做更加需要思考的工作.
關注點分離,讓設計路由的人,統籌管理路由規則.