Code Generation for .NET

 < Day Day Up > 



Several factors have combined to make .NET a good arena for code generation:

  • The increasing popularity and visibility of code generation in software development

  • The extensible nature of VS .NET, which makes it easy to integrate tools to the IDE

  • The System.CodeDom and System.CodeDom.Compiler namespaces, which make it easy to generate source code

As a result, quite a few code-generation tools are available for .NET developers. After a brief look at the native code-generation features of VS .NET, I’ll introduce some of these tools.

Code Generation in Visual Studio .NET

In some sense, all development environments are code generators; the code that ultimately runs on the computer is not precisely the code that you wrote, but a translation of that code into machine language. But VS .NET also includes built-in source code generation facilities, even though you might not have noticed them. In some cases, VS .NET takes your input and translates it into actual C# or Visual Basic .NET source code, which is then compiled into your project.

For instance, VS .NET offers the ability to build strongly typed DataSet classes, which add the benefits of design-time type checking and IntelliSense to the basic DataSet. You might not have used this feature, so here’s an example to let you see how it works:

  1. Launch VS .NET and create a new C# Windows application.

  2. Right-click on the Project node in Solution Explorer and select Add Add New Item.

  3. In the Add New Item dialog box, select the Data Set template. Name the new item Customers.xsd and click Open.

  4. Open the Server Explorer window (by default, this window auto-hides at the left side of the VS .NET workspace).

  5. Click the Connect to Database toolbar button. Use the Data Link Properties dialog box to connect to the Northwind sample database.

  6. Expand the new database node in the Server Explorer tree view and drag the Customers table to the Customers.xsd design surface. This will create a picture of the table’s schema, as shown in Figure 8.3.

click to expand
Figure 8.3: Building a strongly typed DataSet

At this point, you can use the Customers class in code, with object properties representing the table and column names. For example, this code snippet would retrieve a particular customer ID to a text box:

 SqlConnection cnn = new SqlConnection(connectionString);  SqlDataAdapter da = new SqlDataAdapter();  SqlCommand cmd = cnn.CreateCommand("SELECT * FROM Customers");  Customers cust = new Customers();  da.Fill(cust, "Customers");  txtCustomerID.text = cust.Customers[4].CustomerID; 

Magic? Not quite. If you look at the properties of the Customers.xsd item, you’ll see that it has a Custom Tool property with the value of MSDataSetGenerator. This is a code-generation tool supplied by Microsoft. When you save the Customers.xsd file, VS .NET automatically invokes the tool to generate code based on the XML schema that you specified. You can see the generated code if you like. To do so, click the Show All Files button on the Solution Explorer toolbar. Expand the node for Customers.xsd and you’ll find two files, one of which is named Customers.cs. This file contains all of the C# code to build the strongly typed DataSet class, as well as a warning: “Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.”

VS .NET uses this method to generate some other types of code as well. For example, when you set a web reference to a WSDL file, behind the scenes a code generator is running to provide the proxy class for the web service. Independent software vendors can also ship their own code generators that integrate in this fashion.

.NET Code-Generation Tools

A wide variety of code-generation tools work with .NET languages. These range from general-purpose tools, such as CodeSmith, to full application generators that start with a database and give you back an entire ASP.NET application. There’s no way for me to provide a complete list here; even if I could develop such a list, it would be out of date by the time this book was published. Instead, I’ll briefly mention some of the ones that I’ve tried out, so you can get a sense of the market.

  • AlachiSoft TierDeveloper This tool, with pricing starting at $1295, lets you go from a database (SQL Server, Oracle, or some others) to a full application. It integrates with VS .NET and builds data access classes, COM+ transaction-enabled components, and even complete web applications. It can also automatically generate a web services interface to the data objects. It’s available from www.alachisoft.com.

  • Iron Speed Designer This $495 package builds complete ASP.NET applications from a database and a set of “layout pages,” which are HTML templates containing special tags that tell Iron Speed where to insert its code. The generated pages are completely reentrant (that is, they can be regenerated without losing your custom changes), and a variety of flexible examples are provided with the tool, which is itself an ASP.NET application. It’s available from www.ironspeed.com.

  • m3rlin This is a free and open-source project that uses the ASP.NET engine at runtime to generate code based on templates and XML. Although the documentation is minimal, you can look over the source code to see how it works, and you may find it a useful starting point for your own code-generation projects. You’ll find it at www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=dfef25fa-f545-4289-b9fc-05270ea41fb3.

  • Olero ORM.NET At $495, ORM.NET is designed specifically to generate a data access object layer from your SQL Server database. Although you don’t need to worry about stored procedures when using its object-oriented code from your own applications, it will also generate wrappers for any existing stored procedures in your database. It’s available from www.olero.com/OrmWeb/.

  • Solutions Design LLBLGen Pro Another code generator to get Visual Basic .NET or C# code from SQL Server or Oracle databases, LLBLGen Pro is a “170 tool. It features a lot of places where you can fine-tune the generated code, and it builds a variety of different classes to handle data access for you. It will generate either monolithic classes, or dual classes that separate your custom code from generated code so you can rerun the generation process without losing your customizations. It’s available from www.llblgen.com/defaultgeneric.aspx.

  • Workstate Codify This VS .NET add-in for templated code generation costs $75. It’s unique among the code generators I’ve seen in that it can inject little bits of generated code into the middle of a class. For example, you can generate property syntax even while writing the rest of your class from scratch. It also includes an integrated template editor to make it easy to change templates while your project is still loaded. It’s available from www.workstate.com/codify.

  • Yes! Software CodeCharge Studio 2.2 For $500, CodeCharge Studio provides a very flexible environment for generating web applications, hosted in its own IDE or optionally running as a Microsoft FrontPage add-in. It offers a lot of flexibility in code generation, letting you target ASP, JSP, PHP, Perl, ColdFusion, or ASP.NET. With its wizards and security features, CodeCharge Studio ends up feeling more like an alternative development environment than a code generator. It’s available from www.codecharge.com/index2.php.



 < Day Day Up > 



Coder to Developer. Tools and Strategies for Delivering Your Software
Coder to Developer: Tools and Strategies for Delivering Your Software
ISBN: 078214327X
EAN: 2147483647
Year: 2003
Pages: 118

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net