CodeSmith Overview

May 25, 2021 07:00 CodeSmith

Table of contents


The development tutorial for hibernate, which was introduced earlier, mentions that hibernate's corresponding ORM tool on the .Net platform is NHibernate, and you can't help but mention CodeSmith with NHibernate.

CodeSmith is a template-based code generation tool that uses ASP.NET-like syntax to generate any type of code or text. U nlike many other code generation tools, CodeSmith does not require you to subscribe to a specific application design or architecture. W ith CodeSmith, you can build anything from a simple collection of strong types to a complete application. W hen you build an application, you often need to repeat certain tasks, such as writing data access code or building custom collections. C odeSmith is especially useful at these times because you can write templates to automate these tasks, which not only increases your productivity, but also automates the most tedious tasks. CodeSmith comes with a number of templates, including those that correspond to all .NET collection types and those used to build stored procedures, but the real power of the tool is the ability to create custom templates.

CodeSmith can be downloaded website, and the personal development version costs about $300, which I personally think is worth the money.

Let's use an example of how using CodeSmith can greatly reduce the amount of work on your program code, and for database applications, although database tables vary, the basic process is to define database tables, design the classes that correspond to tables, and then use ADO or SQL statements to access the database, create corresponding class objects, and so on.

Using the templates provided by CodeSmith, you can automatically generate the database-related code above almost without having to write a line of code manually.

This example uses Visual Studio 2010, which Codesmith installed with visual Studio plug-in support. T he sample database you're using is also Sales, see Hibernate Development Tutorial (2): Get ready to start.

Because of the MySQL database, you need to download the MySQL .Net library. Sql Server is available directly.

  1. Create a command-line app solution.

  2. Add a MySQL data source with CodeSmith's Schema Explorer

CodeSmith Overview

The Connection String here is: SERVER-localhost; DATABASE-sales; U ID=username; P ASSWORD=password; (Modify the parameters yourself according to your own server)

After the addition is successful, table definitions for the connected database are displayed in Schema Explorer, and so on

CodeSmith Overview

  1. Add a Sales.Data Class Library in this solution and then add a CodeSmith project Item to the project

CodeSmith Overview

  1. Click Sales.csp to use the right mouse button Context Menu

CodeSmith Overview

You can add a template for generating code to your project with Add Output, using three templates under CodeSmith's own PLINQO-NH-CSharp.

The template path is: \Users\...\Documents\CodeSmith Generator\Samples\v6.5\Templates\Frameworks\PLINQO-NH\CSharp\

Adding three templates separately, SourceDatabase selects the Sales database added in Schema Explorer, and the other properties use the default values.

CodeSmith Overview

The three templates are

  • Hbms generates the hbm .xml mapping file
  • The Entities build corresponds to the database table. Net class definition
  • Queries generates the class for the query data

The code is then generated through Sales.csp's Generate code.

You can see that CodeSmith automatically generates a lot of code and adds the required references.

CodeSmith Overview

The classes used to access the data are all generated by CodeSmith without having to write a line of code themselves.

  1. If you use the generated code to access the database, modify the main function of the program .cs the command line app and print out the names of all the Customer.
var salesDataContext = new SalesDataContext();
foreach (var s in salesDataContext.Customer.ToList()) 
    Console.WriteLine(s.FirstName+" " + s.LastName);

Use Customer in SalesDataContext to query objects. Then enumeral each Customer object in the list and print out FirstName and Last Name. You can see that the code automatically completes read access to the database.

CodeSmith Overview

As you can see from this example, using CodeSmith can greatly reduce the amount of manual code, and the general steps used are

  1. Choosing to use the right template, CodeSmith comes with a number of commonly used templates with the development package, and if you can't find the right template, CodeSmith supports custom templates.
  2. Select the appropriate parameter settings for the template.
  3. Auto-generated code (can be any type of code, C, Java, . . XML text, etc.)

The basic approach used by CodeSmith, which is at the heart of the template, is detailed later, so the focus is on the design and use of the template.