Lightweight code generation scheme selection

Source: Internet
Author: User

Version 3rd -- 5.6.22 updated

The last time I talked about myappfuse, I had to have a code generation tool. codegeneration.net is a collection of tools for various platforms and languages.
In fact, code generation is just as old as bad smell, which is already repeated in Code. However, in this era, after fully exploring inheritance, delegation, reflection, and even AOP, coding is still boring and repetitive. At this time, we need to describe the system from a more abstract level and then generate the code we love and hate. This is the Generative programming (GP ).

Advanced MDA applications
Those who use omg uml as the meta model, combined with MOF, OCL, and other definition and conversion syntaxes, expect to be able to fully describe the high-level MDA application of the system. I don't think it will be so fast and widely promoted, it's not a big opportunity for everyone to try coding.
First, the structure of OMG is not very high-yielding. Its current achievements are also profound.
Secondly, it is the constraint of the underlying technology. Because MDA is composed of models, implementations, and conversion programs. If model definition develops rapidly, there will be a huge gap between them and the underlying implementation, and the conversion program needs to do a lot of work to eliminate them. When the gap is large enough, few will be willing to do this conversion. To reduce the gap:
First, it waits for the development of the underlying implementation, but this is the whole thing in the IT industry, not the personal problem of the MDA developers.
Second, we can only reduce the definition height of the model, such as andromda. Many existing templates only rely on the UML static class diagram.
However, with the development of bottom layers such as AOP, meta-data, O/R Mapping, and IOC container, and the impact of Microsoft DSL on UML, MDA will continue to develop slowly, after all, this is our dream.

Lightweight code generation scheme
Currently, it is more practical to select some lightweight code generation solutions. I selected a custom model in XML format + groovy template-based template language (note,It's groovy template, not groovy).

1. xml Model
Now OMG is loose. UML is not the only solution. It can be described and converted through a higher-level model. XML is undoubtedly the lightest choice for lightweight modeling. Martin Fowler also believes that UML does not look like a higher-level high-level language, and the plain XML may be more practical)

2. Generate a solution
Of course, XDoclet can also be used like appfuse, but I think the scalability, management and applicability of XDoclet are the lowest. You can also use the C #/Java program to completely control the generation of code without a template. Now, there are more dynamic languages such as Python, Ruby, and groovy. However, I still prefer more templates.

The template options are available now.JSP,Velocity/freemarkerAnd the latestGroovy Template.
This time, the selected battlefield is moved from the WEB presentation layer to the template generated by lightweight code, and the standards have changed.
First, the flexibility and scalability of the template code are re-required.Because lightweight code generation does not involve MVC or good MVC, the template code must be able to process complicated logic. Therefore, the script type JSP and groovy template are dominant.

Second, the built-in xml dom syntax is required.Because our metadata is defined in XML, We need to access too much XML in the Code generatate process, and the concise and intuitive syntax is very useful, whether it is template readability or writing speed, for example, the following metadata:
<Table>
<Column name = "ID"/>
<Column name = "name"/>
</Table>

Freemarker or groovy template can list the names of all columns in the table as follows:

<# List table. * as column>
$ {Column. @ name}
</# List>

JSP and velocity should use the long JDOM syntax to make the template dirty.

Third, the code is generated, not the page.Not an artist. freemarker/velocity markup-language-based advantages do not exist.

Fourth, the code needed everywhere is concise.JSP is the least concise, while groovy, as a new generation of dynamic language, is quite charming.

5. IDE,Freemarker, velocity, and groovy Template

It can be seen that JSP is more powerful than velocity/freemarker in processing logic, and Java syntax can be used by everyone;
Velocity/freemarker is simpler than JSP and does not need to depend on Web Container. freemarker also has built-in XML syntax.
The Groovy template is precisely a combination of the advantages of both. Of course, it is not yet mature, and there is no good ide.

So, groovy template or freemarker in the team are both feasible options.
In the end, what to use as a template is not a very important thing, and it is not difficult to migrate between templates. Here we just write the group memoring process to record the Process of Low decision.

Related Articles: Generate code 2nd using groovy Template

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.