Improved productivity tools-Model code generator (Net/java) (i)

Source: Internet
Author: User

Originally in the last company, a whole year to do tools, improve production, the project is particularly large, so the total savings for the old club of about 500K dollars.

(in addition to praise, I personally do not have any benefits, the leaders are ladder, depressed)

To the new company, it is also ready to develop some tools to improve production. Recently looking at net MVC and spring MVC data. So you want to develop a model code generation tool.

The company can not git, the code is just beginning to write, so temporarily do not take out. Logic is very simple, blog Park master such as the cloud, look at you know how I write.

Functions are now very simple and perfect later.

The interface of the software is probably like this, verifying that this piece is just a start.

Because the company does not know whether to do net or Java (perhaps all), so, here net and Java are corresponding.

The automatically generated code is also simple and will be extended later.

C # 's

usingSystem;usingSystem.ComponentModel;usingSystem.ComponentModel.DataAnnotations;namespacemodel. models{ Public classAccountmodel {#region"Model"[DisplayName ("User name")] [Required (ErrorMessage="project name cannot be missing")]             Public stringUsername {Get;Set; } [DisplayName ("Password")] [Required (ErrorMessage="project name cannot be missing")]             Public stringPassword {Get;Set; } #endregion    }}

Java's

 Packagemodel; Public classAccountmodel {//User name    PrivateString username;  Public voidSetusername (String username) { This. Username =username; }     PublicString GetUserName () {return  This. Username; }    //Password    PrivateString password;  Public voidSetPassword (String password) { This. Password =password; }     PublicString GetPassword () {return  This. Password; }}

Note the point:

There are some differences between Metadata,java and C #, such as C # character string, Java character string

Is there anyone who understands Java, Java has automatic attributes, set,get a lot of, look at the unpleasant. It's like the spring framework must be written like this ...

Code generation is also very simple, is the operation of the string, StringBuilder inside the constant appendline

usingSystem;usingSystem.IO;usingSystem.Text;namespacedevkit.mvctool{ Public Static Partial classModelcodegenerator {/// <summary>        ///Indent in/// </summary>        Private Static intindent; Private Static stringUsingsystem ="using System;"; Private Static BOOLNeedcomponentmodel =false; Private Static stringUsingcomponentmodel ="using System.ComponentModel;"; Private Static BOOLNeeddataannotations =false; Private Static stringUsingdataannotations ="using System.ComponentModel.DataAnnotations;"; /// <summary>        ///Generate Modelcode/// </summary>        /// <param name= "filename" ></param>        /// <param name= "model" ></param>        Internal Static voidGeneratecsharp (stringfilename, modelinfo model) {StreamWriter Codewriter=NewStreamWriter (filename,false); StringBuilder Code=NewStringBuilder (); //Indent with spaces            CharSpace =" ". 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(varIteminchmodel. 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>"); }                    //DisplayNameCode. 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 (); }    }}

This kind of tool, small project inside actually really does not have any function, big item, need very strict coding norm, then project very much, value is embodied.

All code coding rules can be unified, do the design of the person, directly with the tool for design, and then support from Excel import and export, but also an increase in production measures.

Welcome to ask the demand ...

Improved productivity tools-Model code generator (Net/java) (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.