Click here to Skip to main content
15,886,873 members
Articles / Database Development / SQL Server

Template based code generation

Rate me:
Please Sign up or sign in to vote.
4.48/5 (10 votes)
10 Mar 2005CPOL9 min read 70.5K   1.6K   56  
An article about template based code generation and a demonstration of how to quickly generate a wrapper class for stored procedures.
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace TemplateCodeGenerator
{
	/// <summary>
	/// Summary description for Editor.
	/// </summary>
	public class Editor : System.Windows.Forms.Form
	{
        private System.Windows.Forms.MainMenu mainMenu;
        private System.Windows.Forms.MenuItem menuFile;
        private System.Windows.Forms.MenuItem menuFileSave;
        private System.Windows.Forms.RichTextBox richTextBox;
        private System.Windows.Forms.MenuItem menuFileExit;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Editor()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.mainMenu = new System.Windows.Forms.MainMenu();
            this.menuFile = new System.Windows.Forms.MenuItem();
            this.menuFileSave = new System.Windows.Forms.MenuItem();
            this.menuFileExit = new System.Windows.Forms.MenuItem();
            this.richTextBox = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            // 
            // mainMenu
            // 
            this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                     this.menuFile});
            // 
            // menuFile
            // 
            this.menuFile.Index = 0;
            this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                     this.menuFileSave,
                                                                                     this.menuFileExit});
            this.menuFile.Text = "File";
            // 
            // menuFileSave
            // 
            this.menuFileSave.Index = 0;
            this.menuFileSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
            this.menuFileSave.Text = "Save";
            this.menuFileSave.Click += new System.EventHandler(this.menuFileSave_Click);
            // 
            // menuFileExit
            // 
            this.menuFileExit.Index = 1;
            this.menuFileExit.Text = "E&xit";
            this.menuFileExit.Click += new System.EventHandler(this.menuFileExit_Click);
            // 
            // richTextBox
            // 
            this.richTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richTextBox.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.richTextBox.Location = new System.Drawing.Point(0, 0);
            this.richTextBox.Name = "richTextBox";
            this.richTextBox.ShowSelectionMargin = true;
            this.richTextBox.Size = new System.Drawing.Size(768, 369);
            this.richTextBox.TabIndex = 0;
            this.richTextBox.Text = "richTextBox";
            this.richTextBox.WordWrap = false;
            this.richTextBox.TextChanged += new System.EventHandler(this.richTextBox_TextChanged);
            // 
            // Editor
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(768, 369);
            this.Controls.Add(this.richTextBox);
            this.Menu = this.mainMenu;
            this.Name = "Editor";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Template Code Generator";
            this.Closing += new System.ComponentModel.CancelEventHandler(this.Editor_Closing);
            this.Load += new System.EventHandler(this.Editor_Load);
            this.ResumeLayout(false);

        }
		#endregion

        
        private void Editor_Load(object sender, System.EventArgs e)
        {
            string configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            string appFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            configFile = Path.Combine(appFolder, Path.GetFileName(configFile));
            if(!File.Exists(configFile))
                configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

            StreamReader reader = new StreamReader(configFile);
            using(reader) 
            {
                this.richTextBox.Text = reader.ReadToEnd();
            }

            this.loaded = true;
        }

        void SaveConfiguration(bool prompt)
        {   
            if(!this.textChanged)
                return;

            DialogResult result = DialogResult.Yes;

            if(prompt)
            {
                result = System.Windows.Forms.MessageBox.Show(this,
                    "Save configuration before exit?", this.Text, 
                    System.Windows.Forms.MessageBoxButtons.YesNo);
            }

            if(result == DialogResult.Yes)
            {
                string configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                string appFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                configFile =  Path.Combine(appFolder, Path.GetFileName(configFile));
                StreamWriter writer = new StreamWriter(configFile);
                using(writer)
                {
                    writer.Write(this.richTextBox.Text);
                }
                
            }
            this.textChanged = false;
        }


        private void menuFileSave_Click(object sender, System.EventArgs e)
        {
            SaveConfiguration(false);
        }

        
        bool textChanged;
        bool loaded;
        private void richTextBox_TextChanged(object sender, System.EventArgs e)
        {
            if(loaded)
                textChanged = true;
        }

        private void menuFileExit_Click(object sender, System.EventArgs e)
        {
            SaveConfiguration(true);
            
            this.Close();
        }

        private void Editor_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            SaveConfiguration(true);
        }
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I am a consultant, trainer, software archtect/engineer, since the early 1980s, working in the greater area of Boston, MA, USA.

My work comprises the entire spectrum of software, shrink-wrapped applications, IT client-server, systems and protocol related work, compilers and operating systems, and more ....

I am currently focused on platform development for distributed computing in service oriented data centers.

Comments and Discussions