Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Implementing Model-View-Presenter in ASP.NET

, 17 Nov 2007
Three implementations of Model-View-Presenter in ASP.NET 2.0.
MVPSampleApp.zip
MVP.SampleApp
Lib
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Data.dll
MySql.Data.dll
nunit.framework.dll
Rhino.Mocks.dll
Model
Data
Interfaces
Properties
Presentation
Presentation.Tests
Properties
Interfaces
Properties
SubSonic
ActiveRecord
Builder
CodeGeneration
Templates
CodeLanguage
Configuration
Controls
Calendar
lang
skin
active-bg.gif
calendar.gif
dark-bg.gif
hover-bg.gif
menuarrow.gif
normal-bg.gif
rowhover-bg.gif
status-bg.gif
title-bg.gif
today-bg.gif
Resources
DataProviders
Properties
Sql Tools
SubSonic.snk
Sugar
WebApp
App_Data
Views
SQL2000SampleDb.zip
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System;

namespace SubSonic
{
    [Serializable]
    class ListComparer<ItemType> : Comparer<ItemType> 
        where ItemType : AbstractRecord<ItemType>, new()
    {
        private string columnName;

        public string ColumnName
        {
            get { return columnName; }
            set { columnName = value; }
        }
        private bool ascending;

        public bool Ascending
        {
            get { return ascending; }
            set { ascending = value; }
        }

        private DbType dbType = DbType.String;

        public DbType DBType
        {
            get { return dbType; }
            set { dbType = value; }
        }

        public override int Compare(ItemType x, ItemType y)
        {

            object xVal = x.GetColumnValue<object>(columnName);
            object yVal = y.GetColumnValue<object>(columnName);
            int result;

            if (dbType == DbType.String || dbType == DbType.Guid)
            {
                string sX = xVal.ToString();
                string sY = yVal.ToString();
                result = sX.CompareTo(sY);
            }
            else if (dbType == DbType.DateTime)
            {
                DateTime dX = Convert.ToDateTime(xVal);
                DateTime dY = Convert.ToDateTime(yVal);
                result = dX.CompareTo(dY);
            }
            else if (dbType == DbType.Boolean)
            {
                bool bX = Convert.ToBoolean(xVal);
                bool bY = Convert.ToBoolean(yVal);
                result = bX.CompareTo(bY);
            }
            else
            {
                double dX = Convert.ToDouble(xVal);
                double dY = Convert.ToDouble(yVal);
                result = dX.CompareTo(dY);
            }

            if (!ascending)
            {
                result *= -1;
            }
            return result;
        }
    }

