<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a very simple business object." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
using System;
using System.Data;
using System.Collections;
namespace DemoDataTable
{
#region <%= GetClassName(SourceTable) %>
/// <summary>
/// This object represents the properties and methods of a <%= GetClassName(SourceTable) %>.
/// </summary>
public class <%= GetClassName(SourceTable) %>
{
<% foreach (ColumnSchema column in SourceTable.Columns) { %>
<%= GetMemberVariableDeclarationStatement(column) %>
<% } %>
public <%= GetClassName(SourceTable) %>()
{
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
<%=" "+SourceTable.Columns[i].Name.ToString()+" = "+ GetDefaultValue(SourceTable.Columns[i])+";" %>
<% } %>
}
#region Public Properties
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
public <%= GetCSharpVariableType(SourceTable.Columns[i]) %> <%= GetPropertyName(SourceTable.Columns[i]) %>
{
get {return <%= GetMemberVariableName(SourceTable.Columns[i]) %>;}
set {<%= GetMemberVariableName(SourceTable.Columns[i]) %> = value;}
}
<% if (i < SourceTable.Columns.Count - 1) Response.Write("\r\n"); %>
<% } %>
#endregion
}
#endregion
<script runat="template">
public string GetMemberVariableDeclarationStatement(ColumnSchema column)
{
return GetMemberVariableDeclarationStatement("protected", column);
}
public string GetMemberVariableDeclarationStatement(string protectionLevel, ColumnSchema column)
{
string statement = protectionLevel + " ";
statement += GetCSharpVariableType(column) + " " + GetMemberVariableName(column);
string defaultValue = GetMemberVariableDefaultValue(column);
if (defaultValue != "")
{
statement += " = " + defaultValue;
}
statement += ";";
return statement;
}
public string GetReaderAssignmentStatement(ColumnSchema column, int index)
{
string statement = "if (!reader.IsDBNull(" + index.ToString() + ")) ";
statement += GetMemberVariableName(column) + " = ";
if (column.Name.EndsWith("TypeCode")) statement += "(" + column.Name + ")";
statement += "reader." + GetReaderMethod(column) + "(" + index.ToString() + ");";
return statement;
}
public string GetCamelCaseName(string value)
{
return value.Substring(0, 1).ToLower() + value.Substring(1);
}
public string GetMemberVariableName(ColumnSchema column)
{
string propertyName = GetPropertyName(column);
string memberVariableName = "_" + GetCamelCaseName(propertyName);
return memberVariableName;
}
public string GetPropertyName(ColumnSchema column)
{
string propertyName = column.Name;
if (propertyName == column.Table.Name + "Name") return "Name";
if (propertyName == column.Table.Name + "Description") return "Description";
if (propertyName.EndsWith("TypeCode")) propertyName = propertyName.Substring(0, propertyName.Length - 4);
return propertyName;
}
public string GetMemberVariableDefaultValue(ColumnSchema column)
{
switch (column.DataType)
{
case DbType.Guid:
{
return "Guid.Empty";
}
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
case DbType.String:
{
return "";
}
case DbType.StringFixedLength:
{
return "String.Empty";
}
default:
{
return "";
}
}
}
public string GetCSharpVariableType(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
case DbType.AnsiString: return "string";
case DbType.AnsiStringFixedLength: return "string";
case DbType.Binary: return "byte[]";
case DbType.Boolean: return "bool";
case DbType.Byte: return "byte";
case DbType.Currency: return "decimal";
case DbType.Date: return "DateTime";
case DbType.DateTime: return "DateTime";
case DbType.Decimal: return "decimal";
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.String: return "string";
case DbType.StringFixedLength: return "string";
case DbType.Time: return "TimeSpan";
case DbType.UInt16: return "ushort";
case DbType.UInt32: return "uint";
case DbType.UInt64: return "ulong";
case DbType.VarNumeric: return "decimal";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
public string GetDefaultValue(ColumnSchema column)
{
if (column.Name.EndsWith("TypeCode")) return column.Name;
switch (column.DataType)
{
case DbType.AnsiString: return "\"\"";
case DbType.AnsiStringFixedLength: return "\"\"";
case DbType.Binary: return "{}";
case DbType.Boolean: return "false";
case DbType.Byte: return "0";
case DbType.Currency: return "0";
case DbType.Date: return "DateTime.Now";
case DbType.DateTime: return "DateTime.Now";
case DbType.Decimal: return "0";
case DbType.Double: return "0";
case DbType.Guid: return "\"\"";
case DbType.Int16: return "\"\"";
case DbType.Int32: return "0";
case DbType.Int64: return "0";
case DbType.Object: return "new object()";
case DbType.SByte: return "0";
case DbType.Single: return "0";
case DbType.String: return "\"\"";
case DbType.StringFixedLength: return "\"\"";
case DbType.Time: return "TimeSpan";
case DbType.UInt16: return "\"\"";
case DbType.UInt32: return "0";
case DbType.UInt64: return "0";
case DbType.VarNumeric: return "0";
default:
{
return "__UNKNOWN__" + column.NativeType;
}
}
}
public string GetReaderMethod(ColumnSchema column)
{
switch (column.DataType)
{
case DbType.Byte:
{
return "GetByte";
}
case DbType.Int16:
{
return "GetInt16";
}
case DbType.Int32:
{
return "GetInt32";
}
case DbType.Int64:
{
return "GetInt64";
}
case DbType.AnsiStringFixedLength:
case DbType.AnsiString:
case DbType.String:
case DbType.StringFixedLength:
{
return "GetString";
}
case DbType.Boolean:
{
return "GetBoolean";
}
case DbType.Guid:
{
return "GetGuid";
}
case DbType.Currency:
case DbType.Decimal:
{
return "GetDecimal";
}
case DbType.DateTime:
case DbType.Date:
{
return "GetDateTime";
}
case DbType.Binary:
{
return "GetBytes";
}
default:
{
return "__SQL__" + column.DataType;
}
}
}
public string GetClassName(TableSchema table)
{
if (table.Name.EndsWith("s"))
{
return table.Name.Substring(0, table.Name.Length - 1);
}
else
{
return table.Name;
}
}
public string GetSqlDbType(ColumnSchema column)
{
switch (column.NativeType)
{
case "bigint": return "BigInt";
case "binary": return "Binary";
case "bit": return "Bit";
case "char": return "Char";
case "datetime": return "DateTime";
case "decimal": return "Decimal";
case "float": return "Float";
case "image": return "Image";
case "int": return "Int";
case "money": return "Money";
case "nchar": return "NChar";
case "ntext": return "NText";
case "numeric": return "Decimal";
case "nvarchar": return "NVarChar";
case "real": return "Real";
case "smalldatetime": return "SmallDateTime";
case "smallint": return "SmallInt";
case "smallmoney": return "SmallMoney";
case "sql_variant": return "Variant";
case "sysname": return "NChar";
case "text": return "Text";
case "timestamp": return "Timestamp";
case "tinyint": return "TinyInt";
case "uniqueidentifier": return "UniqueIdentifier";
case "varbinary": return "VarBinary";
case "varchar": return "VarChar";
default: return "__UNKNOWN__" + column.NativeType;
}
}
public string GetPrimaryKeyType(TableSchema table)
{
if (table.PrimaryKey != null)
{
//if (table.PrimaryKey.MemberColumns.Count == 1)
//{
return GetCSharpVariableType(table.PrimaryKey.MemberColumns[0]);
//}
//else
//{
// throw new ApplicationException("This template will not work on primary keys with more than one member column.");
//}
}
else
{
throw new ApplicationException("This template will only work on tables with a primary key.");
}
}
public override string GetFileName()
{
return this.GetClassName(this.SourceTable) + ".cs";
}
</script>
}