
196
|
Chapter 6, Speed Hacks
#50 Define Templates for Generating Code
HACK
Again, you can see the class file that was generated in this example was
named Customers.cs. Taking a look at this class, you can see that the xsd.exe
tool created a class named
Customers that inherits from the DataSet class,
thus creating a strongly typed DataSet object:
[Serializable( )]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Diagnostics.DebuggerStepThrough( )]
[System.ComponentModel.ToolboxItem(true)]
public class Customers : DataSet {
Including all of the code here would be excessive—the generated class is
more than 600 lines long, but it is important to note that the Customers
class is not the only class contained in this file. The following classes were
also generated:
CustomerDataTable
CustomerRow
CustomerRowChangeEvent
CustomerRowChangeEventHandler
I would encourage you to examine the generated classes further to better
understand how strongly typed DataSets work.
—Dave Donaldson
HACK
#50
Define Templates for Generating Code Hack #50
Whenever possible, take a shortcut. You can use the CodeSmith custom tool
to generate code from templates that you create.
CodeSmith is a popular .NET code generation tool. The normal CodeSmith
application is a Windows Forms application that allows you to create tem-
plates in an ASP.NET-like syntax, and then generate code using those tem-
plates. The templates can be based ...