    [Serializable]
    public abstract class AbstractList<ItemType, ListType> : List<ItemType>, ITypedList
        where ItemType : AbstractRecord<ItemType>, new()
        where ListType : AbstractList<ItemType, ListType>, new()
    {
        public void Sort(string columnName, bool ascending)
        {
            ListComparer<ItemType> compare = new ListComparer<ItemType>();
            ItemType item = new ItemType();
            DbType dbType = item.GetDBType(columnName);
            compare.Ascending = ascending;
            compare.ColumnName = columnName;
            compare.DBType = dbType;

            Sort(compare);
        }

        private string _providerName;
        
        public string ProviderName {
            get { return _providerName; }
            set { _providerName = value; }
        }
        
        /// <summary>
        /// Loads the collection and leaves the IDataReader open.
        /// </summary>
        /// <param name="rdr"></param>
        public void Load(IDataReader rdr)
        {
            while (rdr.Read())
            {
                ItemType item = new ItemType();
                item.Load(rdr);
                Add(item);
            }
        }

        /// <summary>
        /// Loads the collection by iterating through the rows in the supplied DataTable.
        /// </summary>
        /// <param name="tbl"></param>
        public void Load(DataTable tbl)
        {
            foreach (DataRow dr in tbl.Rows)
            {
                ItemType item = new ItemType();
                item.Load(dr);
                Add(item);
            }
        }

        /// <summary>
        /// Loads the collection and closes the IDataReader.
        /// </summary>
        /// <param name="rdr"></param>
        public void LoadAndCloseReader(IDataReader rdr)
        {
            Load(rdr);
            rdr.Close();
        }

        #region Utility
        protected static void CheckLogicalDelete(Query q)
        {
            //check the columns and see if there's a "deleted" or "isDeleted"
            q.CheckLogicalDelete();
        }

        /// <summary>
        /// Returns the collection's items in a DataTable
        /// </summary>
        /// <returns></returns>
        public DataTable ToDataTable() {
            DataTable tblOut = new DataTable();

            //create the columns
            ItemType schema = new ItemType();

            //get the schema from the object
            TableSchema.TableColumnSettingCollection settings = schema.GetColumnSettings();
            
            //add the columns
            foreach (TableSchema.TableColumnSetting setting in settings) {
                DataColumn col = new DataColumn(setting.ColumnName);
                tblOut.Columns.Add(col);
            }

            //set the values
            foreach (ItemType item in this) {
                item.CopyTo(tblOut);
                
            }
            return tblOut;
        }

        public void CopyFrom(AbstractList<ItemType, ListType> copyInstance) {
            foreach (ItemType item in copyInstance) {
                ItemType newItem = item.Clone();
                Add(newItem);
            }
        }

        public void CopyTo(AbstractList<ItemType, ListType> copyInstance) {
            foreach (ItemType item in this) {
                ItemType newItem = item.Clone();
                copyInstance.Add(newItem);
            }
        }
        #endregion

        #region WHERE and ORDER BY
        protected List<Where> wheres = new List<Where>();
        protected List<BetweenAnd> betweens = new List<BetweenAnd>();
        protected OrderBy orderBy;

        public ListType OrderByAsc(string columnName)
        {
            orderBy = OrderBy.Asc(columnName);
            return this as ListType;
        }

        public ListType OrderByDesc(string columnName)
        {
            orderBy = OrderBy.Desc(columnName);
            return this as ListType;
        }

        public ListType Where(Where where)
        {
            wheres.Add(where);
            return this as ListType;
        }

        public ListType Where(string columnName, object value)
        {
            if (value != DBNull.Value && value != null)
            {
                return Where(columnName, Comparison.Equals, value);
            }

            else
            {
                return Where(columnName, Comparison.Is, DBNull.Value);
            }
        }

        public ListType Where(string columnName, Comparison comp, object value)
        {
            Where where = new Where();
            where.ColumnName = columnName;
            where.Comparison = comp;
            where.ParameterValue = value;
            Where(where);
            return this as ListType;
        }

        public ListType BetweenAnd(string columnName, DateTime dateStart, DateTime dateEnd)
        {
            BetweenAnd between = new BetweenAnd();
            between.ColumnName = columnName;
            between.StartDate = dateStart;
            between.EndDate = dateEnd;
            between.StartParameterName = "start" + columnName;
            between.EndParameterName = "end" + columnName;
            betweens.Add(between);
            return this as ListType;
        }

        public ListType Load()
        {
            Query qry = new Query(new ItemType().GetSchema());
            CheckLogicalDelete(qry);
            foreach(Where where in wheres)
            {
                qry.AddWhere(where);
            }

            foreach(BetweenAnd between in betweens)
            {
                qry.AddBetweenAnd(between);
            }

            if(orderBy != null)
            {
                qry.OrderBy = orderBy;
            }

            using(IDataReader rdr = qry.ExecuteReader())
            {
                LoadAndCloseReader(rdr);
                return this as ListType;
            }
        }   

        #endregion

        #region ITypedList Members

        public string GetListName(PropertyDescriptor[] listAccessors)
        {
            return String.Empty;
        }

        public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
        {
            if ((listAccessors == null) || listAccessors.Length == 0)
            {
                return GetPropertyDescriptors(typeof(ItemType));
            }
            return null;
        }

        #endregion

        private static PropertyDescriptorCollection GetPropertyDescriptors(Type typeOfObject)
        {
            PropertyDescriptorCollection typePropertiesCollection = TypeDescriptor.GetProperties(typeOfObject);
            ArrayList propertyDescriptorsToUse = new ArrayList();

            foreach (PropertyDescriptor property in typePropertiesCollection)
            {
                HiddenForDataBindingAttribute hiddenAttribute =(HiddenForDataBindingAttribute)property.Attributes[typeof(HiddenForDataBindingAttribute)];

                if (hiddenAttribute != null && hiddenAttribute.IsHidden)
                {
                    continue;
                }
                propertyDescriptorsToUse.Add(property);
            }

            return new PropertyDescriptorCollection((PropertyDescriptor[])propertyDescriptorsToUse.ToArray(typeof(PropertyDescriptor)));
        }
    }
}

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)

Share

About the Author

Alex Mueller
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 17 Nov 2007
Article Copyright 2007 by Alex Mueller
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid