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
<%@ Page Language="C#"%>
<%@ Import namespace="SubSonic.Utilities"%>
<%@ Import Namespace="SubSonic" %>
<%
    string providerName = "#PROVIDER#";
    string viewName = "#VIEW#";
    //The data we need
    TableSchema.Table view = DataService.GetSchema(viewName, providerName, TableType.View);
    ICodeLanguage language = new CSharpCodeLanguage();
    
    //The main vars we need
    TableSchema.TableColumnCollection cols = view.Columns;
    string className = view.ClassName;
    string nSpace = DataService.Providers[providerName].GeneratedNamespace;

    
%>
namespace <%=nSpace %>{
    /// <summary>
    /// Strongly-typed collection for the <%=className%> class.
    /// </summary>

    [Serializable]
    public partial class <%=className%>Collection : ReadOnlyList[<]<%= className %>, <%=className%>Collection[>]
    {        
        public <%=className%>Collection() {}
    }

    /// <summary>
    /// This is  Read-only wrapper class for the <%=viewName%> view.
    /// </summary>
    [Serializable]
    public partial class <%=className%> : ReadOnlyRecord[<]<%= className %>[>] 
    {
    
	    #region Default Settings
	    protected static void SetSQLProps() 
	    {
		    GetTableSchema();
	    }
	    #endregion

        #region Schema Accessor
	    public static TableSchema.Table Schema
        {
            get
            {
                if (BaseSchema == null)
                {
                    SetSQLProps();
                }
                return BaseSchema;
            }
        }
    	
        private static void GetTableSchema() 
        {
            if(!IsSchemaInitialized)
            {
                //Schema declaration
                TableSchema.Table schema = new TableSchema.Table("<%=viewName%>", TableType.View, DataService.GetInstance("<%=providerName%>"));
                schema.Columns = new TableSchema.TableColumnCollection();
                schema.SchemaName = "<%=view.SchemaName%>";

                //columns
                <%
                foreach(TableSchema.TableColumn col in cols)
				{
                    string varName = "col" + col.ArgumentName;
                %>
                TableSchema.TableColumn <%=varName %> = new TableSchema.TableColumn(schema);
                <%=varName %>.ColumnName = "<%= col.ColumnName %>";
                <%=varName %>.DataType = DbType.<%=col.DataType %>;
                <%=varName %>.MaxLength = <%=col.MaxLength %>;
                <%=varName %>.AutoIncrement = false;
                <%=varName %>.IsNullable = <%=col.IsNullable.ToString().ToLower()%>;
                <%=varName %>.IsPrimaryKey = false;
                <%=varName %>.IsForeignKey = false;
                <%=varName %>.IsReadOnly = <%= col.IsReadOnly.ToString().ToLower() %>;
                <%
				if(col.IsForeignKey)
				{
                %>
				<%=varName %>.ForeignKeyTableName = "<%= col.ForeignKeyTableName %>";
                <% } %>
                schema.Columns.Add(<%=varName%>);

                <%
                }
                %>
                
                BaseSchema = schema;
                //add this schema to the provider
                //so we can query it later
                DataService.Providers["<%=providerName %>"].AddSchema("<%=viewName%>",schema);
            }
        }
        #endregion
        
        #region Query Accessor
	    public static Query CreateQuery()
	    {
		    return new Query(Schema);
	    }
	    #endregion
	    
	    #region .ctors
	    public <%=className %>()
	    {
            SetSQLProps();
            SetDefaults();
            MarkNew();
        }

        public <%=className %>(bool useDatabaseDefaults)
	    {
		    SetSQLProps();
		    if(useDatabaseDefaults)
		    {
				ForceDefaults();
			}
			MarkNew();
	    }
	    
	    public <%=className %>(object keyID)
	    {
		    SetSQLProps();
		    LoadByKey(keyID);
	    }
    	 
	    public <%=className %>(string columnName, object columnValue)
        {
            SetSQLProps();
            LoadByParam(columnName,columnValue);
        }
        
	    #endregion
	    
	    #region Props
	    
        <%
        foreach(TableSchema.TableColumn col in cols){
            string propName = col.PropertyName;
            string varType = Utility.GetVariableType(col.DataType, col.IsNullable, language);
        %>  
        [XmlAttribute("<%=propName%>")]
        public <%=varType%> <%=propName%> 
	    {
		    get
		    {
			    return GetColumnValue[<]<%= varType %>[>]("<%= col.ColumnName %>");
		    }
            set 
		    {
			    SetColumnValue("<%=col.ColumnName%>", value);

            }
        }
	    <%
	    }
	    %>
	    #endregion
    
	    #region Columns Struct
	    public struct Columns
	    {
		    
		    <% 
		    foreach (TableSchema.TableColumn col in cols) {
                string propName = col.PropertyName;
            %>
            public static string <%=propName%> = @"<%=col.ColumnName%>";
            <%
              } 
            %>

	    }
	    #endregion
    }
}

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