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

Win-Form/Web-Form Generic Components using the same Controller

Rate me:
Please Sign up or sign in to vote.
4.00/5 (7 votes)
5 Oct 2008CPOL7 min read 43K   1.1K   36  
A framework to develop Win-Form and Web-Form applications using generic components
using System;
using System.Collections.Generic;
using System.Data;

namespace Framework.baInterfaces
{
    /// <summary>
    /// The Change (Add, Edit, Delete) request being 
    /// transfred from Controller Insert/Update/Delete
    /// to Model layer with field values in the Token
    /// </summary>
    #region baToken
    public class baToken
    {
        #region FieldAttributeEnum
        /// A bit field or flag enumeration of a field attribute.
        [Flags]
        public enum FieldAttributeEnum
        {
            isIncluded = 1, /// The field is included in operation
            isInPrimaryKeys = 2, /// The field is PrimaryKey in operation
            All = isIncluded | isInPrimaryKeys 
        }
        #endregion

        /// <summary>
        /// This is the field info including
        /// whether or not it is a PrimaryKey, 
        /// AutoInc and  Included in operation
        /// and the value the field has
        /// </summary>
        #region baField class type
        public class baField
        {
            private object _val; /// The Value of the field
            private DataColumn _Column; /// The DataColumn of the field
            private FieldAttributeEnum _fieldAttribute;
            private byte _precision; 

            #region FieldValue
            public object FieldValue
            {
                get
                {
                    return _val;
                }
                set
                {
                    _val = value;
                }
            }
            #endregion

            #region Column
            public DataColumn Column 
            {
                get
                {
                    return _Column;
                }
            }
            #endregion

            #region Size
            public int Size
            {
                get
                {
                    return _Column.MaxLength;
                }
            }
            #endregion

            #region Precision
            public byte Precision
            {
                get
                {
                    return _precision;
                }
                set
                {
                    _precision = value;
                }
            }
            #endregion

            #region IsPrimaryKeys
            public bool IsPrimaryKey
            {
                get
                {
                    return ((_fieldAttribute & FieldAttributeEnum.isInPrimaryKeys) == 0 ? false : true);
                }
                set
                {
                    _fieldAttribute =
                        (value ?
                            _fieldAttribute | FieldAttributeEnum.isInPrimaryKeys : /// isIncluded be true
                            _fieldAttribute & ~FieldAttributeEnum.isInPrimaryKeys); /// isIncluded be false
                }
            }
            #endregion

            #region IsAuto
            public bool IsAuto
            {
                get
                {
                    return _Column.AutoIncrement;
                }
                set
                {
                    _Column.AutoIncrement = value;
                }
            }
            #endregion

            #region IsIncluded
            public bool IsIncluded
            {
                get
                {
                    return ((_fieldAttribute & FieldAttributeEnum.isIncluded) == 0 ? false : true);
                }
                set
                {
                    _fieldAttribute =
                        (value ?
                            _fieldAttribute | FieldAttributeEnum.isIncluded : /// isIncluded be true
                            _fieldAttribute & ~FieldAttributeEnum.isIncluded); /// isIncluded be false
                }
            }
            #endregion

            #region Constructor
            public baField(DataColumn column, byte precision, object val, bool isPrimaryKey, bool isIncluded)
            {
                this._Column = column;
                this._precision = precision;
                this._val = val;
                this.IsIncluded = isIncluded;
                this.IsPrimaryKey = isPrimaryKey;
            }
            public baField(DataColumn column, object val, bool isPrimaryKey, bool isIncluded)
            {
                this._Column = column;
                this._precision = 0;
                this._val = val;
                this.IsIncluded = isIncluded;
                this.IsPrimaryKey = isPrimaryKey;
            }
            public baField(DataColumn column, bool isPrimaryKey, bool isIncluded)
            {
                this._Column = column;
                this._precision = 0;
                this._val = null;
                this.IsIncluded = isIncluded;
                this.IsPrimaryKey = isPrimaryKey;
            }
            private baField()
            {
            }
            #endregion

            #region bool Equals(baField obj)
            /// <summary>
            /// June 15, 06
            /// Compare this instance of baField with obj. 
            /// </summary>
            /// <param name="obj">baField instance to be compared with this one</param>
            /// <returns>true if the instances are equal else false</returns>
            public bool Equals(baField obj)
            {
                if (this._fieldAttribute == obj._fieldAttribute &&
                    this._val == obj._val)
                    return true;
                else
                    return false;
            }
            #endregion

            #region bool EqualAttributes(baField obj)
            /// <summary>
            /// June 15, 06
            /// Compare the structure of this instance of baField with obj. 
            /// </summary>
            /// <param name="obj">baField instance to be compared with this one</param>
            /// <returns>true if the instances are equal else false</returns>
            public bool EqualAttributes(baField obj)
            {
                if (this._fieldAttribute == obj._fieldAttribute)
                    return true;
                else
                    return false;
            }
            #endregion
        }
        #endregion

        #region baField Fields and TableName values
        private Dictionary<string, baField> fields = null;
        private string tableName = null;
        private DataTable table;
        #region string TableName
        public string TableName
        {
            get
            {
                return tableName;
            }
        }
        #endregion

        public DataTable Table
        {
            get { return table; }
        }

        #region Dictionary<string, baField> Fields
        public Dictionary<string, baField> Fields
        {
            get
            {
                return fields;
            }
        }
        #endregion

        #region baField this[string FieldName]
        public baField this[string FieldName]
        {
            get
            {
                if (Fields.ContainsKey(FieldName))
                    return Fields[FieldName];
                else
                    return null;

            }
            set
            {
                if (Fields.ContainsKey(FieldName))
                    Fields[FieldName] = value;
                else
                    Fields.Add(FieldName, value);
            }
        }
        #endregion

        #region baField[] PrimaryKeys
        public baField[] PrimaryKeys
        {
            get
            {
                // Find the Pk field names
                string[] isPkCol = new string[this.Count];
                int i=0;
                foreach (KeyValuePair<string, baField> kvp in fields)
                {
                    if (kvp.Value.IsPrimaryKey)
                        isPkCol[i++] = kvp.Value.Column.ColumnName;
                }
                baField[] pkCols = new baField[i];
                for (int j = 0; j < i;j++ )
                {
                    pkCols[j] = this[isPkCol[j]];
                }
                return pkCols;
            }
        }
        #endregion

        #region baField AutoField
        public baField AutoField
        {
            get
            {
                foreach (KeyValuePair<string, baField> kvp in fields)
                {
                    if (kvp.Value.IsAuto)
                        return kvp.Value;
                }
                return null;
            }
        }
        #endregion

        #region int Count
        public int Count
        {
            get
            {
                return fields.Count;
            }
        }
        #endregion

        #endregion

        #region Constructor
        public baToken(DataTable _table)
        {
            this.table = _table;
            this.tableName = _table.TableName;
            fields = new Dictionary<string, baField>();
        }
        #endregion

        #region bool Equals(baToken obj)
        /// <summary>
        /// June 15, 06
        /// Compare this instance of baToken with obj. It compares the
        /// structure of two baTokens (Fields) not the values in Fields
        /// </summary>
        /// <param name="obj">baToken instance to be compared with this one</param>
        /// <returns>true if the instances are equal else false</returns>
        public bool Equals(baToken obj)
        {
            if (this.Count == obj.Count && this.TableName==obj.TableName)
            {
                return base.Equals(obj);
            }
            else
                return false;
        }
        #endregion

        #region Contains(baToken token)
        /// <summary>
        /// June 15, 06
        /// Check to see if this baToken is holding token fields structure
        /// </summary>
        /// <param name="token">the token to check their fields existance in this baToken</param>
        /// <returns>true if ba token has these fields else false</returns>
        public bool Contains(baToken token)
        {
            if (this.Count >= token.Count && this.TableName == token.TableName)
            {
                /// Move on the fields of the sent token to see
                /// if this token has exactly the sent token fields
                foreach (KeyValuePair<string, baField> fld in token.Fields)
                {
                    if (this.Fields.ContainsKey(fld.Key))
                    {
                        if (!this.Fields[fld.Key].EqualAttributes(fld.Value))
                            return false;
                    }
                    else
                        return false;
                }
                return true;
            }
            else
                return false;
        }
        #endregion
    }
    #endregion

    /// <summary>
    /// This is the interface that is being used in 
    /// Transportation and Controller layer to 
    /// communicate with Model layer
    /// </summary>
    #region IBaseModel
    public interface IBaseModel
    {
        DataSet GetDataSet();
        int FetchData(DataSet ds, string tableName);
        int FetchData(DataSet ds, string tableName, string filter);
        int FetchData(DataTable dataTable);
        int FetchData(DataTable dataTable, string filter);

        int Insert(baToken theFields);
        int Update(baToken theFields);
        int Delete(baToken theFields);
        int Select(baToken theFields);
    }
    #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