Click here to Skip to main content
15,885,365 members
Articles / Database Development / SQL Server

Business Objects for CodeSmith

Rate me:
Please Sign up or sign in to vote.
4.38/5 (17 votes)
23 Nov 2004Ms-PL6 min read 143.9K   719   105  
Save time when developing large applications, by using code generation for your business objects.
//---------------------------------------------------------------------------------------
// Copyright Notice
// This file contains proprietary information of The Austin Conner Group.
// Copying or reproduction without prior written approval is prohibited.
// Copyright (C) 2004 The Austin Conner Group. All rights reserved.
// 
// The above copyright notice and this permission notice shall be included in all 
// copies or substantial portions of the Software.
//
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met: 
// 
// * Redistributions of source code must retain the above copyright notice, this list 
//   of conditions and the following disclaimer. 
//
// * Redistributions in binary form must reproduce the above copyright notice, this list 
//   of conditions and the following disclaimer in the documentation and/or other materials 
//   provided with the distribution. 
//
// * Neither the name of The Austin Conner Group nor the names of its contributors may be 
//   used to endorse or promote products derived from this software without specific prior 
//   written permission. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 
// OR OTHER DEALINGS IN THE SOFTWARE.
//---------------------------------------------------------------------------------------
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.ComponentModel;
using CodeSmith.Engine;


namespace BusnessObjects.Templates
{
	/// <summary>
	/// Summary description for Builder.
	/// </summary>
	public class Builder : BusnessObjects.Templates.BaseTemplate
	{
		#region Private Properties
		/// <summary/>
		private string outputDirectory;
		/// <summary/>
		private CodeTemplate businessClassTemplate;
		/// <summary/>
		private CodeTemplate businessColletionTemplate;
		/// <summary/>
		private CodeTemplate dataClassTemplate;
		/// <summary/>
		private CodeTemplate dataObjectTemplate;
		/// <summary/>
		private CodeTemplate baseDataClassTemplate;
		/// <summary/>
		private CodeTemplate baseObjectClassTemplate;
		/// <summary/>
		private string xmlFile = String.Empty;
		/// <summary/>
		private XmlDocument xmlObjectDocument = null;	
		/// <summary/>
		private bool generateBaseClasses = false;	
		/// <summary/>
		[Browsable(false)]
		public String BuilderOutput = String.Empty;
		/// <summary/>
		[Browsable(false)]
		public String SQLOutput = String.Empty;

		/// <summary>
		/// XML Document that the classes will be generated from.
		/// </summary>
		[CodeTemplateProperty(CodeTemplatePropertyOption.Required),
		Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
		Category("1. Common Required Template Options"), 
		Description("Required - The XML Document that the classes will be generated from.")]
		public string XmlFile
		{
			get
			{
				return this.xmlFile;
			}
			set
			{
				this.xmlFile = value;
			}
		}


		/// <summary>
		/// The Base Directory for Code Output.
		/// </summary>
		[CodeTemplateProperty(CodeTemplatePropertyOption.Required),
		Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
		Category("3. Common Output Options"), 
		Description("Required - The Base Directory for Code Output.")]
		public string OutputDirectory
		{
			get 
			{
				return outputDirectory;
			}
			set
			{
				if(!Directory.Exists(outputDirectory))
				{
					Directory.CreateDirectory(value);
				}
				outputDirectory = value;
			}
		}

		/// <summary>
		/// Generated base classes.
		/// </summary>
		[CodeTemplateProperty(CodeTemplatePropertyOption.Required),
		Category("1. Common Required Template Options"), 
		Description("Optional - Generated base classes.")]
		public bool GenerateBaseClasses
		{
			get
			{
				return generateBaseClasses;
			}
			set
			{
				generateBaseClasses = value;
			}
		}

		/// <summary/>
		[Browsable(false)]
		public XmlDocument XmlObjectDocument
		{
			get
			{
				if( xmlObjectDocument == null )
				{
					if( ! System.IO.File.Exists( XmlFile ) )
						throw new ApplicationException("Xml Document File doesn't exist");
					xmlObjectDocument = new XmlDocument();
					xmlObjectDocument.Load( XmlFile );
				}
				return xmlObjectDocument;
			}
		}

