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

Implementing Model-View-Presenter in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (27 votes)
17 Nov 2007CPOL12 min read 129.8K   2.7K   120  
Three implementations of Model-View-Presenter in ASP.NET 2.0.
using System;
using System.CodeDom.Compiler;
using System.Data;
using Microsoft.CSharp;
using SubSonic.Utilities;

namespace SubSonic
{
	public class CSharpCodeLanguage : ICodeLanguage
	{
		#region Constants & enumerations

		private readonly string[] keywords = {
			// keywords
			"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class",
			"const", "continue", "date", "datetime", "decimal", "default", "delegate", "do", "double", "else",
			"enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto",
			"if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new",
			"null", "object", "operator", "out", "override", "params", "private", "protected", "public", "readonly",
			"ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct",
			"switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort",
			"using", "virtual", "volatile", "void", "while",
			// contextual keywords
			"get", "partial", "set", "value", "where", "yield"
		};

		#endregion

		#region Properties

		public string CodeProvider
		{
			get { return "CSharpCodeProvider"; }
		}

		public string FileExtension
		{
			get { return SubSonic.FileExtension.DOT_CS; }
		}

		public string  Identifier
		{
			get { return "C#"; }
		}

		public string ShortName
		{
			get { return "cs"; }
		}

		public string TemplatePrefix
		{
			get { return "CS_"; }
		}

		#endregion

		#region Methods

		public CodeDomProvider CreateCodeProvider()
		{
			return new CSharpCodeProvider();
		}

		public string GetDefaultValue(string colName, DbType dbType, bool isNullableColumn)
		{
			string variableType = GetDefaultValue(colName, dbType);
			if (isNullableColumn && Utility.IsNullableDbType(dbType))
				variableType = variableType + CodeFragment.NULLABLE_VARIABLE;
			return variableType;
		}

		public string GetUsingStatements(string[] namespaces)
		{
			string usingStatements = "";
			foreach (string space in namespaces)
				usingStatements += "using " + space + ";" + Environment.NewLine;
			return usingStatements;
		}

		public string GetVariableType(DbType dbType, bool isNullableColumn)
		{
			string variableType = GetVariableType(dbType);
			if (isNullableColumn && Utility.IsNullableDbType(dbType))
				variableType = variableType + CodeFragment.NULLABLE_VARIABLE;
			return variableType;
		}

		public bool IsKeyword(string word)
		{
			return Array.IndexOf(keywords, word.ToLower()) != -1;
		}

		#endregion

		#region Private methods

		private static string GetDefaultValue(string colName, DbType dbType)
		{
			if (Utility.IsLogicalDeleteColumn(colName))
				return "false";

			switch (dbType) {
				case DbType.Guid:
					// HACK: GUID fix
					return "Guid.Empty";
				case DbType.AnsiString:
				case DbType.AnsiStringFixedLength:
				case DbType.String:
				case DbType.StringFixedLength:
					return "null";
				case DbType.Boolean:
					return "false";
				case DbType.Binary:
					return "null";
				case DbType.Xml:
					return "";
				case DbType.Date:
				case DbType.DateTime: {
						return "new DateTime(1900,01, 01)";
					}
				default:
					return "0";
			}
		}

		private static string GetVariableType(DbType dbType)
		{
			switch (dbType) {
				case DbType.AnsiString:
				case DbType.AnsiStringFixedLength:
				case DbType.String:
				case DbType.StringFixedLength: return "string";

				case DbType.Binary:		return "byte[]";
				case DbType.Boolean:	return "bool";
				case DbType.Byte:		return "byte";

				case DbType.Currency:
				case DbType.Decimal:
				case DbType.VarNumeric:	return "decimal";

				case DbType.Date:
				case DbType.DateTime:	return "DateTime";

				case DbType.Double:		return "double";
				case DbType.Guid:		return "Guid";
				case DbType.Int16:		return "short";
				case DbType.Int32:		return "int";
				case DbType.Int64:		return "long";
				case DbType.Object:		return "object";
				case DbType.SByte:		return "sbyte";
				case DbType.Single:		return "float";
				case DbType.Time:		return "TimeSpan";
				case DbType.UInt16:		return "ushort";
				case DbType.UInt32:		return "uint";
				case DbType.UInt64:		return "ulong";

				default:				return "string";
			}
		}

		#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 Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions