http://www.codeproject.com/useritems/SmartCode-Code_Generation.asp |
Release HistoryFull Set of NHibernate Templates, click here. IntroductionSmart Code is a new open source project on the Kontac web site. This is the part one of two articles of Smart Code Generator. What is Smart Code?
BackgroundDespite great advances in software development tools and methodologies, software construction is still very much a detailed and difficult task. This has motivated the development of automatic programming techniques and programming patterns, in order to eliminate or systematize the development of common and repetitive solution architectures. The Smart Code code-generation tool makes possible the automatic generation of programs and components that interact with database systems. It works by reading a database schema, allowing the user to set certain extended properties for tables and columns, and applying templates to each table in order to generate code. The templates are themselves programs that access the Smart Code Object Model to produce tailored programs and components. Although SmartCode comes with a set of core templates, users may edit these or create their own to suit their particular project needs. Moreover, templates may be written in C# or VB.NET (or theoretically in any .NET language that supports the creation of dynamic-link libraries). This is a very powerful paradigm. The benefits of code reuseThe greatest productivity gains in software construction come from reusing components that have already been developed and validated. Moreover, in certain circumstances reusing software may also increase job satisfaction. For example, how many times have us as programmers been asked to develop functionality (including a user interface) for creating, updating, and deleting records from a database table? If there was a way to automate this process, programmers could eliminate some of the most tedious and repetitive work in a typical database project. And they could be able to focus more on ensuring that systems meet their requirements. Reusing software also has organizational benefits. In a typical software development shop, every programmer has her own way of coding database-table maintenance tasks. This makes the software harder to maintain, because one programmer may not necessarily be familiar with the techniques of another. If there was a standard, automated way of producing certain kinds of code, and programmers were trained on the process and on the generated code, anyone could maintain the parts of systems developed in this way. Also, a newly-hired programmer could quickly come to speed on how to maintain this code for all the existing systems that were based on the standard, automated way of producing it. Another organizational benefit, if properly supported, is encouraging the discovery and automation of code patterns and solution architectures. Methods of code reuseAlthough many methods exist for reusing code, such as
It is still the case that automatically generating the code from a high-level specification is the most flexible and reliable strategy. The Smart Code EnvironmentThe Smart Code environment consists of menus and window panes. This section explains them. Top menuThe Smart Code top menu offers the following options:
The explorer PaneThe tables, views, relations and columns objects are located on the left-hand-side of the screen and displays a list of all the these objects in the project, in the form of a tree view.
The Object Property PaneThe project pane appears on the right-hand-side of the screen when the project (the root node) is selected on the each objects:
The project allow the user to view and change objects properties. SmartCode ProjectsA project is a group of tables, views, relations, property settings, template libraries, and template assignments, and is tied to a database. This section explains how to open, create, and save projects, and also how to understand and set project properties. Opening existing projectsSmart Code project files have an ".scp" extension, They can be opened via the File top-menu Open Creating new projectsA project may be created by accessing the File top-menu, Newoption or by clicking on the New in the main toolbar. This starts the project-creation dialog:
The steps to create a new project are
Template LibrariesA template is a program that generates text (which is generally code in a programming language), and may be written in C# VB.NET (or theoretically in any .NET language that can create a dynamic-link library, or DLL). A template library (or library, for short) is a logical grouping of templates. A template takes as input a project and an entity to generate the code. Each template defines a naming convention for the name of the file in which to store the code it generates; this naming convention is typically based on the entity's name. For example, Customer_Sample3.txt Templates, Sample CodeEach Template look like this (in VB.NET):
Collapse
Imports System Imports SmartCode.Template Public Class Sample3 Inherits TemplateBase Public Sub New() MyBase.Name = "Sample3" MyBase.CreateOutputFile = True MyBase.Description = "This template demonstrates using static and dynamic contents." MyBase.OutputFolder = "Samples\Sample3" MyBase.IsProjectTemplate = False End Sub Public Overloads Overrides Function OutputFileName() As String Return Table.Code + "_Sample3.txt" End Function Public Overloads Overrides Sub ProduceCode() WriteLine("This is some static content.") WriteLine("Table: " + Table.Name) For Each column As SmartCode.Model.Column In Table.AllColumns WriteLine("Column: {0}", column.Name) Next WriteLine("This is more static content.") WriteLine() End Sub End Class Add/Remove Template librariesTemplate libraries may be removed from the View Libraies sub menu from the Generation top menu, or by clicking on the icon in the main toolbar. In both cases, a Library Dialog allows for add libraries or selecting the desired template library assembly, and remove this. Generating CodeThe process of generating code consists of assigning templates to Tables, Views and selecting the target folder in which to place the generated files. The process is begun by selecting Generate from the Generation option in the top menu, or by clicking on the icon in the main toolbar. Once the tables and columns properties have been set appropriately, templates are assigned to each of the tables views or project in order to generate code. Assignments are achieved by selecting the entity (or entities, by keeping the Ctrl key pressed) on the left-hand-side, selecting the template (or templates, by keeping the Shift key pressed) on the right-hand-side, and then pressing the right-arrow button in the middle. The left-arrow button is used to remove individually-selected assignments. Code generation and its resultsWhen the Generate button is pressed, a dialog is displayed in which the user is asked to choose the folder in which to place the generated files: Pressing the OK button begins the actual code generation. SmartCode displays progress in the next form : Note: When generating files, existing files are automatically overwritten without asking. Refresh ProjectsIf there are changes in the underlying table or view of an entity, one may wish to refresh its properties. This may be accomplished by clicking the Refresh project Item from Refresh menu This action allow you Add/Update tables and columns from database. For delete object, simply select the object and click on remove option from Toolbar in Explorer pane. Project Level TemplatesIn the last sections we have learn to generate code runing templates assigned to entities, But else in the practics usually we have to generate code for all objects contained in the domain e.g. the list of all tables in the database; With SmartCode we have the option to assign a template to project level, to run only one time, each time that we run the code generation, we can found this template in the second tab called "Project Templates", in the main "Setting Code Generation" dialog Creating User InterfacesSmartCode offer the possibility to generate code for User Interfaces, you can setting the control type for each column as shown the next image:
Build templates to create code for UI is simple, by example the next code teach how to: foreach (Column column in table.AllColumns()) { if (column.Control is SmartCode.Model.Profile.TextBox) { WriteLine(@"<asp:TextBox runat=""server"" ID=""ui{0}""/>", column.Code); } else if (column.Control is SmartCode.Model.Profile.CheckBox) { WriteLine(@"<asp:CheckBox runat=""server"" ID=""ui{0}""/>", column.Code); } else ............ } The Core Template LibrariesThe Smart Code package comes with a Beta core sets of template libraries for generating code in C#, that use stored procedures to access and modify data.
About Danilo Mendez |