Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

SmartCode-An Open Source Code Generator

0.00/5 (No votes)
16 Mar 2009 1  
SmartCode is a template-driven code generator that lets software developers automatically produce programs and components that interact with database system

SmartCode IDE

Release History

A lot of new features.


Introduction

SmartCode is a new open source project on the Kontac web site.

This is the part one of two articles of SmartCode Generator.

What is SmartCode?
SmartCode is an Open Source and template-driven code generator tool (Released under both BSD license and Lesser GPL library license). With the next features:

  • SmartCode operates 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.
  • SmartCode main building blocks are Tables, Views, columns, and top level project definition.
  • In SmartCode , a template is a program that generates text (which is generally code in a programming language), and may be written in C# or VB.NET (or theoretically in any .NET language that supports the creation of dynamic-link libraries, or DLLs).
  • A template library (or library, for short) is a logical grouping of templates. A template takes as input a model and an Table or View to generate the code
  • Allow you to build fully-functional prototypes of web-based applications that interact with database systems.
  • Fast, Because your libraries templates are compiled code and does not need parser script expressions
  • Use Visual Studio to access to SmartCode object Model and debug your templates!
  • Easy to extend, allow you create your custom drivers for access to other databases, and add custom properties to customize the code generation

Background

Despite 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 SmartCode 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 SmartCode 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 reuse

The 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 reuse

Although many methods exist for reusing code, such as

  • Using existing routines and classes, without any modifications.
  • Modifying existing routines and classes to accommodate new requirements.
  • Hand-editing existing code to accommodate a new environment (e.g., data-access code in which one edits table and column names to update a different table), and
  • Buying off-the-shelf software; e.g., ActiveX controls.

It is still the case that automatically generating the code from a high-level specification is the most flexible and reliable strategy.

The SmartCode Environment

The SmartCode environment consists of menus and window panes. This section explains them.

Top menu

The SmartCode top menu offers the following options:

  • File
    New : connect to a database and create a new project.
    Open : locate and open an SmartCode project file.
    Save : save the current project.
    Save As : save the current project under a new file name.
    Exit : quit SmartCode.
  • Project
    Refresh Project: Refresh the loaded project with the info from database.
  • Generation
    Load Library... : load an existing library from Global Assembly Cache.
    View Libraries: display a list of the loaded libraries.
    Generate : bring up the window that displays the current template assignments, allows changing or making new template assignments, and starts the code-generation process.
  • Help
    Help : Contact to me....:)
    About...: displays version and brief license information.

The explorer Pane

The 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.

SmartCode Explorer Pane

The Object Property Pane

The project pane appears on the right-hand-side of the screen when the project (the root node) is selected on the each objects:

SmartCode Property Pane

The project allow the user to view and change objects properties.

SmartCode Projects

A 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 projects

SmartCode project files have an ".scp" extension, They can be opened via the File top-menu Open

Creating new projects

A 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:

SmartCode Project windows Pane

The steps to create a new project are

  • Naming the project.
  • Select the database and Location String, and click on OK.
  • On the next windows choosing database objects.

SmartCode Objects selector dialog

Template Libraries

A 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 Code

Each Template look like this (in VB.NET):
    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 libraries

Template 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.

SmartCode View libraries

Generating Code

The 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.

SmartCode Code generation setting

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 results

When 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:

SmartCode generation setting

Pressing the OK button begins the actual code generation. SmartCode displays progress in the next form :

SmartCode generation setting

Note: When generating files, existing files are automatically overwritten without asking.

Refresh Projects

If 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.

SmartCode Code generation setting

For delete object, simply select the object and click on remove option from Toolbar in Explorer pane.

Project Level Templates

In 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 Interfaces

SmartCode offer the possibility to generate code for User Interfaces, you can setting the control type for each column as shown the next image:

Screenshot - img14.png

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 Libraries

The SmartCode package comes with a Beta core sets of template libraries for generating code in C#, that use stored procedures to access and modify data.

  • Stored Procedures Libraries: These templates generate SQL Server 2000 scripts for creating stored procedures that insert, delete, update, and search for records in tables.
  • Common Data Libraries (TypedDataset): This template generate code for XSD to Render to TypedDataSet objects used to transport data between the tiers
  • Data Tier Libraries: These templates generate code for supporting the data access classes
  • Business Tier Libraries: These templates generate code for supporting the access to Data Tier

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here