Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / JScript .NET

Stored Procedure Invocation Code Generator for VB, C# and JScript.NET

Rate me:
Please Sign up or sign in to vote.
4.92/5 (24 votes)
14 Jun 20038 min read 387K   3.4K   133  
Stored Procedure Invocation Code Generator for VB, C# and JScript.NET
//================================================================================
//
//  Copyright (c) objectnation GmbH, 2002
//
//================================================================================
//
//  Author          simon.wilson
//  Creation Date   17.09.2002
//  Creation Time   16:30:35
//  IDE Version     Microsoft Development Environment Enterprise Edition 7.00
//  OS Version      Microsoft Windows NT 5.1.2600.0
//
//================================================================================

#region Copyright � objectnation GmbH, 2002

// This software is provided 'as-is'. While the greatest care has been taken to 
// ensure that the software functions correctly, no guarantee can be made to this effect.
//
// objectnation grants permission to anyone to use this software for any purpose as long as 
// it is in compliance with the following restrictions:
//
// 1.	This software is the intellectual property and copyright of objectnation GmbH. 
//    Under no circumstances may the origin of the software be misrepresented.
//
// 2.	Should this software be used in the development of a commercial product, 
//    then an acknowledgement in that product's documentation is a requirement. 
//    The following text should be used:
//
//    This product contains code generated by objectnation SP/Invoke, 
//    copyright � objectnation GmbH 2002 (http://www.objectnation.com).
//
// 3.	The software may not be redistributed without the express written permission of 
//    the copyright holder, namely objectnation GmbH, Switzerland.

#endregion

using System;
using System.Collections;
using System.Data.SqlTypes;
using System.CodeDom;
using System.CodeDom.Compiler;

namespace objectnation.SPInvoke
{
	public abstract class ColumnCodeGenerator
	{
		public abstract CodeExpression CreateInitializationExpression();
		public abstract CodeExpression CreateDeserializationExpression(CodeExpression serializationInfoObject, string propName);
		public abstract CodeExpression CreateNullCheckExpression(CodeExpression targetObject);
		public abstract CodeExpression CreateValueExpression(CodeExpression targetObject);
	}

	// ===========================================================================

	public class DefaultColumnCodeGenerator : ColumnCodeGenerator
	{
		Type _type;
		Type _sqlType;

		// ===========================================================================

		public DefaultColumnCodeGenerator(Type type, Type sqlType)
		{
			_type = type;
			_sqlType = sqlType;
		}

		// ===========================================================================

		public override CodeExpression CreateInitializationExpression()
		{
			return new CodeFieldReferenceExpression(
				new CodeTypeReferenceExpression(_sqlType), "Null");
		}

		// ===========================================================================

		public override CodeExpression CreateDeserializationExpression(
			CodeExpression serializationInfoObject, 
			string propName)
		{
			CodeExpression [] parameters =
			{
				new CodePrimitiveExpression(propName),
				new CodeTypeOfExpression(_type)
			};

			CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression(serializationInfoObject, "GetValue", parameters);
			return new CodeObjectCreateExpression(_sqlType, new CodeCastExpression(_type, invoke));
		}

		// ===========================================================================

		public override CodeExpression CreateNullCheckExpression(
			CodeExpression targetObject)
		{
			return new CodePropertyReferenceExpression(targetObject, "IsNull");
		}

		// ===========================================================================

		public override CodeExpression CreateValueExpression(
			CodeExpression targetObject)
		{
			return new CodePropertyReferenceExpression(targetObject, "Value");
		}
	}

	// ===========================================================================

	public class ObjectColumnCodeGenerator : DefaultColumnCodeGenerator
	{
		public ObjectColumnCodeGenerator() : base(typeof(object), typeof(object)) {}

		// ===========================================================================

		public override CodeExpression CreateInitializationExpression()
		{
			return new CodePrimitiveExpression(null);
		}

		// ===========================================================================

		public override CodeExpression CreateDeserializationExpression(
			CodeExpression serializationInfoObject, 
			string propName)
		{
			CodeExpression [] parameters =
			{
				new CodePrimitiveExpression(propName),
				new CodeTypeOfExpression(typeof(object))
			};

			return new CodeMethodInvokeExpression(serializationInfoObject, "GetValue", parameters);
		}

		// ===========================================================================

		public override CodeExpression CreateNullCheckExpression(
			CodeExpression targetObject)
		{
			CodeExpression expression = null;
			expression = new CodeBinaryOperatorExpression(
				targetObject, 
				CodeBinaryOperatorType.IdentityEquality, 
				new CodePrimitiveExpression(null));

			return new CodeCastExpression(typeof(bool), expression);
		}

		// ===========================================================================

		public override CodeExpression CreateValueExpression(
			CodeExpression targetObject)
		{
			return targetObject;
		}
	}

	// =============================================================================

	public class ColumnCodeGeneratorFactory
	{
		public static ColumnCodeGenerator Create(Type type, Type sqlType)
		{
			if(type == typeof(object))
				return new ObjectColumnCodeGenerator();
			else
				return new DefaultColumnCodeGenerator(type, sqlType);
		}
	}
}

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
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions