[NET]. NET programmers ten essential tools-CodeSmith

Source: Internet
Author: User
CodeSmith

CodeSmith is a template-based code generation tool that uses syntax similar to ASP. NET to generate any type of code or text. Unlike many other code generation tools, CodeSmith does not require you to subscribe to specific application designs or architectures. CodeSmith can be used to generate anything including a simple set of strong types and a complete application.

When you generate an application, you often need to repeat some specific tasks, such as writing data access code or generating a custom set. CodeSmith is especially useful in these cases because you can write templates to automatically complete these tasks, which not only improves your work efficiency, but also automatically complete the most boring tasks. CodeSmith comes with many templates, including templates corresponding to all. NET Collection types and templates used to generate stored procedures. But the real power of this tool is the ability to create custom templates. To help you get started, I will quickly introduce how to generate custom templates.

Generate custom templates

The CodeSmith template is only a text file that can be created in any text editor. Their only requirement is to use the. cst file extension to save them. The sample template I will generate will accept a string and then generate a Class Based on the string. The first step to create a template is to add a template header, which can declare the template language, target language, and brief template description:

The next part of the template is the attribute declaration. Here, you can declare the attributes that will be specified during each execution of the template. For this template, the unique attribute I want to use is only a string, so the attribute declaration is as follows:

This attribute Declaration will make the ClassName attribute appear in the CodeSmith attribute window, so that you can specify it when the template is running. The next step is to actually generate the Template subject, which is very similar to encoding with ASP. NET. You can view the entity of the template in the following code block.

// Custom Template
Public sealed class <% =? ClassName? %>
{
Private static volatile <% =? ClassName? %> _ Instance;
Private <% =? ClassName? %> (){}
Private static readonly object _ syncRoot = new object ();

Public static <% =? ClassName? %> Value
{
Get
{
If (_ instance = null)
{
Lock (_ syncRoot)
{
If (_ instance = null)
{
_ Instance = new <% =? ClassName? %> ();
}
}
}
Return _ instance;
}
}
}

// SingletonClass
Public sealed class SingletonClass
{
Private static volatile SingletonClass _ instance;
Private SingletonClass (){}
Private static readonly object _ syncRoot = new object ();

Public static SingletonClass Value
{
Get
{
If (_ instance = null)
{
Lock (_ syncRoot)
{
If (_ instance = null)
{
_ Instance = new SingletonClass ();
}
}
}
Return _ instance;
}
}
}

As you can see, this template accepts string input and uses this class name to generate a separate class. In the template body, use the same start and end tags as ASP. NET. In this template, I only insert attribute values, but you can also use any type of. NET code within these tags. After the template is complete, you can double-click it or open it from the CodeSmith application to load it into CodeSmith. The template that has been loaded to CodeSmith is displayed.

You can see that the attribute on the left is exactly the attribute I declared in this template. If I enter "SingletonClass" as the class name and click the Generate button, the class shown at the bottom of the code block is generated.

CodeSmith is quite easy to use and can produce some incredible results if it can be correctly applied. One of the most common parts of Code-oriented applications is the data access layer. CodeSmith includes a special Assembly named SchemaExplorer that can be used from tables, stored procedures, or almost any other SQL Server? Object generation template.

CodeSmith is written by Eric J. Smith and can be downloaded from the http://www.ericjsmith.net/codesmith.

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.