Search This Blog

2010/07/31

The BLL CodeSmith Template

<%------------------------------------------------------------------------------------------
* 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].ToLower()) +"Base";;
string CoreClassName = StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0].ToLower()) +"Core";
string FrontClassName =StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0].ToLower());
string FrontCollectionClassName =StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0].ToLower())+"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].ToLower()) + "Base"); %>
    {
        #region Class_Variables
        private string _TableName=String.Empty;
        <%
        GenerateBaseClassClassVariables(SourceTable.Columns,SourceTable.Name);
        %>
        <%
        string EntityDBState = "private EntityDBState _" + StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0].ToLower()) + "EntityDBState=EntityDBState.Deafult;";
        string EntityValidationStatus = "private EntityValidationStatus _"+ StringUtil.ToPascalCase(SourceTable.Name.Split('_')[0].ToLower()) + "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].ToLower());
                    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
        {
        if (SourceTable.Columns[k].IsUnique==true)
            {
                GenerateGetByUniqueKey(SourceTable.Name,SourceTable.Columns[k].Name,SourceTable.Columns[k].DataType.ToString());
            }
        }
        %>
        protected 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
        };
        public SortMethod _SortMethod;
        public 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 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*/
        public bool ValuesEquals(object obj)
        {
            if (obj == null) return false;
            if (Object.ReferenceEquals(this, obj)) return true;
            if (this.GetType() != obj.GetType()) return false;
            <% Response.IndentLevel=4;%>
            <%Response.WriteLine(FrontClassName + " obj" + FrontClassName + "=(" + FrontClassName + ")obj;"); %>
            <%Response.WriteLine("if (this." + PrimeColName + ".Equals(obj" + FrontClassName + "." + PrimeColName +")) return true;");%>
            <% Response.IndentLevel=1;%>
            return false;
            <% Response.IndentLevel=1;%>
        }
        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.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
}

No comments:

Post a Comment