http://www.microsoft.com/china/msdn/library/langtool/vsdotnet/realworld1102004.mspx
Code generator and .net
Release Date: 12/23/2004
| Update Date: 12/23/2004
Pierre Couzywinwise
Summary: Code Builder is part of your daily life, even if you don't realize this. Pierre Couzy illustrates how to use them in your project.
This page
Introduction Tools and sample version control summary
Introduction
Suppose you work in a company that rule everything by DBA: You can't generate an application that is just "going to Oracle to take some records". You can only rely on the stored procedure because there is a security layer at this level.
Generating applications typically involve the following steps:
1. Create a batch of stored procedures. 2. Create a C # class that can communicate with the stored procedure. 3. Create a higher level of class that will manage the Microsoft ASP.NET form or Microsoft Windows form or form another layer.
Steps 1 and Step 2 are closely related: they share a large number of structures, they consume something generated by another step, and so on. The problem is that you have two different languages (in this example, PL-SQL and C #) and cannot reference another language from one of the languages.
Do you say that this is not big? The first developers in the team will keep everything normally because he understands the essence of .NET, PL / SQL, and security models. He may write two things: first is a set of help classes, then a valid example - other developers will use these helper classes, copy / paste effective examples, and modify them for them.
If you have worked for a long enough time in the software industry, you will know what will happen next: With the increase in the demand, the helpor class will slowly become bigger, and some will be out of time. Of course, no one dares to modify them, as this may destroy the old project. At the same time, developers will reuse valid examples that are no longer synchronized with new rules, and they will have no way to understand which parts are still use, and which parts can be discarded without risk. You will fall into a chaotic (copy / paste programming).
There are a lot of ways to overcome this complexity, and I will introduce a useful approach you may have ignored - code generator. In fact, you have already used them, even if you don't realize this: Whenever you create a new ASPX page, the hidden mechanism converts it to the C # or Microsoft Visual Basic class. When you reference COM components, web services or even data structures (Type Data Sets) in your project - there is a class that is automatically generated, which hides the complexity you don't want to understand.
Back to top
Tools and examples
The first code generator you will encounter is just a tool for hiding complexity. They don't allow you to explain how to generate a code and don't let you change the generated code (they will clear the updates you).
For example, "Add Web Reference Wizard" does not even display code inserted into the project (unless you request it).
Figure 1. Displayed as a code generated by the web reference
The Windows Form Designer is also a code generator. Here you can view the code, but it is best not to move it.
#Region Windows Form Designer Generated Code
///
/// Required Method for Designer Support - Do Not Modify
/// the contents of this method with the code editor./// summary>
Private vidinitiRizeComponent ()
{
This.button1 = new system.windows.Forms.Button ();
THIS.SUSPENDLAYOUT ();
Let us further explore the issue further: The code generator can provide the main trunk of the format specification for you to add your own code. You may know the Reflector, but you know some of the Reflector's add-in (http://www.dotnetwi.org/default.aspx?tabid=52) containing the code generator? They allow you to do the following:
Figure 2. Generate code using Reflector
When you press the "Generate" button, a new file will be created:
// generated by refly
Namespace mytestnamespace
{
Using system;
///
/// summary>
///
[TestFixTure ()]
Public Class SomebusinessClassTest
{
///
///
Private somebusinessClass_SomebusinessClass = NULL;
///
///
Public Virtual SomebusinessClassClass SomebusinessClass
{
get
{
Return this._somebusinessclass;
}
}
///
///
///
///
/// / * 0 * / return 0x2a;
/// code>
/// remarks>
[Test ()]
[Ignore ()]
Public Virtual Void Estimatesomenumber0 ()
{
Throw new system.notimplementException ();
}
///
///
///
///
/// / * 0 * / return false;
/// code>
/// remarks>
[Test ()]
[Ignore ()]
Public virtual void issomethingalReadyindatabase0 ()
{
Throw new system.notimplementException ();
///
///
[Setup ()]
Public Virtual Void Setup ()
{
Throw new system.notimplementException ();
}
///
///
[Teardown ()]
Public Virtual Void TEARDOWN ()
{
Throw new system.notimplementException ();
}
}
}
You don't know anything about "Unit Test", but you can still generate a valid test class. The difference from the previous example is that this time you must be encoded internally in the generated class. However, we still can't create our own template.
The next step is to create our own code generator. All what we need is a common information according to a set of common information (eg, the name of the column to be obtained, to request information, etc.) to generate text files (C #, PL-SQL, etc. Wait a way. Of course, you can't generate all things, therefore need a tool that can generate custom trunk, and a developer will then add specific implementation details.
This tool can be Perl, Microsoft Vbscript or normal legacy ASP (in this article) - after all, it is a good tool that can generate text files based on parameters. You may use ASP to generate an HTML file, but it also applies to generating Microsoft Excel or CSV, WML - therefore, why don't you use it to generate T-SQL or C #?
Your ASP file may be as follows:
<% @ Language = VBScript%>
<% IF Request ("generate"). count = 0 THEN%>