Codesmith is used for encoding.

Source: Internet
Author: User

The role of codesmith is self-evident. People who have used it will think it is very powerful. based on custom templates, codesmith can be quickly generated.CodeOnly when we use it, we need to use the template in the codesmith studio environment provided by it, and then input the corresponding parameters to generate the net class file.

If we can integrate the codesmith function into the net code through encoding, it will be more flexible and convenient for us to control. We can implement it according to the following steps.

First, let's talk about the environment. The codesmith version I installed is 5.1, of course, a cracked version (You know ). codesmith registration fees are expensive. we need the following DLL files, which can be found in the installation path.

Codesmith. Engine. dll

Codesmith. Feedback. Client

Schemaexploer. dll

Schemaexploer. sqlschemaprovider. dll

After these DLL files are introduced into the project file, you need to create a template. You can also directly use the template that comes with codesmith. open codesmith studio and choose template exploer on the right. Right-click a template and choose Open folder in window exploer from the shortcut menu. After confirming the template path, you can start encoding.

Before writing code, let's take a look at the composition of the template, as shown in-1.

Figure-1

In code 2nd, the property keyword indicates the attributes of this template. It is also a parameter that needs to be passed in. The type is schemaexploer. databaseschema. That is to say, we only need to pass in the parameter value of this type.

Code-2

Figure 2

The code is very simple. Load the template before compiling, input the attribute value, and then output it. The template compilation part is the compiletemplate method, as shown in-3.

Figure-3

The code for passing in the property value is codetemplate. setproperty;

The code of the output class file is rendertofile;

OK. The whole process is over. Let's take a look at our results, as shown in-4.

Figure 4

This template is used to generate enumeration classes for all dB tables in the DB database.

Codesmith encoding method is very flexible. For example, if I want to generate the createsql statement for the DB table in the database, we may have spelled the character string before. Now we use the codesmith template, which is very simple. -5. You can refer to this code and try it yourself. Is it very simple?

 

Code File

Codesmith. Files

Figure 5

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.