		/// <summary/>
		[Browsable(false)]
		private CodeTemplate ClassTemplate
		{
			get 
			{ 
				if(businessClassTemplate == null)
					businessClassTemplate = CompileTemplate(CodeTemplateInfo.DirectoryName + "ClassGenerator.cst");
				return businessClassTemplate;			
			}
		}
		/// <summary/>
		[Browsable(false)]
		private CodeTemplate CollectionTemplate
		{
			get 
			{ 
				if(businessColletionTemplate == null)
					businessColletionTemplate = CompileTemplate(CodeTemplateInfo.DirectoryName + "CollectionGenerator.cst");
				return businessColletionTemplate;			
			}
		}
		/// <summary/>
		[Browsable(false)]
		private CodeTemplate DataClassTemplate
		{
			get 
			{ 
				if(dataClassTemplate == null)
					dataClassTemplate = CompileTemplate(CodeTemplateInfo.DirectoryName + "DataClassGenerator.cst");
				return dataClassTemplate;			
			}
		}
		/// <summary/>
		[Browsable(false)]
		private CodeTemplate DataTemplate
		{
			get 
			{ 
				if(dataObjectTemplate == null)
					dataObjectTemplate = CompileTemplate(CodeTemplateInfo.DirectoryName + "DataGenerator.cst");
				return dataObjectTemplate;			
			}
		}
		/// <summary/>
		[Browsable(false)]
		private CodeTemplate BaseDataClassTemplate
		{
			get 
			{ 
				if(baseDataClassTemplate == null)
					baseDataClassTemplate = CompileTemplate(CodeTemplateInfo.DirectoryName + "BaseDataClass.cst");
				return baseDataClassTemplate;			
			}
		}
		/// <summary/>
		[Browsable(false)]
		private CodeTemplate BaseObjectClassTemplate
		{
			get 
			{ 
				if(baseObjectClassTemplate == null)
					baseObjectClassTemplate = CompileTemplate(CodeTemplateInfo.DirectoryName + "BaseObjectClass.cst");
				return baseObjectClassTemplate;			
			}
		}

		#endregion

		#region Constructor(s)

		public Builder()
		{
		}

		#endregion
		
		#region Public Methods

		/// <summary>
		/// 
		/// </summary>
		public string Build()
		{

			XmlElement root = XmlObjectDocument.DocumentElement;
			XmlNodeList classList = root.GetElementsByTagName("class");
			// Process all the classes
			foreach(XmlNode classNode in classList)
			{
				// Extract Class Name
				string TargetClass = classNode.Attributes["name"].Value;

				ArrayList _classNames = new ArrayList();
				foreach(XmlNode classNames in classList)
				{

					// Extract Class Names

					if(classNames.Attributes["name"].Value != TargetClass)
					{
						_classNames.Add(classNames.Attributes["name"].Value);
					}
				}
		
				ClassNames =  _classNames;

				// Extract the properties
				string propertiesXML = classNode.InnerXml;
				XmlDocument xmlDoc = new XmlDocument();
				xmlDoc.LoadXml(propertiesXML);
				PropertyList = xmlDoc.GetElementsByTagName("property");	

				ArrayList _foreignKeys = new ArrayList();
				foreach(XmlNode propertyNode in PropertyList)
				{
					if(isClassName(propertyNode.Attributes["name"].Value))
					{
						_foreignKeys.Add(propertyNode.Attributes["name"].Value);
					}
				}
				// Call the Class Builders
				this.ClassTemplate.SetProperty("CompanyName", CompanyName);
				this.ClassTemplate.SetProperty("DeveloperName", DeveloperName);	
				this.ClassTemplate.SetProperty("RootNamespace", RootNamespace);	
				this.ClassTemplate.SetProperty("ObjectNamespace", ObjectNamespace);	
				this.ClassTemplate.SetProperty("BusinessLogicNamespace", BusinessLogicNamespace);	
				this.ClassTemplate.SetProperty("DataAccessNamespace", DataAccessNamespace);	
				this.ClassTemplate.SetProperty("TargetClass", TargetClass);	
				this.ClassTemplate.SetProperty("PropertyList", PropertyList);	
				this.ClassTemplate.SetProperty("ClassNames", _classNames);	
				this.ClassTemplate.SetProperty("ForeignKeys", _foreignKeys);	
	
				RenderToFile(ObjectNamespace  + "\\" + BusinessLogicNamespace , TargetClass + ".cs", ClassTemplate);
				BuilderOutput += TargetClass + " Built \n";

				this.CollectionTemplate.SetProperty("CompanyName", CompanyName);
				this.CollectionTemplate.SetProperty("DeveloperName", DeveloperName);	
				this.CollectionTemplate.SetProperty("RootNamespace", RootNamespace);	
				this.CollectionTemplate.SetProperty("ObjectNamespace", ObjectNamespace);	
				this.CollectionTemplate.SetProperty("BusinessLogicNamespace", BusinessLogicNamespace);	
				this.CollectionTemplate.SetProperty("DataAccessNamespace", DataAccessNamespace);	
				this.CollectionTemplate.SetProperty("TargetClass", TargetClass);	

				RenderToFile(ObjectNamespace  + "\\" + BusinessLogicNamespace , TargetClass + "Collection.cs", CollectionTemplate);
				BuilderOutput += TargetClass + "Collection Built \n";

				this.DataClassTemplate.SetProperty("CompanyName", CompanyName);
				this.DataClassTemplate.SetProperty("DeveloperName", DeveloperName);	
				this.DataClassTemplate.SetProperty("RootNamespace", RootNamespace);	
				this.DataClassTemplate.SetProperty("ObjectNamespace", ObjectNamespace);	
				this.DataClassTemplate.SetProperty("BusinessLogicNamespace", BusinessLogicNamespace);	
				this.DataClassTemplate.SetProperty("DataAccessNamespace", DataAccessNamespace);	
				this.DataClassTemplate.SetProperty("TargetClass", TargetClass);	
				this.DataClassTemplate.SetProperty("PropertyList", PropertyList);	
				this.DataClassTemplate.SetProperty("ClassNames", _classNames);	
				this.DataClassTemplate.SetProperty("ForeignKeys", _foreignKeys);	

				RenderToFile(ObjectNamespace  + "\\" + DataAccessNamespace , TargetClass + "Data.cs", DataClassTemplate);
				BuilderOutput += TargetClass + "Data Built \n";
				
				this.DataTemplate.SetProperty("CompanyName", CompanyName);
				this.DataTemplate.SetProperty("DeveloperName", DeveloperName);	
				this.DataTemplate.SetProperty("RootNamespace", RootNamespace);	
				this.DataTemplate.SetProperty("ObjectNamespace", ObjectNamespace);	
				this.DataTemplate.SetProperty("BusinessLogicNamespace", BusinessLogicNamespace);	
				this.DataTemplate.SetProperty("DataAccessNamespace", DataAccessNamespace);	
				this.DataTemplate.SetProperty("TargetClass", TargetClass);	
				this.DataTemplate.SetProperty("PropertyList", PropertyList);	
				this.DataTemplate.SetProperty("ClassNames", _classNames);	
				this.DataTemplate.SetProperty("ForeignKeys", _foreignKeys);	

				// SQLOutput += DataTemplate.RenderToString(); 
				RenderToFile(ObjectNamespace  + "\\SQL" , TargetClass + ".sql", DataTemplate);
				BuilderOutput += TargetClass + " SQL Objects Built \n";

			}
			if(this.generateBaseClasses)
			{
				this.BaseDataClassTemplate.SetProperty("CompanyName", CompanyName);
				this.BaseDataClassTemplate.SetProperty("DeveloperName", DeveloperName);	
				this.BaseDataClassTemplate.SetProperty("RootNamespace", RootNamespace);	
				this.BaseDataClassTemplate.SetProperty("ObjectNamespace", ObjectNamespace);	
				this.BaseDataClassTemplate.SetProperty("BusinessLogicNamespace", BusinessLogicNamespace);	
				this.BaseDataClassTemplate.SetProperty("DataAccessNamespace", DataAccessNamespace);	
				this.BaseDataClassTemplate.SetProperty("TargetClass", TargetClass);
				RenderToFile(ObjectNamespace  + "\\" + DataAccessNamespace , "BaseDataClass.cs", BaseDataClassTemplate);
	
				this.BaseObjectClassTemplate.SetProperty("CompanyName", CompanyName);
				this.BaseObjectClassTemplate.SetProperty("DeveloperName", DeveloperName);	
				this.BaseObjectClassTemplate.SetProperty("RootNamespace", RootNamespace);	
				this.BaseObjectClassTemplate.SetProperty("ObjectNamespace", ObjectNamespace);	
				this.BaseObjectClassTemplate.SetProperty("BusinessLogicNamespace", BusinessLogicNamespace);	
				this.BaseObjectClassTemplate.SetProperty("DataAccessNamespace", DataAccessNamespace);	
				this.BaseObjectClassTemplate.SetProperty("TargetClass", TargetClass);	
				RenderToFile(ObjectNamespace  + "\\" + BusinessLogicNamespace , "BaseObjectClass.cs", BaseObjectClassTemplate);
			}
			return BuilderOutput;
		}

