Click here to Skip to main content
11,577,710 members (62,014 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.9K 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.Data;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using SubSonic.Utilities;

namespace SubSonic
{
    public class StoredProcedure
    {
        public class Parameter
        {
            private DbType dbType;

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

	
            private string name;

            public string Name
            {
                get { return name; }
                set { name = value; }
            }

            private string displayName;

            public string DisplayName
            {
                get { return displayName; }
                set { displayName = value; }
            }

            private string queryParameter;

            public string QueryParameter
            {
                get { return queryParameter; }
                set { queryParameter = value; }
            }

            private ParameterDirection mode = ParameterDirection.Input;

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

        public class ParameterCollection : List<Parameter>
        {
        }

        private ParameterCollection _parameters = new ParameterCollection();

        public ParameterCollection Parameters
        {
            get { return _parameters; }
            set { _parameters = value; }
        }

        private DataProvider _provider;

        public DataProvider Provider
        {
            get { return _provider; }
        }

        public List<object> OutputValues;

        private QueryCommand _cmd;

        public QueryCommand Command
        {
            get { return _cmd; }
            set { _cmd = value; }
        }

        public IDataReader GetReader()
        {
            IDataReader result = DataService.GetReader(_cmd);
            OutputValues = _cmd.OutputValues;
            return result;
        }

        /// <summary>
        /// Executes an SP using the default provider and returns a reader
        /// </summary>
        /// <param name="spAndParams">The SP name and each argument required - these must be in order</param>
        /// <returns>System.Data.IDataReader</returns>
        public static IDataReader GetReader(string spAndParams)
        {
            return GetReader(spAndParams, "");
        }

        public static IDataReader GetReader(string spAndParams, string providerName) {
            QueryCommand cmd = new QueryCommand("EXEC " + spAndParams, providerName);
            cmd.CommandType = CommandType.Text;
            cmd.ProviderName = providerName;

            IDataReader result = DataService.GetReader(cmd);
            return result;
        }

        /// <summary>
        /// Executes an SP using the default provider
        /// </summary>
        /// <param name="spAndParams">The SP name and each argument required - these must be in order</param>
        /// <returns>System.Data.DataSet</returns>
        public static DataSet GetDataSet(string spAndParams)
        {
            return GetDataSet(spAndParams, String.Empty);
        }

        /// <summary>
        /// Executes an SP using the specified provider
        /// </summary>
        /// <param name="spAndParams">The SP name and each argument required - these must be in order</param>
		/// <param name="providerName">Specifies the specific provider to use.</param>
        /// <returns>System.Data.DataSet</returns>
        public static DataSet GetDataSet(string spAndParams, string providerName)
        {
            QueryCommand cmd = new QueryCommand("EXEC " + spAndParams, providerName);
            cmd.CommandType = CommandType.Text;
            cmd.ProviderName = providerName;
            return DataService.GetDataSet(cmd);
        }

        public DataSet GetDataSet()
        {
            return GetDataSet<DataSet>();
        }

        public T GetDataSet<T>() where T : DataSet, new()
        {
            T result = DataService.GetDataSet<T>(_cmd);
            OutputValues = _cmd.OutputValues;
            return result;
        }

		/// <summary>
		/// Executes the stored procedure and returns the number of rows affected.
		/// </summary>
		/// <returns></returns>
        public int Execute()
        {
            int rowsAffected = DataService.ExecuteQuery(_cmd);
            OutputValues = _cmd.OutputValues;
        	return rowsAffected;
        }

        public object ExecuteScalar()
        {
            object result = DataService.ExecuteScalar(_cmd);
            OutputValues = _cmd.OutputValues;
            return result;
        }

        private int commandTimeout = 60;

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

        private string name;

        public string Name
        {
            get { return name; }
        }

        private string displayName;

        public string DisplayName
        {
            get { return displayName; }
        }

        private string tableName;

        public string TableName
        {
            get { return tableName; }
        }

        public StoredProcedure(string spName)
            : this(spName, DataService.Provider)
        {
        }

        public StoredProcedure(string spName, DataProvider provider)
        {
            name = spName;
            tableName = provider.SPClassName;
            _cmd = new QueryCommand(spName, provider.Name);
            _cmd.CommandType = CommandType.StoredProcedure;
            _provider = provider;
            _cmd.ProviderName = _provider.Name;
            _cmd.CommandTimeout = this.commandTimeout;
            string newName = spName;

            if(provider.ExtractClassNameFromSPName)
            {
                //Order of if/else matters here!
                Match underscoreMatch = Regex.Match(spName, RegexPattern.EMBEDDED_CLASS_NAME_UNDERSCORE_MATCH);
                if(underscoreMatch.Success)
                {
                    tableName = underscoreMatch.Value;
                    newName = Regex.Replace(spName, RegexPattern.EMBEDDED_CLASS_NAME_UNDERSCORE_REPLACE, String.Empty);
                }
                else
                {
                    Match match = Regex.Match(spName, RegexPattern.EMBEDDED_CLASS_NAME_MATCH);
                    if(match.Success)
                    {
                        tableName = match.Value;
                        newName = Regex.Replace(spName, RegexPattern.EMBEDDED_CLASS_NAME_REPLACE, String.Empty);
                    }
                }
            }

            displayName = TransformSPName(newName, provider);
            //init the list so the count comes back properly
            OutputValues = new List<object>();
        }

        private static string TransformSPName(string spName, DataProvider provider)
        {
            if(String.IsNullOrEmpty(spName))
            {
                return string.Empty;
            }

            string newName = spName;
            newName = Utility.RegexTransform(newName, provider);
            newName = Utility.StripText(newName, provider.StripSPText);
            newName = Utility.GetProperName(newName, provider.RemoveUnderscores);
            newName = Utility.IsStringNumeric(newName) ? "_" + newName : newName;
            newName = Utility.StripNonAlphaNumeric(newName);
            newName = newName.Trim();
            return Utility.KeyWordCheck(newName, String.Empty, provider);
        }
    }
}

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

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 17 Nov 2007
Article Copyright 2007 by Alex Mueller
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid