Click here to Skip to main content
15,896,606 members
Articles / Web Development / HTML

Windows and Web Generic Components

Rate me:
Please Sign up or sign in to vote.
4.08/5 (7 votes)
23 Jul 2008CPOL4 min read 27.4K   742   15  
A method to create Windows and Web components with the same interface
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data.Common;
using System.Data;
using Framework.DAL;
using Framework.Lib;
using Framework.baInterfaces;

namespace App.BusinessController
{
    /// <summary>
    /// Use DAL to fetch data from database
    /// and do database transactions.
    /// BaseModel is a creational object that
    /// will create a TableModel object for a
    /// request comming from Transportation Layer,
    /// If the TableModel object was created, then 
    /// use it, otherwise create a new one and use it.
    /// The TableModel objects are being created in Model
    /// creator object in a hash table that holds them.
    /// Note:
    ///     - In windows form, the TableModel object 
    ///     will be created just once and be used
    ///     on all the up comiing requests from GUI.
    ///     - In web forms, the TableModel object 
    ///     will be created each time a request is comming
    ///     from the web page, to improve the effeciency
    ///     the TableModel objects can be created inside a
    ///     COM+ object with proper time-to-live and JIT settings
    /// </summary>
    public class BaseModel : IBaseModel
    {
        protected DBase database;
        protected Hashtable modelTables;

        #region Constructor
        /// <summary>
        /// BaseModel Constructor
        /// </summary>
        public BaseModel()
        {
			try
			{
				database = new DBase();
                modelTables = new System.Collections.Hashtable();
			}
            catch (Exception baExp)
            {
                throw new baDALException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
            }
        }
        #endregion

        #region Destructor
        /// <summary>
        /// Destructor
        /// </summary>
        /// <param name="disposing"></param>
        protected void Dispose(bool disposing)
        {
            try
            {
                database.close();
            }
            catch (Exception baExp)
            {
                throw new baDALException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
            }
        }
        #endregion

        #region DataSet GetDataSet()
        /// <summary>
        /// Return Geo table dataset 
        /// </summary>
        /// <returns>Geo Dataset</returns>
        public DataSet GetDataSet()
        {
            return null;
        }
        #endregion

        #region int FetchData(System.Data.DataTable dataTable)
        /// <summary>
        /// Set a request to DAL to Fetch 
        /// a table data into a dataset
        /// </summary>
        /// <param name="ds">The dataset to be filled</param>
        /// <param name="tableName">Table name that exists in the Dataset</param>
        /// <returns></returns>
        public int FetchData(System.Data.DataTable dataTable)
        {
            int i = -1;
            try
            {
                database.open();
                i = database.FillDataSet(dataTable);
            }
            catch (Exception baExp)
            {
                throw new baDALException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
            }
            finally
            {
                database.close();
            }
            return i;
        }
        #endregion

        #region int FetchData(System.Data.DataSet ds, string tableName)
        /// <summary>
        /// Set a request to DAL to Fetch 
        /// a table data into a dataset
        /// </summary>
        /// <param name="ds">The dataset to be filled</param>
        /// <param name="tableName">Table name that exists in the Dataset</param>
        /// <returns></returns>
        public int FetchData(System.Data.DataSet ds, string tableName)
        {
            int i = -1;
            try
            {
                database.open();
                i = database.FillDataSet(ds, tableName);
            }
            catch (Exception baExp)
            {
                throw new baDALException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
            }
            finally
            {
                database.close();
            }
            return i;
        }
        #endregion

        #region int FetchData(System.Data.DataTable dataTable, string filter)
        /// <summary>
        /// Set a request to DAL to Fetch 
        /// a table data into a dataset
        /// </summary>
        /// <param name="ds">The dataset to be filled</param>
        /// <param name="tableName">Table name that exists in the Dataset</param>
        /// <returns></returns>
        public int FetchData(System.Data.DataTable dataTable, string filter)
        {
            int i = -1;
            try
            {
                database.open();
                i = database.FillDataSet(dataTable, filter);
            }
            catch (Exception baExp)
            {
                throw new baDALException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
            }
            finally
            {
                database.close();
            }
            return i;
        }
        #endregion

        #region int FetchData(System.Data.DataSet ds, string tableName, string filter)
        /// <summary>
        /// Set a request to DAL to Fetch 
        /// a table data into a dataset
        /// </summary>
        /// <param name="ds">The dataset to be filled</param>
        /// <param name="tableName">Table name that exists in the Dataset</param>
        /// <returns></returns>
        public int FetchData(System.Data.DataSet ds, string tableName, string filter)
        {
            int i = -1;
            try
            {
                database.open();
                i = database.FillDataSet(ds, tableName, filter);
            }
            catch (Exception baExp)
            {
                throw new baDALException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
            }
            finally
            {
                database.close();
            }
            return i;
        }
        #endregion

        #region object CreateTableModel(baToken theFields)
        private object CreateTableModel(baToken theFields)
        {
            /// Create a TableModel object for the 
            /// TableName if it was not existed before
            if (modelTables[theFields.TableName] == null ||
                !((TableModel)modelTables[theFields.TableName]).Token.Contains(theFields)) // June 15,06: or if this table model has the passed fields to this function
                modelTables[theFields.TableName] = new TableModel(database, theFields);
            return modelTables[theFields.TableName];
        }
        #endregion

        #region int Insert(baToken theFields)
        public int Insert(baToken theFields)
        {
            if (CreateTableModel(theFields) != null)
            {
                ((TableModel)modelTables[theFields.TableName]).Insert(theFields);
                int id = Select(theFields);
                return id;
            }
            else
                return -1;
        }
        #endregion

        #region int Update(baToken theFields)
        public int Update(baToken theFields)
        {
            if (CreateTableModel(theFields) != null)
            {
                ((TableModel)modelTables[theFields.TableName]).Update(theFields);
                int id = Convert.ToInt32(theFields.AutoField.FieldValue);
                return id;
            }
            else
                return -1;
        }
        #endregion

        #region int Delete(baToken theFields)
        public int Delete(baToken theFields)
        {
            if (CreateTableModel(theFields) != null)
            {
                ((TableModel)modelTables[theFields.TableName]).Delete(theFields);
                int id = Convert.ToInt32(theFields.AutoField.FieldValue);
                return id; 
            }
            else
                return -1;
        }
        #endregion

        #region int Select(baToken theFields)

        public int Select(baToken theFields)
        {
            if (CreateTableModel(theFields) != null)
                return ((TableModel)modelTables[theFields.TableName]).Select(theFields);
            else
                return -1;
        }

        #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
Software Developer (Senior)
Canada Canada
Software development experience since 1993

Skills
- Programming Languages: C# .NET, Delphi, C++, Java and VB
- Development Methodologies (SDLC): RUP, Scrum and XP
- Database: SQL server, Oracle, Apollo database and MS Access.

Educations & Certificates
- Microsoft® Certified Professional (MCP)
- Sun® Certified Java2 Programmer
- B.S. in computer science

Comments and Discussions