Click here to Skip to main content
15,892,674 members
Articles / Web Development / ASP.NET

Template-Based Code Generation with SmartCode

Rate me:
Please Sign up or sign in to vote.
4.82/5 (35 votes)
25 Dec 20067 min read 101.1K   3.5K   121  
SmartCode is a template based code generator.This tutorial describes the process of building a templates to SmartCode
/*
 * Copyright � 2005-2006 Danilo Mendez <danilo.mendez@kontac.net>
 * Adolfo Socorro <ajs@esolutionspr.com>
 * www.kontac.net 
 * All rights reserved.
 * Released under both BSD license and Lesser GPL library license.
 * Whenever there is any discrepancy between the two licenses,
 * the BSD license will take precedence.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using SmartCode.Studio.Database;
using SmartCode.Model;
using SmartCode.Studio.Database.Info;

namespace SmartCode.Studio.Model
{
    public class BuildDomain
    {
        private BuildDomainDlg sender = null;
        private Delegate senderDelegate = null;

        private Domain domain;

        public Domain Domain
        {
            get { return domain; }
        }

        private BuildDomain()
        {
        }

        public BuildDomain(BuildDomainDlg sender, Delegate senderDelegate)
        {
            this.sender = sender;
            this.senderDelegate = senderDelegate;
        }

        /// <summary>
        /// Method for ThreadStart delegate
        /// </summary>
        public void RunProcess()
        {
            Thread.CurrentThread.IsBackground = true; //make them a daemon
            LocalRunProcess();
        }

        /// <summary>
        /// Local Method for the actual work.
        /// </summary>
        private void LocalRunProcess()
        {
            try
            {
                domain = new Domain(sender.Driver.ConnectionInfo);

                int totalOfMessages = sender.Tables.Length + sender.Views.Length;
                int i = 0;
                for (; i < sender.Tables.Length; i++)
                {
                    string tableName = sender.Tables[i];
                    string message = "Loading objects for " + tableName;
                    LoadEntityInfo(tableName, true);

                    sender.BeginInvoke(senderDelegate, new object[] { totalOfMessages, i, message, false });
                }

                int j = 0;
                for (; j < sender.Views.Length; j++)
                {
                    string message = "Loading objects for " + sender.Views[j];
                    LoadEntityInfo(sender.Views[j], false);
                    sender.BeginInvoke(senderDelegate, new object[] { totalOfMessages, i + j, message, false });
                }
                sender.BeginInvoke(senderDelegate, new object[] { totalOfMessages, 0, "Loading Constraints", false });

                totalOfMessages = sender.Tables.Length;
                i = 0;
                for (; i < sender.Tables.Length; i++)
                {
                    string tableName = sender.Tables[i];
                    string message = "Creating references for " + tableName;

                    LoadConstraints(tableName);
                    sender.BeginInvoke(senderDelegate, new object[] { totalOfMessages, i, message, false });
                }


                sender.BeginInvoke(senderDelegate, new object[] { totalOfMessages, i, "Domain created successful", true });
                sender.DialogResult = DialogResult.OK;
                sender.Close();

            }
            catch  
            {
                Thread.CurrentThread.Abort();
                sender.DialogResult = DialogResult.Cancel;
                throw;

            }
            finally
            {
            }
        }

        private void LoadConstraints(string tableName)
        {
            Table childTable = this.domain.ConnectionInfo.FindTable(tableName);
            if (childTable != null)
            {
                SchemaExtractor extractor = sender.Driver.Extractor;

                ConstraintInfo[] constraintsInfo = extractor.GetConstraints(tableName);
                foreach (ConstraintInfo constraint in constraintsInfo)
                {
                    Table parentTable = this.domain.ConnectionInfo.FindTable(constraint.PrimaryKeyTable);
                    if (parentTable != null)
                    {
                        // ConstraintInfo.PrimaryKeyTableColumns and ConstraintInfo.Columns are RelatedImageListAttribute 1-1
                        Reference reference = new Reference(constraint.Name, parentTable, childTable);
                        reference.OnDeleteCascade = constraint.OnDeleteCascade;
                        reference.OnUpdateCascade = constraint.OnUpdateCascade;

                        for (int i = 0; i < constraint.PrimaryKeyTableColumns.Length; i++)
                        {
                            Column parentColumn = parentTable.FindColumn(constraint.PrimaryKeyTableColumns[i]);
                            Column childColumn = childTable.FindColumn(constraint.Columns[i]);
                            reference.AddNewJoin(parentColumn, childColumn);
                        }
                        parentTable.AddOutReference(reference);
                        childTable.AddInReference(reference);
                    }
                }
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="isTable"></param>
        private void LoadEntityInfo(string tableName, bool isTable)
        {
            try
            {
                Table table = new Table(this.domain, tableName);
                table.IsTable = isTable;

                ColumnInfo[] columnsInfo = sender.Driver.Extractor.GetColumns(tableName);
                foreach (ColumnInfo columnInfo in columnsInfo)
                {
                    Column column = new Column(columnInfo.Name, table);
                    column.SqlType = columnInfo.SqlType;

                    column.NetDataType = columnInfo.NetDataType;
                    column.Length = columnInfo.Size;
                    column.OriginalSQLType = columnInfo.OriginalSQLType;
                    column.IsIdentity = columnInfo.AutoIncrement;
                    column.IsRequired = !columnInfo.AllowNull;
                    column.Scale = columnInfo.Scale;
                    table.AddColumn(column);
                }

                this.domain.ConnectionInfo.AddTable(table);

                KeyInfo[] keysInfo = sender.Driver.Extractor.GetKeys(tableName);
                foreach (KeyInfo keyInfo in keysInfo)
                {
                    //Bug Fix
                    if (keyInfo.TableName == table.Name)
                    {
                        Column keyColumn = table.FindColumn(keyInfo.ColumnName);
                        if (keyColumn != null)
                        {
                            keyColumn.IsPrimaryKey = true;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Web Developer
United States United States
Danilo is the creator of SmartRules, a Business Rules Engine. He is an industry consultant working primarily with companies interested in implementing dynamic rules programming concepts to add flexibility to their architectures on web, CE, and desktop platforms. He operates his own website, Kontac, where you will find more information.

To contact Danilo, email him at danilo.mendez@gmail.com.

Comments and Discussions