Click here to Skip to main content
11,489,574 members (65,121 online)
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 CPOL 97.1K 2.6K 122
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
SQL2000SampleDb.msi
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using SubSonic.Utilities;

namespace SubSonic
{


    /*
     * This set of classes abstracts out commands and their parameters so that
     * the DataProviders can work their magic regardless of the client type. The
     * System.Data.Common class was supposed to do this, but sort of fell flat
     * when it came to MySQL and other DB Providers that don't implement the Data
     * Factory pattern.
     * 
    */

    /// <summary>
    /// Abstracts out the assignment of parameters, etc
    /// </summary>
    public class QueryParameter
    {
    	internal const int DefaultSize = 50;
		internal const ParameterDirection DefaultParameterDirection = ParameterDirection.Input;

        private int _size = DefaultSize;

        public int Size
        {
            get { return _size; }
            set { _size = value; }
        }

		private ParameterDirection _mode = DefaultParameterDirection;

        public ParameterDirection Mode
        {
            get { return _mode; }
            set { _mode = value; }
        }

        private string parameterName;

        public string ParameterName
        {
            get { return parameterName; }
            set { parameterName = value; }
        }

        private object parameterValue;

        public object ParameterValue
        {
            get { return parameterValue; }
            set { parameterValue = value; }
        }
        private DbType dataType;

        public DbType DataType
        {
            get { return dataType; }
            set { dataType = value; }
        }


    }

    public class QueryParameterCollection : List<QueryParameter>
    {
        public void Add(string parameterName, object value)
        {
            QueryParameter param = new QueryParameter();
            param.ParameterName = parameterName;
            param.ParameterValue = value;
            Add(param);
        }
        public void Add(string parameterName, object value, DbType dataType)
        {
            QueryParameter param = new QueryParameter();
            param.ParameterName = parameterName;
            param.ParameterValue = value;
            param.DataType = dataType;
            Add(param);

        }
        public void Add(string parameterName, object value, DbType dataType, ParameterDirection mode)
        {
            QueryParameter param = new QueryParameter();
            param.ParameterName = parameterName;
            param.ParameterValue = value;
            param.DataType = dataType;
            param.Mode = mode;
            Add(param);

        }
    }

    public class QueryCommandCollection : List<QueryCommand>
    {

    }

    public class QueryCommand
    {

        private int commandTimeout=60;

        /// <summary>
        /// Connection timeout in seconds
        /// </summary>
        public int CommandTimeout {
            get { return commandTimeout; }
            set { commandTimeout = value; }
        }
	
        public List<object> OutputValues;

        public bool HasOutputParams()
        {
            bool bOut = false;

            //loop the params and see if one is in/out
            foreach (QueryParameter param in Parameters)
            {
                if (param.Mode == ParameterDirection.InputOutput || param.Mode == ParameterDirection.Output || param.Mode == ParameterDirection.ReturnValue)
                {
                    bOut = true;
                    break;
                }
            }

            return bOut;
        }

        private string _providerName = string.Empty;

        public string ProviderName
        {
            get { return _providerName; }
            set { _providerName = value; }
        }

        private DataProvider _provider;
        public DataProvider Provider
        {
            get { return _provider; }
            set { _provider = value; }
        }

        private CommandType commandType;

        public CommandType CommandType
        {
            get { return commandType; }
            set { commandType = value; }
        }

        public QueryCommand(string sql, string providerName)
        {
            ProviderName = providerName;
            Provider = DataService.GetInstance(providerName);
            commandSql = sql;
            CommandType = CommandType.Text;
            parameters = new QueryParameterCollection();
            OutputValues = new List<object>();
        }

        public QueryCommand(string sql)
        {
            //use the default
            ProviderName = DataService.Provider.Name;
            Provider = DataService.Provider;
            commandSql = sql;
            CommandType = CommandType.Text;
            parameters = new QueryParameterCollection();
            OutputValues = new List<object>();
        }

        //public QueryCommand()
        //{
        //    CommandType = CommandType.Text;
        //    parameters = new QueryParameterCollection();
        //    OutputValues = new List<object>();
        //}

        private string commandSql;

        public string CommandSql
        {
            get { return commandSql; }
            set { commandSql = value; }
        }

        private QueryParameterCollection parameters;

        public QueryParameterCollection Parameters
        {
            get { return parameters; }
            set { parameters = value; }
        }

		// The public AddParameter methods should call this one.
		private void AddParameter(string parameterName, object parameterValue, int maxSize, DbType dbType, ParameterDirection direction)
		{
			if (parameters == null)
			{
				parameters = new QueryParameterCollection();
			}

			QueryParameter param = new QueryParameter();
            param.ParameterName = Utility.PrefixParameter(parameterName, Provider);
			param.ParameterValue = parameterValue ?? DBNull.Value;
			param.Mode = direction;
			param.DataType = dbType;
			if (maxSize > -1)
			{
				param.Size = maxSize;
			}
			parameters.Add(param);
		}

        public void AddParameter(string parameterName, object parameterValue, DbType dataType, ParameterDirection parameterDirection)
        {
            AddParameter(parameterName, parameterValue, QueryParameter.DefaultSize, dataType, parameterDirection);
        }

        public void AddParameter(string parameterName, object parameterValue, DbType dataType)
        {
			AddParameter(parameterName, parameterValue, QueryParameter.DefaultSize, dataType, QueryParameter.DefaultParameterDirection);
        }

        public void AddParameter(string parameterName, object parameterValue)
        {
            AddParameter(parameterName, parameterValue, DbType.AnsiString);
        }

        public void AddOutputParameter(string parameterName, int maxSize, DbType dbType)
        {
        	AddParameter(parameterName, DBNull.Value, maxSize, dbType, ParameterDirection.Output);
        }

		public void AddOutputParameter(string parameterName, int maxSize)
        {
            AddOutputParameter(parameterName, maxSize, DbType.AnsiString);
        }

        public void AddOutputParameter(string parameterName)
        {
            AddOutputParameter(parameterName, -1, DbType.AnsiString);
        }

        public void AddOutputParameter(string parameterName, DbType dbType)
        {
            AddOutputParameter(parameterName, -1, dbType);
        }

		/// <summary>
		/// Adds a return parameter (@RETURN_VALUE) to the command.
		/// </summary>
		public void AddReturnParameter()
		{
			AddParameter("@RETURN_VALUE", null, DbType.Int32, ParameterDirection.ReturnValue);
		}

    	public IDbCommand ToIDbCommand()
        {
            return DataService.GetIDbCommand(this);
        }

        public DbCommand ToDbCommand()
        {
            return DataService.GetDbCommand(this);
        }
    }
}

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 | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 17 Nov 2007
Article Copyright 2007 by Alex Mueller
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid