Ten must-have .NET development gadgets (3): Codesmith

xiaoxiao2021-03-06  53

Author: James Avery translation: lucentoff

Source: http://msdn.microsoft.com/msdnmag/issues/04/07/musthavetools/default.aspx declaration: The copyright belongs to the original author, reproduced please indicate the source!

Codesmith

CodeSmith is a template-based code generation tool that uses syntax similar to ASP.NET to generate code or text. Codesmith is not like many other code generation tools, which does not require you to master special applications or architectures. With CODesmith, you can generate a simple intensive set, or you can generate a complete application.

When you create an application, whether you write a data access code or create a custom collection, you often find yourself in repeating certain work. At this time, Codesmith is especially useful, because you can write templates to move repetitive tasks, not only increase productivity, but also automatically complete the mission.

Codesmith has many templates, including all .NET collection type templates, and templates that generate stored procedures, but it really powerful is to create custom templates. Let's quickly introduce to create a custom template.

Create a custom template

Codesmith

Template is a simple text file, you can create any text editor, just save it

.cst

To the extension file. I will have a string to create a string and create a class named in this string. The first is to add a template head, which declares a brief description of the template language, target language, and template:

<% @ CODETEMPLATE LANGUAGE = "C #"

?? TargetLanguage = "C #"

?? Description = "car template"%>

Next is to declare the properties to be specified at each time of the template. I want to use a string type attribute, which is as follows:

<% @Property name = "classname" type = "string" category = "context"

???? description = "class name"%>

When the template is running, the className property will appear in the CodeSmith property window. The next step is to create a practical template body, very similar to writing code with ASP.NET, as shown in Figure 3.

Figure 3 custom template and code output

It can be seen that the template will accept string input and generate a single class with it for class. The start and end delimiter marks (<% and%>>) used in the template body are the same as ASP.NET. In this template, I just simply inserted the attribute value, but you can use any type of .NET code in the delimiter mark. Once the template is completed, you can load it into the CODesmith by double-clicking on it, as shown in Figure 4.

Figure 4 Open the template file

It can be seen that the attribute on the left is declared in the template. If you enter "Singletonclass" as class name, then click the "Generate" button to generate the class shown at the bottom of Figure 3.

CODesmith is easier to use, properly use it. One of the most common features of the code generated application is to generate data access layer code. Codesmith includes a dedicated assembly called SchemaExplorer, which can be used to generate data tables, stored procedures, or almost any other SQL Server? Object template.

CODesmith is written by Eric J. Smith, download URL: http://www.ericjsmith.net/codesmith. [

The following is the translator to add, taken from:

http://www.ericjsmith.net/codesmith]

CODesmith overview

CODesmith is a template-based code generator free software that can generate any ASCII-based programming language code. The generated code can be customized using attributes. The property can be any .NET object with the designer (most .NET built-in type already designer), or a simple Boolean property that allows you to add or remove code from the results, or one Objects, such as TableSchema objects that can access database table information (included in SchemaExplorer). CODesmith fully scalable, which allows users to create custom property types. An example of a plurality of custom properties types, such as customization of an attribute type that allows selection of XML files (using XMLSerializer to objects). CODesmith also allows users to reference and call the specified external assembly in the template and allow the class to generate a template from the external assembly.

CODesmith's syntax is almost the same as ASP.NET. So if you are familiar with ASP.NET, you should understand the template syntax very quickly. You can use C #, VB.NET, or JScript.net languages ​​in the template, and the template can output any ASCII-based language.

With CODesmith Explorer, you can view all templates in the specified folder and you can drag and drop the template to any target that supports dragging text. You can also double-click the execution template file in Windows Explorer. The CodeSmith Explorer window can also be embedded in Visual Studio .NET 2003 as a tool window, allowing you to easily access your template.

CoDesmith has several ways to generate code. Codesmith includes a custom tool for Visual Studio .NET 2003 that includes code generating templates based on multiple properties sets. This custom tool allows the CodeSmith analog model to create multiple special template instances by adding additional property sets to a template. When the custom tool runs again, the change in the template will be reflected in the instance of any template. Codesmith includes a variety of optimized collection templates (such as ArrayList, DictionaryList, HashTable, iDictionary, ILIST, SortedList, Idictionary, ILIST, and STACK), using these templates, can create strong-type collection that avoids boxes and unboxes, their performance is more than .NET Framework The collection type is 1500% high. Codesmith also includes a console-based version that is the same as custom tools, just run from the command line and can be included in any compilation and connection.

Codesmith includes an assembly called SchemaExplorer, using it to access almost all database summary (Schema) details. Accessing this information allows you to generate a variety of code, such as stored procedures, type DataSets, business objects, representation layer code, or any other database based on database sub-information.

CODesmith is published as a free software, its purpose is to create a powerful user and template community. I hope that the programmer can make the programmer less troubles! As an exchange, I want to receive feedback, wrong report, tutorial, documentation, and some useful templates to make our lives easier!

转载请注明原文地址:https://www.9cbs.com/read-116445.html

New Post(0)