提高生产性工具 - Model代码生成器(NET / JAVA) (一)

原来在上一家公司,整整一年都在做工具,提高生产性,那个项目特别巨大,所以总共为老东家节约了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的开发.

欢迎大家提需求。。。

更新:

我还希望能做这样一个路由配置功能,这些功能,其实更对的想给设计人员使用,或者能够将大家的时间从记忆代码写法中解放出来.

我喜欢可视化的东西,目标是做一个设计书到代码的自动工具.当然,我承认,不可能做到完全不靠手工编码,但是希望这个比例能够减少很多.

人,应该去做更加需要思考的工作.

关注点分离,让设计路由的人,统筹管理路由规则.

 

posted @ 2014-12-12 11:26  灰毛毛  阅读(5194)  评论(17编辑  收藏  举报