Click here to Skip to main content
15,895,746 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.Reflection;
using SmartCode.Model;

namespace SmartCode.Studio.Utils
{
	/// <summary>
	/// Summary description for Mapper.
	/// </summary>
	public class Mapper
	{
		public enum Behaviour
		{
			FieldsToFields,
			FieldsToProperties,
			PropertiesToFields,
			PropertiesToProperties,
			PropertiesPropertiesDataEntity
		};

		public Mapper()
		{
		}
		
		public static void Map( Object from, Object to )
		{
			Map( from, to, true, Behaviour.FieldsToFields );
		}

		public static void Map( Object from, Object to, Behaviour behaviour )
		{
			Map( from, to, true, behaviour );
		}

		public static void Map( Object from, Object to, bool caseSensitive, Behaviour behaviour )
		{
			switch ( behaviour )
			{
				case Behaviour.FieldsToFields:
					MapFieldsToFields( from, to, caseSensitive );
					break;
				case Behaviour.FieldsToProperties:
					MapFieldsToProperties( from, to, caseSensitive );
					break;
				case Behaviour.PropertiesToFields:
					MapPropertiesToFields( from, to, caseSensitive );
					break;
				case Behaviour.PropertiesToProperties:
					MapPropertiesToProperties( from, to, caseSensitive );
					break;
				case Behaviour.PropertiesPropertiesDataEntity:
					MapPropertiesToPropertiesDataEntity( from, to, caseSensitive );
					break;
			}
		}

		private static void MapFieldsToFields( Object from, Object to, bool caseSensitive )
		{
			FieldInfo toFieldInfo;
			try
			{
				Type fromType = from.GetType();
				Type toType = to.GetType();

				foreach( FieldInfo fromFieldInfo in fromType.GetFields() )
				{
					if( caseSensitive )
						toFieldInfo = toType.GetField( fromFieldInfo.Name );
					else
						toFieldInfo = toType.GetField( fromFieldInfo.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase );

					// if field exists, set value
					if( toFieldInfo != null )
					{
						if ( fromFieldInfo.FieldType.IsArray )
						{
							toFieldInfo.SetValue( to, fromFieldInfo.GetValue( from ) );
						}
						else
						{
							toFieldInfo.SetValue( to, fromFieldInfo.GetValue( from ) );
						}
					}
				}
			}
			catch
			{
				throw;
			}
		}

		private static void MapFieldsToProperties( Object from, Object to, bool caseSensitive )
		{
			PropertyInfo toPropertyInfo;
			try
			{
				Type fromType = from.GetType();
				Type toType = to.GetType();
				

				foreach( FieldInfo fromFieldInfo in fromType.GetFields() )
				{
					//PropertyInfo toPropertyInfo;
					if( caseSensitive )
						toPropertyInfo = toType.GetProperty( fromFieldInfo.Name );
					else
						toPropertyInfo = toType.GetProperty( fromFieldInfo.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase );

					// if field exists, set value
					if( toPropertyInfo != null )
					{
						//if ( fromFieldInfo.FieldType.IsArray )
						//{
						//	throw new NotImplementedException();
						//}
						//else
					{
						toPropertyInfo.SetValue( to, fromFieldInfo.GetValue( from ), null );
					}
					}
				}
			}
			catch ( Exception )
			{
				throw;
			}
		}

		private static void MapPropertiesToFields( Object from, Object to, bool caseSensitive )
		{
			FieldInfo toFieldInfo;
			Type fromType = from.GetType();
			Type toType = to.GetType();

			try
			{
				foreach( PropertyInfo fromPropertyInfo in fromType.GetProperties() )
				{
					if( caseSensitive )
						toFieldInfo = toType.GetField( fromPropertyInfo.Name );
					else
						toFieldInfo = toType.GetField( fromPropertyInfo.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase );
				
					// if field exists, set value
					if( toFieldInfo != null )
					{
						toFieldInfo.SetValue( to, fromPropertyInfo.GetValue( from, null ) );
					}
				}
			}
			catch ( Exception )
			{
				throw;
			}

		}

		private static void MapPropertiesToProperties( Object from, Object to, bool caseSensitive )
		{
			Type fromType = from.GetType();
			Type toType = to.GetType();

			foreach( PropertyInfo fromPropertyInfo in fromType.GetProperties() )
			{
				PropertyInfo toPropertyInfo;
				if( caseSensitive )
					toPropertyInfo = toType.GetProperty( fromPropertyInfo.Name );
				else
					toPropertyInfo = toType.GetProperty( fromPropertyInfo.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase );

				// if field exists, set value
				if( toPropertyInfo != null )
				{
					if (toPropertyInfo.PropertyType != fromPropertyInfo.PropertyType)
						continue;
					/*if ( fromPropertyInfo.PropertyType.IsArray )
					{
						throw new NotImplementedException();
					}
					else*/
					{
						
						if (toPropertyInfo.CanWrite)
							toPropertyInfo.SetValue( to, fromPropertyInfo.GetValue( from, null ), null );
					}
				}
			}
		}

		private static void MapPropertiesToPropertiesDataEntity( Object from, Object to, bool caseSensitive )
		{
			Type fromType = from.GetType();
			Type toType = to.GetType();

			foreach( PropertyInfo fromPropertyInfo in fromType.GetProperties() )
			{
				PropertyInfo toPropertyInfo;
				if( caseSensitive )
					toPropertyInfo = toType.GetProperty( fromPropertyInfo.Name );
				else
					toPropertyInfo = toType.GetProperty( fromPropertyInfo.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase );

				
				// if field exists, set value
				if( toPropertyInfo != null )
				{
					if (toPropertyInfo.Name == "ID")
						continue;

					if (toPropertyInfo.PropertyType != fromPropertyInfo.PropertyType)
						continue;

					if ( fromPropertyInfo.PropertyType.IsArray )
					{
						throw new NotImplementedException();
					}
					else
					{
						if (toPropertyInfo.CanWrite)
						{
							object val = fromPropertyInfo.GetValue( from, null );
							if (val != null)
							toPropertyInfo.SetValue( to, fromPropertyInfo.GetValue( from, null ), null );
						}
					}
				}
			}
		}
		


	}
}

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