Click here to Skip to main content
15,886,799 members
Articles / Web Development / ASP.NET

Implementing Model-View-Presenter in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (27 votes)
17 Nov 2007CPOL12 min read 129.5K   2.7K   120  
Three implementations of Model-View-Presenter in ASP.NET 2.0.
<%@ 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)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions