|
|
YOUR FEEDBACK
|
TOP MICROSOFT .NET LINKS BizTalk
My Generation: A Fresh Approach to Code Generation
The makings of a great code generator
By: Mike Griffin
Digg This!
My Generation is a very flexible, powerful code generator and it's 100% free. MyGeneration executes templates to transform database meta-data or schemas into object-oriented architectures, stored procedures, XML, HTML, ASP.NET pages, and more. Templates can be written in VBScript, JScript, C#, and VB.NET. All four template languages provide the ability to toggle between literal content and executable code via the standard <% %> tags. The two scripting languages have full access to COM via CreateObject() and the two .NET languages have full access to the .NET namespaces. Anything is possible! Templates are a very flexible way to generate code, as opposed to wizards or other fixed user interfaces that would hem you in and force you to work in a certain way. As its main source of input, MyGeneration uses the metadata within your DBMS system. This metadata is, in many ways, an excellent description of your application. This is especially true when creating n-tiered applications that have a data-access layer and a business logic layer. MyGeneration can easily generate your particular n-tiered architecture or you can use the architecture shipped with MyGeneration which is named dOOdads (see Figure 1). MyGeneration supports Microsoft SQL, Microsoft Access, Oracle, IBM DB2, MySQL (instances running on Microsoft platforms), PostgreSQL, FireBird, Interbase, SQLite, and VistaDB. MyGeneration ships with many sample templates, including templates that can generate the dOOdads architecture in C# or in VB.NET. However, you can think of MyGeneration as a clean sheet of paper ready to record your thoughts and generate whatever you need. MyGeneration Software even hosts an online template library that allows its users to register and publish templates to be shared with other users. Some of the templates published include templates that generate ASPX pages, ASCX controls, Gentle.NET classes, XML Documents, SQL Migration scripts, and more. Templates One of the most unique features of MyGeneration is that it allows you to create your own user interface for your template within the template itself. A MyGeneration template consists of two parts: the template body and the template user interface. MyGeneration first executes the user interface section of the template and then ensures that the user selections are made available to the body portion of the template, which is executed next. In Figure 2 the template scripting engine (or body) is set to Microsoft Script, which means it will be written in either JScript or VBScript. However, the GUI scripting engine is set to .NET Script, which means it will be written in either C# or VB.NET. Notice how you can mix and match engines and languages to suit your needs within the same template. Using ".NET Script" as your user interface language will grant you full access to the System.Windows.Forms namespace. The code used to create the user interface shown in Figure 3 was written in VBScript and is actually embedded in the template itself. Controls are added to the template portion of a script as in the following: ui.AddLabel "lblNamespace", "Namespace: ", "Provide your objects namespace." Later, in the body of the template, the input is gathered as in the following: Dim namespace There is a finite set of controls that you can use in your VBScript or JScript user interfaces. For instance, the DataGrid is not available. However, remember that MyGeneration's .NET template languages will effectively grant you full access to the System.Windows.Forms namespace, allowing you to create your own fully functional user interface complete with DataGrids and third party controls. Are you thinking of creating your own Object Relational Mapping (ORM) tool and an accompanying architecture? MyGeneration supplies everything that you need. Mapping DBMS Types MyGeneration Plugins <configuration> The assembly attribute references the DLL and the classpath attribute is the .NET class that will be instantiated to represent the intrinsic object. The varname attribute is the name of the variable as it will appear to the template. The plug-in architecture offers all kinds of opportunities. The MyMeta Plug-in The MyMeta plug-in was written in C# and is exposed via COM interop to give VBScript and JScript access to the metadata. The metadata provided by the MyMeta plug-in is exposed via interfaces. For instance, a column in a table or a view is represented by the IColumn interface. The IColumn interface remains the same across all DBMS systems. As mentioned earlier, MyGeneration ships with a free .NET architecture named dOOdads. A single template creates dOOdad business entities for Microsoft SQL, Microsoft Access, Oracle, and Firebird. This is possible because the MyMeta plugin makes all DBMS systems appear the same through these interfaces. The interfaces supported by MyMeta are as follows: IDatabase, ITable, IView, IColumn, IIndex, IForeignKey, IProcedure, IParameter, IResultColumn, and IDomain. Each of these interfaces also has a corresponding collection. For instance, IColumns is the collection for IColumn. Preserving Hand-Coded Modifications Suppose you want to generate, insert, and update stored procedures for all of the tables in your database. This is no problem for MyGeneration. However, suppose that a handful of these stored procedures need to write to an audit table when a record is updated. There is no metadata lying around in your DBMS system (accessible in your template) that will relay this requirement. One option is to simply hand code it into the generated stored procedures. But what if you later change a column in one of your tables, and you have to regenerate a stored procedure that you have hand edited? Will you lose your changes? Certainly not. You would protect any hand-coded areas with a Preserve Region. The first step is to register your Preserve Region format with your template. This is done by giving it the file name of the original file on disk (usually derived from the table name during script execution) and the comment begin and end blocks. Everything between the comment begin and end blocks will ultimately be preserved. In this case, you are trying to preserve hand-coded comments in an SQL Server stored procedure. Thus, you would use SQL's /* */ syntax. The Preserver Region is registered as follows: output.setPreserveSource("proc_" + objTable.Name + "_Update", "/o", "o/") After you have registered the Preserve Region format, you are ready to use one or more Preserve Regions within your template. In this case the name of the Preserve Region is "AuditTrail." To actually declare a Preserve Region you would use this syntax in the spot within the template where you want to make your hand edits: output.preserve("AuditTrail") This will result in the following code being injected into your output: /o PRESERVE_BEGIN AuditTrail o/ You can now edit your newly generated stored procedure on disk and add code within the Preserve Region. Notice that this is done in the generated code itself, not in the template: /o PRESERVE_BEGIN AuditTrail o/ The next time you regenerate the stored procedure, everything within and including the Preserve Region will be preserved. You can even move the output.preserve("AuditTrail") region around in your template, and your custom code will move to the new location the next time you regenerate. If you remove the output.preserve("AuditTrail") region from your template, the next time you regenerate that region will be gone. There is an example of this in the "HTML Database Report" template provided with MyGeneration, but it uses the HTML comment syntax. Project Files You can select any template or folder in the project tree and regenerate from that point onward. To regenerate the entire project, you simply select the root and execute. There is also a command line utility that can execute a project file or an individual template. Conclusion MICROSOFT .NET LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
||||||||||||||||||||||||||||||||||