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;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;

namespace SubSonic
{
    public class ELib3DataProvider : SqlDataProvider
    {
        #region Query Execution overrides
        public override IDataReader GetReader(QueryCommand qry)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd;
            if (qry.CommandType == CommandType.Text)
            {
                cmd = db.GetSqlStringCommand(qry.CommandSql);
            }
            else
            {
                cmd = db.GetStoredProcCommand(qry.CommandSql);

            }
            foreach (QueryParameter param in qry.Parameters)
            {
                db.AddInParameter(cmd, param.ParameterName, param.DataType, param.ParameterValue);
            }
            return db.ExecuteReader(cmd);

        }

		private static DbCommand GetDataSetCommand(Database db, QueryCommand qry)
		{
			DbCommand cmd;
			if (qry.CommandType == CommandType.Text)
				cmd = db.GetSqlStringCommand(qry.CommandSql);
			else
				cmd = db.GetStoredProcCommand(qry.CommandSql);

			foreach (QueryParameter param in qry.Parameters)
				db.AddInParameter(cmd, param.ParameterName, param.DataType, param.ParameterValue);

			return cmd;
		}

        public override T GetDataSet<T>(QueryCommand qry)
        {
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand cmd = GetDataSetCommand(db, qry);

			T ds = null;
			if (typeof(T) == typeof(DataSet)) // LoadDataSet doesn't accept empty table names.
				ds = (T)db.ExecuteDataSet(cmd);
			else
			{
				ds = new T();
				db.LoadDataSet(cmd, ds, GetTableNames(ds));
			}
			return ds;
        }

        public override object ExecuteScalar(QueryCommand qry)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd;

            if (qry.CommandType == CommandType.Text)
            {
                cmd = db.GetSqlStringCommand(qry.CommandSql);
            }
            else
            {
                cmd = db.GetStoredProcCommand(qry.CommandSql);

            }
            cmd.CommandType = qry.CommandType;

            foreach (QueryParameter param in qry.Parameters)
            {
                db.AddInParameter(cmd, param.ParameterName, param.DataType, param.ParameterValue);
            }
            return db.ExecuteScalar(cmd);
        }
        public override int ExecuteQuery(QueryCommand qry)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd;

            if (qry.CommandType == CommandType.Text)
            {
                cmd = db.GetSqlStringCommand(qry.CommandSql);
            }
            else
            {
                cmd = db.GetStoredProcCommand(qry.CommandSql);

            }
            cmd.CommandType = qry.CommandType;

            foreach (QueryParameter param in qry.Parameters)
            {
                db.AddInParameter(cmd, param.ParameterName, param.DataType, param.ParameterValue);
            }
            return db.ExecuteNonQuery(cmd);
        }
        public override void ExecuteTransaction(QueryCommandCollection commands)
        {
            Database db = DatabaseFactory.CreateDatabase();

            //make sure we have at least one
            if (commands.Count > 0)
            {
                DbCommand cmd = null;

                //a using statement will make sure we close off the connection
                using (DbConnection conn = db.CreateConnection())
                {
                    //open up the connection and start the transaction
                    conn.Open();

                    DbTransaction trans = conn.BeginTransaction();

                    foreach (QueryCommand qry in commands)
                    {
                        if (qry.CommandType == CommandType.Text)
                        {
                            cmd = db.GetSqlStringCommand(qry.CommandSql);
                        }
                        else
                        {
                            cmd = db.GetStoredProcCommand(qry.CommandSql);
                        }

                        try
                        {
                            foreach (QueryParameter param in qry.Parameters)
                            {
                                db.AddInParameter(cmd, param.ParameterName, param.DataType, param.ParameterValue);
                            }

                            db.ExecuteNonQuery(cmd, trans);
                        }
                        catch (DbException x)
                        {
                            //if there's an error, roll everything back
                            trans.Rollback();

                            //clean up
                            conn.Close();
                            cmd.Dispose();

                            //throw the error retaining the stack.
                            throw new Exception(x.Message);
                        }
                    }
                    //if we get to this point, we're good to go
                    trans.Commit();

                    //close off the connection
                    conn.Close();
                    if (cmd != null)
                    {
                        cmd.Dispose();
                    }
                }
            }
            else
            {
                throw new Exception("No commands present");
            }
        }

        #endregion

        #region Command Builder Overrides
        public override DbCommand GetDbCommand(QueryCommand qry)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd;
            if (qry.CommandType == CommandType.StoredProcedure)
            {
                cmd = db.GetStoredProcCommand(qry.CommandSql);
            }
            else
            {
                cmd = db.GetSqlStringCommand(qry.CommandSql);
            }

            foreach (QueryParameter par in qry.Parameters)
            {
                db.AddInParameter(cmd, par.ParameterName, par.DataType, par.ParameterValue);
            }

            return cmd;
        }
        public override IDbCommand GetCommand(QueryCommand qry)
        {

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetSqlStringCommand(qry.CommandSql);
            foreach (QueryParameter param in qry.Parameters)
            {
                db.AddInParameter(cmd, param.ParameterName, param.DataType, param.ParameterValue);
            }

            return cmd;
        }
        #endregion


    }

    /// <summary>
    /// This is class is for backwards compatibility only. Please update your references to ELib3DataProvider
    /// </summary>
    public class ELib2DataProvider : ELib3DataProvider
    {
    }
}

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)

About the Author

Alex Mueller
Web Developer
United States United States
No Biography provided

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