		#endregion

		#region Private Methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="typeName"></param>
		/// <param name="className"></param>
		/// <param name="template"></param>
		private void RenderToFile(string typeName, string className, CodeTemplate template)
		{
			if(!Directory.Exists(Path.Combine(OutputDirectory, typeName)))
			{
				Directory.CreateDirectory(Path.Combine(OutputDirectory, typeName));
			}

			string fileName = Path.Combine(OutputDirectory, typeName + "\\" + className);	
			template.RenderToFile(fileName, true);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="templateName"></param>
		/// <returns></returns>
		private CodeTemplate CompileTemplate(string templateName)
		{
			CodeTemplateCompiler compiler = new CodeTemplateCompiler(templateName);
			compiler.Compile();
			
			if (compiler.Errors.Count == 0)
			{
				return compiler.CreateInstance();
			}
			else
			{
				for (int i = 0; i < compiler.Errors.Count; i++)
				{
					Response.WriteLine(compiler.Errors[i].ToString());
				}			
				return null;
			}
			
		}

		#endregion
	}

}

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 Microsoft Public License (Ms-PL)


Written By
Web Developer
United States United States
Software Architect with 20++ years of software design and development experience with a strong hold in Object-Oriented software engineering using UML with Design Patterns. Architected and developed several industrial software packages and passed through full software development life-cycle. Currently managing a small group of developers, developing management software for the agriculture industry.

Comments and Discussions