Click here to Skip to main content
12,624,429 members (37,816 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads

Stats

103.2K views
2.6K downloads
121 bookmarked
Posted

Implementing Model-View-Presenter in ASP.NET

, 17 Nov 2007 CPOL
Three implementations of Model-View-Presenter in ASP.NET 2.0.
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.msi
<%@ Page Language="C#" %>
<%@ Import namespace="SubSonic.Utilities"%>
<%@ Import Namespace="SubSonic" %>
<%
    //The data we need
	string providerName = "#PROVIDER#";
	string tableName = "#TABLE#";
	TableSchema.Table tbl = DataService.GetSchema(tableName, providerName, TableType.Table);
	DataProvider provider = DataService.Providers[providerName];
    ICodeLanguage lang = new CSharpCodeLanguage();
    
    //The main vars we need
    TableSchema.TableColumnCollection cols = tbl.Columns;
    TableSchema.TableColumn[] keys = cols.GetPrimaryKeys();
%>

//Generated on <%=DateTime.Now.ToString() %> by <%=Environment.UserName %>
namespace <%=provider.GeneratedNamespace %>
{
    /// <summary>
    /// Controller class for <%=tbl.Name %>
    /// </summary>
    [System.ComponentModel.DataObject]
    public partial class <%=tbl.ClassName %>Controller
    {
        // Preload our schema..
        <%=tbl.ClassName%> thisSchemaLoad = new <%=tbl.ClassName%>();

        private string userName = string.Empty;
        protected string UserName
        {
            get
            {
				if (userName.Length == 0) 
				{
    				if (System.Web.HttpContext.Current != null)
    				{
						userName=System.Web.HttpContext.Current.User.Identity.Name;
					}
					else
					{
						userName=System.Threading.Thread.CurrentPrincipal.Identity.Name;
					}
				}
				return userName;
            }
        }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public <%=tbl.ClassName%>Collection FetchAll()
        {
            <%=tbl.ClassName%>Collection coll = new <%=tbl.ClassName%>Collection();
            Query qry = new Query(<%=tbl.ClassName%>.Schema);
            coll.LoadAndCloseReader(qry.ExecuteReader());
            return coll;
        }

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public <%=tbl.ClassName%>Collection FetchByID(object <%=tbl.PrimaryKey.PropertyName%>)
        {
            <%=tbl.ClassName%>Collection coll = new <%=tbl.ClassName%>Collection().Where("<%=tbl.PrimaryKey.ColumnName %>", <%=tbl.PrimaryKey.PropertyName%>).Load();
            return coll;
        }
		
		[DataObjectMethod(DataObjectMethodType.Select, false)]
        public <%=tbl.ClassName%>Collection FetchByQuery(Query qry)
        {
            <%=tbl.ClassName%>Collection coll = new <%=tbl.ClassName%>Collection();
            coll.LoadAndCloseReader(qry.ExecuteReader()); 
            return coll;
        }

        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public bool Delete(object <%=tbl.PrimaryKey.PropertyName%>)
        {
            return (<%=tbl.ClassName%>.Delete(<%=tbl.PrimaryKey.PropertyName%>) == 1);
        }

        [DataObjectMethod(DataObjectMethodType.Delete, false)]
        public bool Destroy(object <%=tbl.PrimaryKey.PropertyName%>)
        {
            return (<%=tbl.ClassName%>.Destroy(<%=tbl.PrimaryKey.PropertyName%>) == 1);
        }
        
        <%
            string deleteArgs = string.Empty;
            string whereArgs = string.Empty;

            string deleteDelim = "";
            string whereDelim = "";
			bool useNullables = tbl.Provider.GenerateNullableProperties;
            foreach (TableSchema.TableColumn key in keys)
            {
                string propName = key.PropertyName;
				bool useNullType = useNullables ? key.IsNullable : false;
                string varType = Utility.GetVariableType(key.DataType, useNullType, lang);

                deleteArgs += deleteDelim + varType + " " + propName;
                deleteDelim = ",";

                whereArgs += whereDelim + "(\"" + propName + "\", " + propName + ")";
                whereDelim = ".AND";
                
            }
            // add this delete method if the table has multiple keys
            if (keys.Length > 0)
            {
        %>
        
        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public bool Delete(<%=deleteArgs%>)
        {
            Query qry = new Query(<%=tbl.ClassName%>.Schema);
            qry.QueryType = QueryType.Delete;
            qry.AddWhere<%=whereArgs%>;
            qry.Execute();
            return (true);
        }        

       
    	<%
            }
			string insertArgs = string.Empty;
			string updateArgs = string.Empty;
			string seperator = ",";

			foreach (TableSchema.TableColumn col in cols)
			{
				string propName = col.PropertyName;
				bool useNullType = useNullables ? col.IsNullable : false;
				string varType = Utility.GetVariableType(col.DataType, useNullType, lang);

				updateArgs += varType + " " + propName + seperator;
				if (!col.AutoIncrement)
				{
					insertArgs += varType + " " + propName + seperator;
				}
			}
			if (insertArgs.Length > 0)
				insertArgs = insertArgs.Remove(insertArgs.Length - seperator.Length, seperator.Length);
			if (updateArgs.Length > 0)
				updateArgs = updateArgs.Remove(updateArgs.Length - seperator.Length, seperator.Length);
%>
    	
	    /// <summary>
	    /// Inserts a record, can be used with the Object Data Source
	    /// </summary>
        [DataObjectMethod(DataObjectMethodType.Insert, true)]
	    public void Insert(<%=insertArgs%>)
	    {
		    <%=tbl.ClassName %> item = new <%= tbl.ClassName %>();
		    <% 
		    foreach (TableSchema.TableColumn col in cols) {
                if (!col.AutoIncrement) { 
            %>
            item.<%=col.PropertyName%> = <%=col.PropertyName%>;
            <%
                }
              } 
            %>
	    
		    item.Save(UserName);
	    }

    	
	    /// <summary>
	    /// Updates a record, can be used with the Object Data Source
	    /// </summary>
        [DataObjectMethod(DataObjectMethodType.Update, true)]
	    public void Update(<%=updateArgs%>)
	    {
		    <%=tbl.ClassName%> item = new <%=tbl.ClassName %>();
		    <% 
		    foreach (TableSchema.TableColumn col in cols) 
			{
				%>
				item.<%=col.PropertyName%> = <%=col.PropertyName%>;
				<%
			} 
            %>
		    item.MarkOld();
		    item.Save(UserName);
	    }

    }

}

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...

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