Search This Blog

Wednesday, August 4, 2010

Modification To Earlier Template

The Codesmith template in last post was having left out the implementation of ValuesEquals method half baked,further due to inconsistency in naming there were few errors also some variable have higher accessiblity & some methods are lower visibility than required.
 I also modified delete method now the code-smith BLL template is ready for new cycle of bug fixing.I am providing the new template bellow.If someone out there finding any bug or have some usefull suggestion  please let me know.


The Template Bellow:

<%------------------------------------------------------------------------------------------
* Author: Sangram S. Desai
* Description: This template will generate A Collectionbase class depending upon table columns
------------------------------------------------------------------------------------------%>
<%@ CodeTemplate Debug="True" Language="C#" Inherits="CodeSmith.BaseTemplates.SqlCodeTemplate" TargetLanguage="T-SQL"
    Description="Generate Collection based class" %>
<%-- Context --%>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="1. Context"
    Description="Table that the classes should be based on." %>
<%-- Options --%>
<%-- Assembly References --%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Assembly Name="CodeSmith.Engine" %>
<%@ Assembly Name="System.Data" %>

<%-- Namespace Imports --%>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="CodeSmith.CustomProperties" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="CodeSmith.Engine" %>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using Conflux.DLL;

using System.Data.SqlTypes;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections;
<%
string BaseClassName =StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0]) +"Base";;
string CoreClassName = StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0]) +"Core";
string FrontClassName =StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0]);
string FrontCollectionClassName =StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0])+"s";
string PrimeColName = StringUtil.ToPascalCase(SourceTable.Columns[0].Name);
%>
namespace Conflux.BLL
{
    #region Enums
    <%
    GenerateEnum(SourceTable.Columns,SourceTable.Name);
    %>
    #endregion

