Preliminary Study on codesmith Technical Principles

Source: Internet
Author: User

A Preliminary Study on codesmith's technical principles.

I first heard that codesmith was in an article called <ten essential tools that every developer should download now>. At first codesmith gave me the impression that codesmith was not attractive. during a relatively idle period last year, I tried codesmith to find that codesmith is indeed a good thing. after codesmith is used, it is discovered that its implementation principle is not very complex. net syntax, it should be said that codesmith implements a part of Asp.net functions (generate HTML scripts from the aspx template) pseudo Asp.net, but codesmith provides an additional attribute declaration function. the following is a brief overview of codesmith's implementation principles:
1. Supports code block execution syntax, which is exactly the same as Asp.net syntax.
2. Supports the inline expression syntax, which is exactly the same as the Asp.net syntax.
3/support code behind (similar to Asp.net ()
4. Supports the function code block function, which is similar to the function code block function of Asp.net.
5. Attribute declaration is supported. The attribute. Asp.net syntax used to configure the template is not supported.

About how codesmith generates code through templates, we only need to understand how Asp.net generates HTML scripts through the aspx file. in fact, the process of generating HTML scripts by Asp.net is far more complex than codesmith, because Asp.net also needs to present the HTML Script of servercontrol. codesmith only focuses on grammar analysis and conversion of "Execution code block" "inline expression" "function code block. the syntax analysis logic is relatively simple (the same as Asp.net). The specific process is as follows:
1/generate a class class1 (using the template file name as the class name), which is inherited from the codetemplate class by default. If code behind exists, then inherit from the parent class marked by code behind (the parent class must inherit from the codetemplate class)
2. Add the corresponding attribute declaration and member variable code in class1 Based on the declared attributes.
3. Copy the function body in the function code block in the template file to class1 as a member function of class1.
4/start to generate the core function body of class1 :__ rendermethod1 (system. io. textwriter writer, codesmith. engine. control), migrate the code blocks (excluding the character '') in the Execution code blocks in the template file to _ rendermethod1 in sequence, convert the code in the template file that does not belong to the Execution code block and inline expression into writer. write ("Code"); convert the expression of the inline expression in the template file to writer. write (expression ).
5. Compile the generated class class1.
6. Call the _ rendermethod1 function of class1 to generate the actual code (as in Asp.net, the runtime framework of Asp.net generates HTML scripts for each request ).

Through the above analysis, we feel that codesmith's implementation principle is relatively simple, it is basically the same as Asp.net's HTML Script Generation Principle (you can compare the compiled source produced by codesmith with the page compilation source of Asp.net ). at this point, someone will think, if codesmith is not available, can I use Asp.net instead of codesmith? Of course, the answer is absolutely yes. codesmith only brings us a lot of convenience (for example, accessing dbschema functions, setting attribute values, etc., directly compile and generate code ), using Asp.net as a code generator is relatively complicated (at least create a web virtual directory, design An ASPX template file, and obtain the HTML Script generated by Asp.net using IE requests) we also need to add some extensions similar to codesmith, so the workload is not small. If you are interested, please try it.

In addition, in codesmith2.6, the number of lines of compiled error codes is the number of lines of compiled source, not the number of lines in the original template file, in codesmith 3.0, it is already pointing to the number of rows in the original template file (this is already the same as the page compilation source of Asp.net, which is implemented through the preprocessing command: # Line)

Some good codesmith template files can be from http://www.ericjsmith.net/codesmith/forum/default.aspx? F = 16 & P = 1 & ord = VD download...

 

Share the codesmith template that automatically generates DTO/dataaccess/bizobject
If you have any code templates, can you share them with us? I plan to build a team that shares the codesmith template and share our templates (there are already many templates on the codesmith Forum ).
As long as you click the button, some code of the DTO object and its collection class, data volume class, and business layer class is automatically generated to form three directories: model, dataaccess, and Biz, and generate corresponding class files.
Mytemplates.rar

Prerequisites:
1. We recommend that you name the tables in the plural form when creating tables in the database.
2. Set the primary key of the table.
3. Set the foreign key of the table.
4. Create a table index.

Main features:
1. One-to-many and multi-to-one relationships are supported.
2. Supports like queries.

Usage:
1. Complete the steps mentioned in "prerequisite.
2. Use allstoredprocedures. CST to generate all stored procedures
3. Use model/bulk. CST to generate all files

Disadvantages:
1. I just created it, and I haven't come to test it in detail yet.
2. modelobject. CST in the template is useless.
3. bizobject. CST is not released yet.
(Although there is a template for making your own enterpriselibrary application block, but there is no content in it, it will be provided later)

If you have any good architecture, I can help you establish a template and hope to provide more comments on this 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.