    #region Base_Class
    [Serializable]
    public class <% Response.Write(StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0]) + "Base"); %>
    {
        #region Class_Variables
        private string _TableName=String.Empty;
      <%
        GenerateBaseClassClassVariables(SourceTable.Columns,SourceTable.Name);
        %>
        <%
        string EntityDBState = "private EntityDBState _" + StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0]) + "EntityDBState=EntityDBState.Deafult;";
        string EntityValidationStatus = "private EntityValidationStatus _"+ StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0]) + "EntityValidationStatus=EntityValidationStatus.Default;";
        Response.WriteLine(EntityDBState);
        Response.WriteLine(EntityValidationStatus);
        Response.IndentLevel=1;
     
        %>
        #endregion

        #region Constructors
        <%
        GenerateBaseClassDeafultConstructor(SourceTable.Name);
        %>
        <%
        GenerateBaseClassNonDeafultConstructor(SourceTable.Columns,SourceTable.Name);
        %>
        #endregion

        #region Properties
        <%
        GenerateBaseClassEntityDBState(SourceTable.Name);
        %>
        <%
        GenerateBaseClassValidationState(SourceTable.Name);
       %>
        public string TableName
        {
            get
            {
                return _TableName;
            }
            set
            {
                _TableName = value;
            }
        }
        <%
        GeneralEnumsCreator(SourceTable.Columns);
        %>
        #endregion

       

        #region Methods
        <%
        GenerateMarkToDelete(SourceTable.Name);
       %>
        <%
        GenerateRemoveDeleteMark(SourceTable.Name);
        %>
        #endregion
    }
   
    #endregion
    #region Core_Class
    public class <% string BaseName = StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0]);Response.Write(BaseName + "Core:" + BaseName + "Base");%>
    {
        #region Class_Variables
        private int _AffectedRows;
        IDBManager dbManager = new DBManager(DataProvider.MSSQL);
        #endregion

        #region Properties
        int AffectedRows
        {
            get
            {
                return _AffectedRows;
            }
            set
            {
                _AffectedRows = value;
            }
        }
        #endregion

        #region Constructor
        public <% Response.Write(BaseName+"Core");%>():base()
        {

        }
        #endregion

        #region Database_Interaction_Methods
        <%
        GenerateDelete(SourceTable.Name,SourceTable.Columns[0].Name);
        %>
       <%
        GenerateFind(SourceTable.Name,SourceTable.Columns);
        %>
        <% GenerateGetAll(SourceTable.Name);%>
        <%
        for(int k=0;k<SourceTable.Columns.Count;k++)
        {
        if (SourceTable.Columns[k].IsUnique==true)
            {
                GenerateGetByUniqueKey(SourceTable.Name,SourceTable.Columns[k].Name,SourceTable.Columns[k].DataType.ToString());
            }
        }
        %>
       public DataSet GetPaged(string WhereClause, string OrderBy, int PageIndex, int PageSize)
        {
            dbManager.ConnectionString = ConfigurationManager.ConnectionStrings["MSSQL"].ToString();
            DataSet ds = new DataSet();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);
                dbManager.AddParameters(0, "@WhereClause", WhereClause, ParameterDirection.Input);
                dbManager.AddParameters(1, "@OrderBy", OrderBy, ParameterDirection.Input);
                dbManager.AddParameters(2, "@PageIndex", PageIndex, ParameterDirection.Input);
                dbManager.AddParameters(3, "@PageSize", PageSize, ParameterDirection.Input);

                ds = dbManager.ExecuteDataSet(CommandType.StoredProcedure,<% Response.Write("\""+SourceTable.Name + "_GetPaged\""); %>);
                return ds;
            }
            catch (Exception ex)
            {
                //Usual code             
            }
            finally
            {
                dbManager.Dispose();
            }
            return ds;
        }
        <% GenerateInsert(SourceTable.Name,SourceTable.Columns);%>
        <% GenerateUpdate(SourceTable.Name,SourceTable.Columns);%>
        <% GenerateInsertOrUpdate(SourceTable.Name,SourceTable.Columns);%>
        #endregion
    }
    #endregion

    #region Front_Class
    public class <% Response.Write(FrontClassName + ":" + CoreClassName + ", IComparable, ICloneable"); %>
    {
        #region Sort_Enums
        <%GenerateEnumSortMethod(SourceTable.Columns);%>
        public enum SortOrder
        {
            Ascending,
            Descending
        }
        private SortMethod _SortMethod;
        private SortOrder _SortOrder;
        #endregion

        #region Properties
        public SortMethod PreferredSortMethod
        {
            get
            {
                return _SortMethod;
            }
            set
            {
                _SortMethod = value;
            }
        }
        public SortOrder PreferredSortOrder
        {
            get
            {
                return _SortOrder;
            }
            set
            {
                _SortOrder = value;
            }
        }
        #endregion

        #region Constructors
        public <% Response.Write(FrontClassName + "()") ;%>:base()
        {
            /*Default Sort Criteria*/
            this.PreferredSortMethod = SortMethod.<% Response.Write(PrimeColName) ;%>;
            this.PreferredSortOrder = SortOrder.Ascending;
        }
        #endregion

        #region MethodToMaskDirectDatastoreAccess
        /*create a new object with data fetched
         * from database from it's primary key*/
        <% GenerateFetch(SourceTable.Name,SourceTable.Columns);%>
        <% GenerateSave(SourceTable.Name,SourceTable.Columns);%>
       <% GenerateDelete(SourceTable.Name,SourceTable.Columns);%>
        #endregion

        #region IComparable Members
        <% GenerateCompareTo(SourceTable.Name,SourceTable.Columns);%>
        #endregion

        #region ICloneable_Members
        /*shallow copy*/
        public object Clone()
        {
            return this.MemberwiseClone();
        }

        /*Deep Copy*/
        public static T DeepCopy<T>(T item)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            formatter.Serialize(stream, item);
            stream.Seek(0, SeekOrigin.Begin);
            T result = (T)formatter.Deserialize(stream);
            stream.Close();
            return result;
        }

        #endregion

        #region Equality Comparison
        /*Referencial Equality*/
        public override bool Equals(object obj)
        {
            return (Object.ReferenceEquals(this, obj))?true:false;
        }
        /*Value based Equality*/
        <% GenerateValuesEquals(SourceTable.Name,SourceTable.Columns);%>
   
        public override int GetHashCode()
        {
            <% Response.WriteLine("return this." + PrimeColName + ".GetHashCode();");%>
        }
        #endregion
    }
    #endregion

    #region Collection_Class
    /*Strongly Typed Collection*/
    class <% Response.Write(FrontCollectionClassName);%> : CollectionBase
    {
        /*ILIST Interface methods*/
        <% GenerateCollectionAdd(SourceTable.Name);%>
        <% GenerateCollectionRemove(SourceTable.Name);%>
      
        /************Strongly Typed Accessor*****************
        * set-will override existing value at specified index
        * get-return object at specified index
        *****************************************************/
        <% GenerateCollectionIndexer(SourceTable.Name);%>
        #region Constructors
        <% GenerateCollectionConstructor(SourceTable.Name);%>
        #endregion
        <% GenerateCollectionCopyTo(SourceTable.Name);%>
        <% GenerateCollectionIndexOf(SourceTable.Name);%>
        <% GenerateCollectionInsert(SourceTable.Name);%>
        <% GenerateCollectionContains(SourceTable.Name);%>
        <% GenerateCollectionReadOnly(SourceTable.Name);%>
        protected override void OnValidate(object value)
        {
            base.OnValidate(value);
            if (!(value is <% Response.Write(FrontClassName);%>))
            {
                throw new ArgumentException("Collection only supports <% Response.Write(FrontClassName);%> objects.");
            }
        }
       
       
       
       

        #region InlineClassForGettingReadOnlyCollection
        <% Response.IndentLevel=1; Response.Write("private sealed class ReadOnly" + FrontClassName + "s : " + FrontClassName + "s");%>
        {
            private const string ERROR_STRING = "Collection is read-only.";
           <% GenerateReadOnlyConstructor(SourceTable.Name);%>
            <% GenerateReadOnlyAdd(SourceTable.Name);%>
            <% GenerateReadOnlyRemove(SourceTable.Name);%>
           
            <% GenerateReadOnlyOnClear();%>
             <% GenerateReadOnlyOnInsert();%>
            <% GenerateReadOnlyOnRemove();%>
            <% GenerateReadOnlyOnSet();%>
        }
        #endregion

    }
    #endregion

    #region ReadOnlyDeptCollection

    #endregion
}
<script runat="template">
public void GenerateEnum(ColumnSchemaCollection columns,string tableName)
{
    string EnumName=StringUtil.ToPascalCase(tableName.Split('_')[0]) + "Column";
    Response.IndentLevel=2;
    Response.WriteLine("public enum " + EnumName);
    Response.WriteLine("{");
    Response.Indent();
    for(int i=0;i< columns.Count;i++)
    {
        string dbColName = columns[i].Name.ToString();
        if(i==columns.Count-1)
        {
            Response.WriteLine(StringUtil.ToPascalCase(dbColName));
        }
       else
        {
            Response.WriteLine(StringUtil.ToPascalCase(dbColName)+ ",");
        }
    }
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
   
}
public void GenerateBaseClassClassVariables(ColumnSchemaCollection columns,string tableName)
{
    string BaseClassName=StringUtil.ToPascalCase(tableName.Split('_')[0]) + "Base";
    string OutValue;
    Response.IndentLevel=3;
    for(int i=0;i< columns.Count;i++)
    {
       OutValue = String.Empty;
      switch(columns[i].DataType)
       {
        case System.Data.DbType.Int32:
            OutValue="private int "+"_"+StringUtil.ToPascalCase(columns[i].Name)+" = int.MinValue;";
            break;
       case System.Data.DbType.AnsiString:
            OutValue="private string "+"_"+StringUtil.ToPascalCase(columns[i].Name)+" = String.Empty;";
            break;
        case System.Data.DbType.AnsiStringFixedLength:
            OutValue="private char "+"_"+StringUtil.ToPascalCase(columns[i].Name)+" = Char.MinValue;";
            break;
        case System.Data.DbType.DateTime:
            OutValue="private DateTime "+"_"+StringUtil.ToPascalCase(columns[i].Name)+" = DateTime.MinValue;";
            break;
        case System.Data.DbType.Double:
            OutValue="private double "+"_"+StringUtil.ToPascalCase(columns[i].Name)+" = Double.MinValue;";
           break;
       }
    Response.WriteLine(OutValue);
    }
}
public void GenerateBaseClassDeafultConstructor(string tableName)
{
    Response.IndentLevel=3;
    string BaseClassName=StringUtil.ToPascalCase(tableName.Split('_')[0]) + "Base";
    string ClassName=StringUtil.ToPascalCase(tableName.Split('_')[0]);
    Response.WriteLine("public " + BaseClassName + "()");
    Response.WriteLine("{");   
    Response.Indent();
    Response.WriteLine("this." + ClassName + "EntityDBState = EntityDBState.Added;");
    Response.Unindent();
    Response.WriteLine("}");   
}
public void GenerateBaseClassNonDeafultConstructor(ColumnSchemaCollection columns,string tableName)
{
    Response.IndentLevel=3;
    string BaseClassName=StringUtil.ToPascalCase(tableName.Split('_')[0]) + "Base";
   
    string ColumnNameList=string.Empty;
    string OutValue;
    for(int i=1;i< columns.Count;i++)
    {
       OutValue = String.Empty;
       switch(columns[i].DataType)
       {
        case System.Data.DbType.Int32:
            OutValue="int " + "c" + StringUtil.ToPascalCase(columns[i].Name);
            break;
        case System.Data.DbType.AnsiString:
            OutValue="string " + "c" + StringUtil.ToPascalCase(columns[i].Name);
            break;
        case System.Data.DbType.AnsiStringFixedLength:
            OutValue="char " + "c" + StringUtil.ToPascalCase(columns[i].Name);
            break;
        case System.Data.DbType.DateTime:
            OutValue="DateTime " + "c" + StringUtil.ToPascalCase(columns[i].Name);
            break;
        case System.Data.DbType.Double:
            OutValue="double " + "c" + StringUtil.ToPascalCase(columns[i].Name);
            break;
       }
        if(i==columns.Count-1)
        {
            ColumnNameList=ColumnNameList+OutValue;
        }
        else
        {
            ColumnNameList=ColumnNameList+OutValue+",";
        }
    }
    Response.WriteLine("public " + BaseClassName + "("+ColumnNameList+")");
    Response.WriteLine("{");   
    Response.Indent();
    for(int i=1;i< columns.Count;i++)
    {
        Response.WriteLine("this." + StringUtil.ToPascalCase(columns[i].Name) + " = c" + StringUtil.ToPascalCase(columns[i].Name)+";");
    }
    Response.Unindent();
    Response.WriteLine("}");   
    Response.IndentLevel=1;
}
public void GenerateBaseClassEntityDBState(string tableName)
 {
  Response.IndentLevel=3;
  string EnumPropertyName = StringUtil.ToPascalCase(tableName.Split('_')[0]) + "EntityDBState";
  Response.WriteLine("public EntityDBState " + EnumPropertyName);
  Response.Indent();
  Response.WriteLine("{");
  Response.Indent();

  Response.WriteLine("get");
  Response.WriteLine("{");
  Response.Indent();
  Response.WriteLine("return _"+EnumPropertyName+";");
  Response.Unindent();
  Response.WriteLine("}");

  Response.WriteLine("set");
  Response.WriteLine("{");
  Response.Indent();
  Response.WriteLine("_" + EnumPropertyName+" = value;");
  Response.Unindent();
  Response.WriteLine("}");

  Response.Unindent();
  Response.WriteLine("}");
  Response.Unindent();
}
public void GenerateBaseClassValidationState(string tableName)
 {
  Response.IndentLevel=3;
  string EnumPropertyName = StringUtil.ToPascalCase(tableName.Split('_')[0]) + "EntityValidationStatus";
  Response.WriteLine("public EntityValidationStatus " + EnumPropertyName);
  Response.Indent();
  Response.WriteLine("{");
  Response.Indent();

  Response.WriteLine("get");
  Response.WriteLine("{");
  Response.Indent();
  Response.WriteLine("return _"+EnumPropertyName + ";");
  Response.Unindent();
  Response.WriteLine("}");

  Response.WriteLine("set");
  Response.WriteLine("{");
  Response.Indent();
  Response.WriteLine("_" + EnumPropertyName+" = value;");
  Response.Unindent();
  Response.WriteLine("}");
 
  Response.Unindent();
  Response.WriteLine("}");
  Response.Unindent();
  Response.IndentLevel=1;
}
/*Generalized Enum Creation Function*/
public void GeneralEnumCreator(string BaseType,string ColName)
 {
  Response.IndentLevel=3;
  string EnumPropertyName = StringUtil.ToPascalCase(ColName);
  Response.WriteLine("public " + BaseType + " " + EnumPropertyName);
  Response.Indent();
  Response.WriteLine("{");
  Response.Indent();

  Response.WriteLine("get");
  Response.WriteLine("{");
  Response.Indent();
  Response.WriteLine("return _"+EnumPropertyName + ";");
  Response.Unindent();
  Response.WriteLine("}");

  Response.WriteLine("set");
  Response.WriteLine("{");
  Response.Indent();
  Response.WriteLine("_" + EnumPropertyName+" = value;");
  Response.Unindent();
  Response.WriteLine("}");
 
  Response.Unindent();
  Response.WriteLine("}");
  Response.Unindent();
  Response.IndentLevel=1;
}
public void GeneralEnumsCreator(ColumnSchemaCollection columns)
{
    for(int i=0;i< columns.Count;i++)
    {
       switch(columns[i].DataType)
       {
        case System.Data.DbType.Int32:
            GeneralEnumCreator("int",columns[i].Name);
            break;
        case System.Data.DbType.AnsiString:
            GeneralEnumCreator("string",columns[i].Name);
            break;
        case System.Data.DbType.AnsiStringFixedLength:
            GeneralEnumCreator("char",columns[i].Name);
            break;
        case System.Data.DbType.DateTime:
            GeneralEnumCreator("DateTime",columns[i].Name);
            break;
        case System.Data.DbType.Double:
            GeneralEnumCreator("Double",columns[i].Name);
            break;
       }
    }
}
public void GenerateMarkToDelete(string tableName)
{
    string EntityVariable =StringUtil.ToPascalCase(tableName.Split('_')[0]) + "EntityDBState";
   
    Response.IndentLevel=3;   
    Response.WriteLine("public void MarkToDelete()");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("if (" + EntityVariable + " != EntityDBState.Added)");
    Response.WriteLine("{");
    Response.Indent();
    Response.WriteLine(EntityVariable + " = EntityDBState.MarkForDelete;");
    Response.Unindent();
    Response.WriteLine("}");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateRemoveDeleteMark(string tableName)
{
    string EntityVariable =StringUtil.ToPascalCase(tableName.Split('_')[0]) + "EntityDBState";
   
    Response.IndentLevel=3;   
    Response.WriteLine("public void RemoveDeleteMark()");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("if (" + EntityVariable + " != EntityDBState.Added)");
    Response.WriteLine("{");
    Response.Indent();
    Response.WriteLine(EntityVariable + " = EntityDBState.Changed;");
    Response.Unindent();
    Response.WriteLine("}");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;   
}
public void GenerateDelete(string tableName,string primaryKeyColName)
{
    Response.IndentLevel=3;   
    Response.WriteLine("public virtual bool Delete(int id)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.ConnectionString = ConfigurationManager.ConnectionStrings[\"MSSQL\"].ToString();");
    /*try block*/
    Response.WriteLine("try");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Open();");
    Response.WriteLine("dbManager.CreateParameters(1);");
    Response.WriteLine("dbManager.AddParameters(0, " + "\"@" + primaryKeyColName +"\", id, ParameterDirection.Input);");
    Response.WriteLine("this.AffectedRows = dbManager.ExecuteNonQuery(CommandType.StoredProcedure, \"" + tableName + "_Delete\");");
    Response.WriteLine("return (this.AffectedRows>0)?true:false;");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*try block*/
   
    /*catch*/
    Response.WriteLine("catch(Exception ex)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.Unindent();
    Response.WriteLine("}");
    /*catch*/
   
    /*finally*/
    Response.WriteLine("finally");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Dispose();");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*finally*/
   
    Response.WriteLine("return false;");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;   
}
public void GenerateFind(string tableName,ColumnSchemaCollection columns)
{
       Response.IndentLevel=3;   
    string ColName =string.Empty;
    string ProcedureName = tableName + "_Find";
    int paramCount = columns.Count+1;
    string functionParams="public DataSet Find(bool? SearchUsingOR,";
    for(int i=0;i< columns.Count;i++)
    {
       ColName = StringUtil.ToPascalCase(columns[i].Name);
       switch(columns[i].DataType)
       {
        case System.Data.DbType.Int32:
            functionParams= functionParams + "int? " + ColName;
            break;
        case System.Data.DbType.AnsiString:
            functionParams= functionParams + "string " + ColName;
            break;
        case System.Data.DbType.AnsiStringFixedLength:
            functionParams= functionParams + "char? " + ColName;
            break;
        case System.Data.DbType.DateTime:
            functionParams= functionParams +  "DateTime? " + ColName;
            break;
        case System.Data.DbType.Double:
            functionParams= functionParams +  "Double? " + ColName;
            break;
        }
        if(i != columns.Count-1)
        {
            functionParams= functionParams + ",";   
        }
    }
    functionParams = functionParams +")";
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.ConnectionString = ConfigurationManager.ConnectionStrings[\"MSSQL\"].ToString();");
    Response.WriteLine("DataSet ds=new DataSet();");
   
    /*try block*/
    Response.WriteLine("try");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Open();");
   
    Response.WriteLine("dbManager.CreateParameters(" + paramCount + ");");
   
    Response.WriteLine("dbManager.AddParameters(0, \"@SearchUsingOR\", SearchUsingOR, ParameterDirection.Input);");
    for(int j=0;j< columns.Count;j++)
    {
        ColName = StringUtil.ToPascalCase(columns[j].Name);
        Response.WriteLine("dbManager.AddParameters(" + (j+1).ToString() + ",\"@" + ColName + "\","+ ColName + ", ParameterDirection.Input);");
    }
    Response.WriteLine("ds = dbManager.ExecuteDataSet(CommandType.StoredProcedure, \"" + ProcedureName+ "\");");
    Response.WriteLine("return ds;");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*try block*/
   
    /*catch*/
    Response.WriteLine("catch(Exception ex)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.Unindent();
    Response.WriteLine("}");
    /*catch*/
   
    /*finally*/
    Response.WriteLine("finally");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Dispose();");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*finally*/
   
    Response.WriteLine("return ds;");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;   
}
public void GenerateGetAll(string tableName)
{
    Response.IndentLevel=3;   
    string ProcedureName = tableName + "_Get_List";
    Response.WriteLine("public DataSet GetAll()");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.ConnectionString = ConfigurationManager.ConnectionStrings[\"MSSQL\"].ToString();");
    Response.WriteLine("DataSet ds=new DataSet();");
   
    /*try block*/
    Response.WriteLine("try");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Open();");
       
    Response.WriteLine("ds = dbManager.ExecuteDataSet(CommandType.StoredProcedure, \"" + ProcedureName+ "\");");
    Response.WriteLine("return ds;");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*try block*/
   
    /*catch*/
    Response.WriteLine("catch(Exception ex)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.Unindent();
    Response.WriteLine("}");
    /*catch*/
   
    /*finally*/
    Response.WriteLine("finally");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Dispose();");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*finally*/
   
    Response.WriteLine("return ds;");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;   
}
/*get by primary key*/
public void GenerateGetByID(string tableName,ColumnSchemaCollection columns)
{
       Response.IndentLevel=3;   
    string ColName =string.Empty;
    int paramCount =1;
    string PrimaryKeyColumn = StringUtil.ToPascalCase(columns[0].Name);
    string FunctionName = "GetBy" + PrimaryKeyColumn;
    string ProcedureName = tableName + "_" + FunctionName;
    Response.WriteLine("public DataSet " + FunctionName + "(int id)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.ConnectionString = ConfigurationManager.ConnectionStrings[\"MSSQL\"].ToString();");
    Response.WriteLine("DataSet ds=new DataSet();");
   
    /*try block*/
    Response.WriteLine("try");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Open();");
   
    Response.WriteLine("dbManager.CreateParameters(" + paramCount + ");");
   
    Response.WriteLine("dbManager.AddParameters(0, \"@" + PrimaryKeyColumn + "\", id, ParameterDirection.Input);");

    Response.WriteLine("ds = dbManager.ExecuteDataSet(CommandType.StoredProcedure, \"" + ProcedureName+ "\");");
    Response.WriteLine("return ds;");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*try block*/
   
    /*catch*/
    Response.WriteLine("catch(Exception ex)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.Unindent();
    Response.WriteLine("}");
    /*catch*/
   
    /*finally*/
    Response.WriteLine("finally");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Dispose();");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*finally*/
   
    Response.WriteLine("return ds;");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;   
}
/*get by unique key*/
public void GenerateGetByUniqueKey(string tableName,string UniqueKeyColumn,string DataType)
{
       Response.IndentLevel=3;   
    string ColName =StringUtil.ToPascalCase(UniqueKeyColumn);
    int paramCount =1;
    string FunctionName = "GetBy" + ColName;
    string ProcedureName = tableName + "_" + FunctionName;
    string ParametreString;
    ParametreString = String.Empty;
    switch(DataType)
    {
        case "Int32":
            ParametreString="int " + ColName;
            break;
        case "AnsiString":
            ParametreString="string " + ColName;
            break;
        case "AnsiStringFixedLength":
            ParametreString="char " + ColName;
            break;
        case "DateTime":
            ParametreString="DateTime " + ColName;
            break;
        case "Double":
            ParametreString="double " + ColName;
            break;
    }
   
    Response.WriteLine("public DataSet " + FunctionName + "(" + ParametreString + ")");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.ConnectionString = ConfigurationManager.ConnectionStrings[\"MSSQL\"].ToString();");
    Response.WriteLine("DataSet ds=new DataSet();");
   
    /*try block*/
    Response.WriteLine("try");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Open();");
   
    Response.WriteLine("dbManager.CreateParameters(" + paramCount + ");");
   
    Response.WriteLine("dbManager.AddParameters(0, \"@" + ColName + "\", " + ColName + ", ParameterDirection.Input);");

    Response.WriteLine("ds = dbManager.ExecuteDataSet(CommandType.StoredProcedure, \"" + ProcedureName+ "\");");
    Response.WriteLine("return ds;");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*try block*/
   
    /*catch*/
    Response.WriteLine("catch(Exception ex)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.Unindent();
    Response.WriteLine("}");
    /*catch*/
   
    /*finally*/
    Response.WriteLine("finally");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Dispose();");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*finally*/
   
    Response.WriteLine("return ds;");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;   
}
/*generate insert function*/
public void GenerateInsert(string tableName,ColumnSchemaCollection columns)
{
       Response.IndentLevel=3;   
    string ColName =string.Empty;
    string ProcedureName = tableName + "_Insert";
    int paramCount = columns.Count;
    string functionParams="public int Insert(";
    string ParamDirection=string.Empty;
    string ParamValue=string.Empty;
    for(int i=0;i< columns.Count;i++)
    {
        if(i == 0)
        {
            continue;
        }
       ColName = StringUtil.ToPascalCase(columns[i].Name);
       switch(columns[i].DataType)
       {
        case System.Data.DbType.Int32:
            functionParams= functionParams + "int " + ColName;
            break;
        case System.Data.DbType.AnsiString:
            functionParams= functionParams + "string " + ColName;
            break;
        case System.Data.DbType.AnsiStringFixedLength:
            functionParams= functionParams + "char " + ColName;
            break;
        case System.Data.DbType.DateTime:
            functionParams= functionParams +  "DateTime " + ColName;
            break;
        case System.Data.DbType.Double:
            functionParams= functionParams +  "double " + ColName;
            break;
        }
        if(i != columns.Count-1)
        {
            functionParams= functionParams + ",";   
        }
    }
    functionParams = functionParams +")";
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.ConnectionString = ConfigurationManager.ConnectionStrings[\"MSSQL\"].ToString();");
       
    /*try block*/
    Response.WriteLine("try");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Open();");
   
    Response.WriteLine("dbManager.CreateParameters(" + paramCount + ");");
   
    for(int j=0;j< columns.Count;j++)
    {
        ColName = StringUtil.ToPascalCase(columns[j].Name);
        ParamDirection="ParameterDirection.Input";
        if(j==0)
        {
            ParamDirection="ParameterDirection.Output";
            ParamValue="0";
        }
        else
        {
            ParamValue = ColName;
        }
        Response.WriteLine("dbManager.AddParameters(" + (j).ToString() + ",\"@" + ColName + "\","+ ParamValue + ", " + ParamDirection + ");");
    }
    Response.WriteLine("this.AffectedRows = dbManager.ExecuteNonQuery(CommandType.StoredProcedure, \"" + ProcedureName + "\");");
    Response.WriteLine("return int.Parse(dbManager.Parameters[0].Value.ToString());");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*try block*/
   
    /*catch*/
    Response.WriteLine("catch(Exception ex)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.Unindent();
    Response.WriteLine("}");
    /*catch*/
   
    /*finally*/
    Response.WriteLine("finally");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Dispose();");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*finally*/
   
    Response.WriteLine("return 0;");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;   
}
/*generate update function*/
public void GenerateUpdate(string tableName,ColumnSchemaCollection columns)
{
       Response.IndentLevel=3;   
    string ColName =string.Empty;
    string ProcedureName = tableName + "_Update";
    int paramCount = columns.Count;
    string functionParams="public bool Update(";
    string ParamDirection=string.Empty;
    for(int i=0;i< columns.Count;i++)
    {
       ColName = StringUtil.ToPascalCase(columns[i].Name);
       switch(columns[i].DataType)
       {
        case System.Data.DbType.Int32:
            functionParams= functionParams + "int " + ColName;
            break;
        case System.Data.DbType.AnsiString:
            functionParams= functionParams + "string " + ColName;
            break;
        case System.Data.DbType.AnsiStringFixedLength:
            functionParams= functionParams + "char " + ColName;
            break;
        case System.Data.DbType.DateTime:
            functionParams= functionParams +  "DateTime " + ColName;
            break;
        case System.Data.DbType.Double:
            functionParams= functionParams +  "double " + ColName;
            break;
        }
        if(i != columns.Count-1)
        {
            functionParams= functionParams + ",";   
        }
    }
    functionParams = functionParams +")";
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.ConnectionString = ConfigurationManager.ConnectionStrings[\"MSSQL\"].ToString();");
       
    /*try block*/
    Response.WriteLine("try");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Open();");
   
    Response.WriteLine("dbManager.CreateParameters(" + paramCount + ");");
   
    for(int j=0;j< columns.Count;j++)
    {
        ParamDirection="ParameterDirection.Input";
        ColName = StringUtil.ToPascalCase(columns[j].Name);
        Response.WriteLine("dbManager.AddParameters(" + (j).ToString() + ",\"@" + ColName + "\","+ ColName + ", " + ParamDirection + ");");
    }
    Response.WriteLine("this.AffectedRows = dbManager.ExecuteNonQuery(CommandType.StoredProcedure, \"" + ProcedureName + "\");");
    Response.WriteLine("return (this.AffectedRows > 0) ? true : false;");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*try block*/
   
    /*catch*/
    Response.WriteLine("catch(Exception ex)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.Unindent();
    Response.WriteLine("}");
    /*catch*/
   
    /*finally*/
    Response.WriteLine("finally");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Dispose();");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*finally*/
   
    Response.WriteLine("return false;");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;   
}
/*generate update function*/
public void GenerateInsertOrUpdate(string tableName,ColumnSchemaCollection columns)
{
       Response.IndentLevel=3;   
    string ColName =string.Empty;
    string ProcedureName = tableName + "_Insert_Update";
    int paramCount = columns.Count;
    string functionParams="public int InsertORUpdate(";
    string ParamDirection=string.Empty;
    for(int i=0;i< columns.Count;i++)
    {
       ColName = StringUtil.ToPascalCase(columns[i].Name);
       switch(columns[i].DataType)
       {
        case System.Data.DbType.Int32:
            functionParams= functionParams + "int " + ColName;
            break;
        case System.Data.DbType.AnsiString:
            functionParams= functionParams + "string " + ColName;
            break;
        case System.Data.DbType.AnsiStringFixedLength:
            functionParams= functionParams + "char " + ColName;
            break;
        case System.Data.DbType.DateTime:
            functionParams= functionParams +  "DateTime " + ColName;
            break;
        case System.Data.DbType.Double:
            functionParams= functionParams +  "double " + ColName;
            break;
        }
        if(i != columns.Count-1)
        {
            functionParams= functionParams + ",";   
        }
    }
    functionParams = functionParams +")";
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.ConnectionString = ConfigurationManager.ConnectionStrings[\"MSSQL\"].ToString();");
       
    /*try block*/
    Response.WriteLine("try");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Open();");
   
    Response.WriteLine("dbManager.CreateParameters(" + paramCount + ");");
   
    for(int j=0;j< columns.Count;j++)
    {
        ParamDirection="ParameterDirection.Input";
        ColName = StringUtil.ToPascalCase(columns[j].Name);
        Response.WriteLine("dbManager.AddParameters(" + (j).ToString() + ",\"@" + ColName + "\","+ ColName + ", " + ParamDirection + ");");
    }
    Response.WriteLine("this.AffectedRows = dbManager.ExecuteNonQuery(CommandType.StoredProcedure, \"" + ProcedureName + "\");");
    Response.WriteLine("return int.Parse(dbManager.Parameters[0].Value.ToString());");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*try block*/
   
    /*catch*/
    Response.WriteLine("catch(Exception ex)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.Unindent();
    Response.WriteLine("}");
    /*catch*/
   
    /*finally*/
    Response.WriteLine("finally");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("dbManager.Dispose();");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*finally*/
   
    Response.WriteLine("return 0;");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;   
}
/*Generate fetch function*/
public void GenerateFetch(string tableName,ColumnSchemaCollection columns)
{
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    Response.IndentLevel=3;   
    string ColName =string.Empty;
    string functionParams="public " + Table + " Fetch(int id)";
    string ServerFunction= "GetBy" + StringUtil.ToPascalCase(columns[0].Name);
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
       
    Response.WriteLine("DataSet ds = this." + ServerFunction + "(id);");
    Response.WriteLine(Table + " " + Table + "Obj = new " + Table + "();");
   
    Response.WriteLine(Table + "Obj." + Table + "EntityDBState = EntityDBState.Fetching;");
    Response.WriteLine(Table + "Obj." + StringUtil.ToPascalCase(columns[0].Name) + "= id;");
   
    string AssignLine=string.Empty;
    for(int j=1;j< columns.Count;j++)
    {
        ColName = StringUtil.ToPascalCase(columns[j].Name);
        switch(columns[j].DataType)
        {
        case System.Data.DbType.Int32:
            AssignLine = Table + "Obj." + ColName + "= int.Parse(ds.Tables[0].Rows[0][\"" + columns[j].Name + "\"].ToString());";
            break;
        case System.Data.DbType.AnsiString:
            AssignLine = Table + "Obj." + ColName + "= ds.Tables[0].Rows[0][\"" + columns[j].Name + "\"].ToString();";
            break;
        case System.Data.DbType.AnsiStringFixedLength:
            AssignLine = Table + "Obj." + ColName + "= ds.Tables[0].Rows[0][\"" + columns[j].Name + "\"].ToString().ToCharArray()[0];";
            break;
        case System.Data.DbType.DateTime:
            AssignLine = Table + "Obj." + ColName + "=(DateTime)ds.Tables[0].Rows[0][\"" + columns[j].Name + "\"];";
            break;
        case System.Data.DbType.Double:
            AssignLine = Table + "Obj." + ColName + "=(double)ds.Tables[0].Rows[0][\"" + columns[j].Name + "\"];";
            break;
        }
       
        Response.WriteLine(AssignLine);
    }
    Response.WriteLine(Table + "Obj." + Table + "EntityDBState = EntityDBState.UnChanged;");
    Response.WriteLine("return " + Table + "Obj;");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
/*Generate Save*/
public void GenerateSave(string tableName,ColumnSchemaCollection columns)
{
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    Response.IndentLevel=3;   
    string ColName =string.Empty;
    string functionParams="public void Save()";
    string ServerFunctionParam =string.Empty;
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("int LastInsertID;");
   
    /*if for insert*/
    Response.WriteLine("if (this." + Table + "EntityDBState == EntityDBState.Added)");
    Response.WriteLine("{");
    Response.Indent();
    for(int j=1;j< columns.Count;j++)
    {
        ColName = StringUtil.ToPascalCase(columns[j].Name);
        ServerFunctionParam = ServerFunctionParam + "this." + ColName;
        if(j != columns.Count-1)
        {
            ServerFunctionParam=ServerFunctionParam + ",";
        }
    }
    Response.WriteLine("LastInsertID = this.Insert(" + ServerFunctionParam + ");");
    Response.WriteLine("this." + Table + "EntityDBState = EntityDBState.UnChanged;");
    Response.WriteLine("this." + StringUtil.ToPascalCase(columns[0].Name) + " = LastInsertID;");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*if for insert*/
   
    /*if for update*/
    Response.WriteLine("if (this." + Table + "EntityDBState == EntityDBState.Changed)");
    Response.WriteLine("{");
   
    ServerFunctionParam = "this." + StringUtil.ToPascalCase(columns[0].Name)+ "," + ServerFunctionParam;
    Response.WriteLine("this.Update(" + ServerFunctionParam + ");");
    Response.WriteLine("this." + Table + "EntityDBState = EntityDBState.UnChanged;");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*if for update*/
   
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
/*Generate Delete*/
public void GenerateDelete(string tableName,ColumnSchemaCollection columns)
{
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    Response.IndentLevel=3;   
    string ColName =string.Empty;
    string functionParams="public new bool Delete()";
    string ServerFunctionParam =string.Empty;
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("bool DeleteSucceed=false;");
    /*if for insert*/
    Response.WriteLine("if(this." + Table + "EntityDBState == EntityDBState.MarkForDelete)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("DeleteSucceed=base.Delete(" + columns[0].Name + ");");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*if for insert*/
   
    /*if for update*/
    Response.WriteLine("if (DeleteSucceed == true)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("this." + Table + "EntityDBState = EntityDBState.Deleted;");
    Response.WriteLine("return true;");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*if for update*/
   
    Response.WriteLine("return false;");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateCompareTo(string tableName,ColumnSchemaCollection columns)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string ColName =string.Empty;
    string functionParams="public int CompareTo(object obj)";

    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("if (obj is " + Table + ")");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine(Table + " Other"+Table+" = obj as " + Table + ";");
    Response.WriteLine("switch (_SortMethod)");
    Response.WriteLine("{");
    Response.Indent();
   
    for(int j=0;j< columns.Count;j++)
    {
    ColName = StringUtil.ToPascalCase(columns[j].Name);
    /*case start*/
    Response.WriteLine("case SortMethod." + ColName + ":");
    Response.WriteLine("if (_SortOrder == SortOrder.Ascending)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("return this." + ColName + ".CompareTo(Other" + Table + "." + ColName + ");");
   
    Response.Unindent();
    Response.WriteLine("}");
   
    Response.WriteLine("else");
   
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("return Other" + Table + "." + ColName + ".CompareTo(this." + ColName + ");");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*case end*/
    }
    /*default start*/
    Response.WriteLine("default:");
    Response.WriteLine("if (_SortOrder == SortOrder.Ascending)");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("return this." + ColName + ".CompareTo(Other" + Table + "." + StringUtil.ToPascalCase(columns[0].Name) + ");");
   
    Response.Unindent();
    Response.WriteLine("}");
   
    Response.WriteLine("else");
   
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("return Other" + Table + "." + ColName + ".CompareTo(this." + StringUtil.ToPascalCase(columns[0].Name) + ");");
   
    Response.Unindent();
    Response.WriteLine("}");
    /*default end*/
    Response.Unindent();
    Response.WriteLine("}");
   
   
    Response.Unindent();
    Response.WriteLine("}");
   
    Response.WriteLine("else");
   
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("throw new ArgumentException(\"Object to compare is not a Dept Object\");");
   
    Response.Unindent();
    Response.WriteLine("}");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateCollectionAdd(string tableName)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string functionParams="public virtual void Add(" + Table + " new" + Table + ")";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("List.Add(new" + Table + ");");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateCollectionRemove(string tableName)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string functionParams="public virtual void Remove(" + Table + " new" + Table + ")";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("List.Remove(new" + Table + ");");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateCollectionIndexer(string tableName)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string functionParams="public "+ Table + " this[int index]";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("get");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("return (" + Table + ")this.List[index];");
   
    Response.Unindent();
    Response.WriteLine("}");
   
    Response.WriteLine("set");
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("this.List[index] = value;");
   
    Response.Unindent();
    Response.WriteLine("}");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateCollectionConstructor(string tableName)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string functionParams="public " + Table + "s(" + Table + "s new" + Table + "s)";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("this.InnerList.AddRange(new" + Table + "s);");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateCollectionCopyTo(string tableName)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string functionParams="public void CopyTo(" + Table + "[] array, int index)";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("List.CopyTo(array, index);");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateCollectionIndexOf(string tableName)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string functionParams="public int IndexOf(" + Table + " new" + Table + ")";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("return base.List.IndexOf(new" + Table + ");");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateCollectionInsert(string tableName)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string functionParams="public void Insert(int index, " + Table + " item)";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("List.Insert(index, item);");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateCollectionContains(string tableName)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string functionParams="public bool Contains(" + Table + " new" + Table + ")";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("return List.Contains(new" + Table + ");");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateCollectionReadOnly(string tableName)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string functionParams="public static " + Table + "s ReadOnly(" + Table + "s coll)";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("return new " + Table + "s.ReadOnly" + Table + "s(coll);");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateReadOnlyConstructor(string tableName)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string functionParams="internal ReadOnly" + Table + "s(" + Table + "s coll): base(coll)";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
       
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateReadOnlyAdd(string tableName)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string functionParams="public override void Add(" + Table + " value)";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("throw new NotSupportedException(ERROR_STRING);");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateReadOnlyRemove(string tableName)
{
    Response.IndentLevel=3;
    string Table = StringUtil.ToPascalCase(tableName.Split('_')[0]);
    string functionParams="public override void Remove(" + Table + " value)";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("throw new NotSupportedException(ERROR_STRING);");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateReadOnlyOnClear()
{
    Response.IndentLevel=3;
    string functionParams="protected override void OnClear()";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("throw new NotSupportedException(ERROR_STRING);");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateReadOnlyOnInsert()
{
    Response.IndentLevel=3;
    string functionParams="protected override void OnInsert(int index, object value)";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("throw new NotSupportedException(ERROR_STRING);");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateReadOnlyOnRemove()
{
    Response.IndentLevel=3;
    string functionParams="protected override void OnRemove(int index, object value)";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("throw new NotSupportedException(ERROR_STRING);");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateReadOnlyOnSet()
{
    Response.IndentLevel=3;
    string functionParams="protected override void OnSet(int index, object oldValue, object newValue)";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("throw new NotSupportedException(ERROR_STRING);");
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateEnumSortMethod(ColumnSchemaCollection columns)
{
    Response.IndentLevel=3;
    string functionParams="public enum SortMethod";
   
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    string EnumLine=string.Empty;
    for(int j=0;j< columns.Count;j++)
    {
     EnumLine = StringUtil.ToPascalCase(columns[j].Name) + "=" + j.ToString();
     if(j!=columns.Count-1)
     {
        EnumLine=EnumLine+",";
     }
     Response.WriteLine(EnumLine);
    }
   
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
public void GenerateValuesEquals(string TableName,ColumnSchemaCollection Columns)
{
    Response.IndentLevel=3;
    string functionParams="public bool ValuesEquals(object obj)";
    string FClass=StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0]);
    string StrMiddleLine=string.Empty;
    Response.WriteLine(functionParams);
    Response.WriteLine("{");
    Response.Indent();
   
    Response.WriteLine("if (obj == null) return false;");
    Response.WriteLine("if (Object.ReferenceEquals(this, obj)) return true;");
    Response.WriteLine(FClass + " obj" + FClass + "=(" + FClass + ")obj;");
    Response.WriteLine("if (this." + Columns[0].Name + ".Equals(obj" + FClass + "." + Columns[0].Name +")) return false;");
    for(int j=0;j< Columns.Count;j++)
    {
        StrMiddleLine=string.Empty;
        StrMiddleLine="if (this." + Columns[j].Name + ".Equals(objModule." + Columns[j].Name + ")) return false;";
        Response.WriteLine(StrMiddleLine);
    }
    Response.WriteLine("return true;");
    Response.Unindent();
    Response.WriteLine("}");
    Response.IndentLevel=1;
}
</script>


   Here I am using the .NetTier Stored procedure along with one added by me for insert or update that was in  available in my last storeprocedure codesmith template related post.

No comments:

Post a Comment