Click here to Skip to main content
15,885,546 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.
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using Model.Data.Interfaces;
using SubSonic;
using SubSonic.Utilities;

//Generated on 11/6/2007 2:44:49 PM by Alex

namespace Model.Data.Northwind{
    /// <summary>
    /// Strongly-typed collection for the Product class.
    /// </summary>
    [Serializable]
    public partial class ProductCollection : ActiveList<Product, ProductCollection> 
    {       
        public ProductCollection() {}

    }

    /// <summary>
    /// This is an ActiveRecord class which wraps the Products table.
    /// </summary>
    [Serializable]
    public partial class Product : ActiveRecord<Product>, IProduct 
    {
    
	    #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("Products", TableType.Table, DataService.GetInstance("Northwind"));
                schema.Columns = new TableSchema.TableColumnCollection();
                schema.SchemaName = @"dbo";
                //columns
                
                TableSchema.TableColumn colvarProductID = new TableSchema.TableColumn(schema);
                colvarProductID.ColumnName = "ProductID";
                colvarProductID.DataType = DbType.Int32;
                colvarProductID.MaxLength = 0;
                colvarProductID.AutoIncrement = true;
                colvarProductID.IsNullable = false;
                colvarProductID.IsPrimaryKey = true;
                colvarProductID.IsForeignKey = false;
                colvarProductID.IsReadOnly = false;
                
                
                schema.Columns.Add(colvarProductID);
                
                TableSchema.TableColumn colvarProductName = new TableSchema.TableColumn(schema);
                colvarProductName.ColumnName = "ProductName";
                colvarProductName.DataType = DbType.String;
                colvarProductName.MaxLength = 40;
                colvarProductName.AutoIncrement = false;
                colvarProductName.IsNullable = false;
                colvarProductName.IsPrimaryKey = false;
                colvarProductName.IsForeignKey = false;
                colvarProductName.IsReadOnly = false;
                
                
                schema.Columns.Add(colvarProductName);
                
                TableSchema.TableColumn colvarSupplierID = new TableSchema.TableColumn(schema);
                colvarSupplierID.ColumnName = "SupplierID";
                colvarSupplierID.DataType = DbType.Int32;
                colvarSupplierID.MaxLength = 0;
                colvarSupplierID.AutoIncrement = false;
                colvarSupplierID.IsNullable = true;
                colvarSupplierID.IsPrimaryKey = false;
                colvarSupplierID.IsForeignKey = true;
                colvarSupplierID.IsReadOnly = false;
                
                
				colvarSupplierID.ForeignKeyTableName = "Suppliers";
                
                schema.Columns.Add(colvarSupplierID);
                
                TableSchema.TableColumn colvarCategoryID = new TableSchema.TableColumn(schema);
                colvarCategoryID.ColumnName = "CategoryID";
                colvarCategoryID.DataType = DbType.Int32;
                colvarCategoryID.MaxLength = 0;
                colvarCategoryID.AutoIncrement = false;
                colvarCategoryID.IsNullable = true;
                colvarCategoryID.IsPrimaryKey = false;
                colvarCategoryID.IsForeignKey = true;
                colvarCategoryID.IsReadOnly = false;
                
                
				colvarCategoryID.ForeignKeyTableName = "Categories";
                
                schema.Columns.Add(colvarCategoryID);
                
                TableSchema.TableColumn colvarQuantityPerUnit = new TableSchema.TableColumn(schema);
                colvarQuantityPerUnit.ColumnName = "QuantityPerUnit";
                colvarQuantityPerUnit.DataType = DbType.String;
                colvarQuantityPerUnit.MaxLength = 20;
                colvarQuantityPerUnit.AutoIncrement = false;
                colvarQuantityPerUnit.IsNullable = true;
                colvarQuantityPerUnit.IsPrimaryKey = false;
                colvarQuantityPerUnit.IsForeignKey = false;
                colvarQuantityPerUnit.IsReadOnly = false;
                
                
                schema.Columns.Add(colvarQuantityPerUnit);
                
                TableSchema.TableColumn colvarUnitPrice = new TableSchema.TableColumn(schema);
                colvarUnitPrice.ColumnName = "UnitPrice";
                colvarUnitPrice.DataType = DbType.Currency;
                colvarUnitPrice.MaxLength = 0;
                colvarUnitPrice.AutoIncrement = false;
                colvarUnitPrice.IsNullable = true;
                colvarUnitPrice.IsPrimaryKey = false;
                colvarUnitPrice.IsForeignKey = false;
                colvarUnitPrice.IsReadOnly = false;
                
						colvarUnitPrice.DefaultSetting = @"((0))";
				
                
                schema.Columns.Add(colvarUnitPrice);
                
                TableSchema.TableColumn colvarUnitsInStock = new TableSchema.TableColumn(schema);
                colvarUnitsInStock.ColumnName = "UnitsInStock";
                colvarUnitsInStock.DataType = DbType.Int16;
                colvarUnitsInStock.MaxLength = 0;
                colvarUnitsInStock.AutoIncrement = false;
                colvarUnitsInStock.IsNullable = true;
                colvarUnitsInStock.IsPrimaryKey = false;
                colvarUnitsInStock.IsForeignKey = false;
                colvarUnitsInStock.IsReadOnly = false;
                
						colvarUnitsInStock.DefaultSetting = @"((0))";
				
                
                schema.Columns.Add(colvarUnitsInStock);
                
                TableSchema.TableColumn colvarUnitsOnOrder = new TableSchema.TableColumn(schema);
                colvarUnitsOnOrder.ColumnName = "UnitsOnOrder";
                colvarUnitsOnOrder.DataType = DbType.Int16;
                colvarUnitsOnOrder.MaxLength = 0;
                colvarUnitsOnOrder.AutoIncrement = false;
                colvarUnitsOnOrder.IsNullable = true;
                colvarUnitsOnOrder.IsPrimaryKey = false;
                colvarUnitsOnOrder.IsForeignKey = false;
                colvarUnitsOnOrder.IsReadOnly = false;
                
						colvarUnitsOnOrder.DefaultSetting = @"((0))";
				
                
                schema.Columns.Add(colvarUnitsOnOrder);
                
                TableSchema.TableColumn colvarReorderLevel = new TableSchema.TableColumn(schema);
                colvarReorderLevel.ColumnName = "ReorderLevel";
                colvarReorderLevel.DataType = DbType.Int16;
                colvarReorderLevel.MaxLength = 0;
                colvarReorderLevel.AutoIncrement = false;
                colvarReorderLevel.IsNullable = true;
                colvarReorderLevel.IsPrimaryKey = false;
                colvarReorderLevel.IsForeignKey = false;
                colvarReorderLevel.IsReadOnly = false;
                
						colvarReorderLevel.DefaultSetting = @"((0))";
				
                
                schema.Columns.Add(colvarReorderLevel);
                
                TableSchema.TableColumn colvarDiscontinued = new TableSchema.TableColumn(schema);
                colvarDiscontinued.ColumnName = "Discontinued";
                colvarDiscontinued.DataType = DbType.Boolean;
                colvarDiscontinued.MaxLength = 0;
                colvarDiscontinued.AutoIncrement = false;
                colvarDiscontinued.IsNullable = false;
                colvarDiscontinued.IsPrimaryKey = false;
                colvarDiscontinued.IsForeignKey = false;
                colvarDiscontinued.IsReadOnly = false;
                
						colvarDiscontinued.DefaultSetting = @"((0))";
				
                
                schema.Columns.Add(colvarDiscontinued);
                
                BaseSchema = schema;
                //add this schema to the provider
                //so we can query it later
                DataService.Providers["Northwind"].AddSchema("Products",schema);
            }

        }

        #endregion
        
        #region Query Accessor
	    public static Query CreateQuery()
	    {
		    return new Query(Schema);
	    }

	    #endregion
	    
	    #region .ctors
	    public Product()
	    {
            SetSQLProps();
            SetDefaults();
            MarkNew();
        }

        
        public Product(bool useDatabaseDefaults)
	    {
		    SetSQLProps();
		    if(useDatabaseDefaults)
		    {
				ForceDefaults();
			}

			MarkNew();
	    }

	    public Product(object keyID)
	    {
		    SetSQLProps();
            SetDefaults();
		    LoadByKey(keyID);
	    }

    	 
	    public Product(string columnName, object columnValue)
        {
            SetSQLProps();
            SetDefaults();
            LoadByParam(columnName,columnValue);
        }

        
	    #endregion
	    
	    #region Props
	    
          
        [XmlAttribute("ProductID")]
        public int ProductID 
	    {
		    get { return GetColumnValue<int>("ProductID"); }

            set 
		    {
			    SetColumnValue("ProductID", value);
            }

        }

	      
        [XmlAttribute("ProductName")]
        public string ProductName 
	    {
		    get { return GetColumnValue<string>("ProductName"); }

            set 
		    {
			    SetColumnValue("ProductName", value);
            }

        }

	      
        [XmlAttribute("SupplierID")]
        public int? SupplierID 
	    {
		    get { return GetColumnValue<int?>("SupplierID"); }

            set 
		    {
			    SetColumnValue("SupplierID", value);
            }

        }

	      
        [XmlAttribute("CategoryID")]
        public int? CategoryID 
	    {
		    get { return GetColumnValue<int?>("CategoryID"); }

            set 
		    {
			    SetColumnValue("CategoryID", value);
            }

        }

	      
        [XmlAttribute("QuantityPerUnit")]
        public string QuantityPerUnit 
	    {
		    get { return GetColumnValue<string>("QuantityPerUnit"); }

            set 
		    {
			    SetColumnValue("QuantityPerUnit", value);
            }

        }

	      
        [XmlAttribute("UnitPrice")]
        public decimal? UnitPrice 
	    {
		    get { return GetColumnValue<decimal?>("UnitPrice"); }

            set 
		    {
			    SetColumnValue("UnitPrice", value);
            }

        }

	      
        [XmlAttribute("UnitsInStock")]
        public short? UnitsInStock 
	    {
		    get { return GetColumnValue<short?>("UnitsInStock"); }

            set 
		    {
			    SetColumnValue("UnitsInStock", value);
            }

        }

	      
        [XmlAttribute("UnitsOnOrder")]
        public short? UnitsOnOrder 
	    {
		    get { return GetColumnValue<short?>("UnitsOnOrder"); }

            set 
		    {
			    SetColumnValue("UnitsOnOrder", value);
            }

        }

	      
        [XmlAttribute("ReorderLevel")]
        public short? ReorderLevel 
	    {
		    get { return GetColumnValue<short?>("ReorderLevel"); }

            set 
		    {
			    SetColumnValue("ReorderLevel", value);
            }

        }

	      
        [XmlAttribute("Discontinued")]
        public bool Discontinued 
	    {
		    get { return GetColumnValue<bool>("Discontinued"); }

            set 
		    {
			    SetColumnValue("Discontinued", value);
            }

        }

	    
	    #endregion
	    
	    
	    #region PrimaryKey Methods
	    
		#endregion
		
	 	
			
	    
	    
	    
	    #region ForeignKey Properties
	    
	    #endregion
	    
	    
	    
	    #region Many To Many Helpers
	    
	    #endregion
	    
	    #region ObjectDataSource support
    	
    	
	    /// <summary>
	    /// Inserts a record, can be used with the Object Data Source
	    /// </summary>
	    public static void Insert(string varProductName,int? varSupplierID,int? varCategoryID,string varQuantityPerUnit,decimal? varUnitPrice,short? varUnitsInStock,short? varUnitsOnOrder,short? varReorderLevel,bool varDiscontinued)
	    {
		    Product item = new Product();
		    
            item.ProductName = varProductName;
            
            item.SupplierID = varSupplierID;
            
            item.CategoryID = varCategoryID;
            
            item.QuantityPerUnit = varQuantityPerUnit;
            
            item.UnitPrice = varUnitPrice;
            
            item.UnitsInStock = varUnitsInStock;
            
            item.UnitsOnOrder = varUnitsOnOrder;
            
            item.ReorderLevel = varReorderLevel;
            
            item.Discontinued = varDiscontinued;
            
	    
		    if (System.Web.HttpContext.Current != null)
			    item.Save(System.Web.HttpContext.Current.User.Identity.Name);
		    else
			    item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
	    }

    	
	    /// <summary>
	    /// Updates a record, can be used with the Object Data Source
	    /// </summary>
	    public static void Update(int varProductID,string varProductName,int? varSupplierID,int? varCategoryID,string varQuantityPerUnit,decimal? varUnitPrice,short? varUnitsInStock,short? varUnitsOnOrder,short? varReorderLevel,bool varDiscontinued)
	    {
		    Product item = new Product();
		    
                item.ProductID = varProductID;
				
                item.ProductName = varProductName;
				
                item.SupplierID = varSupplierID;
				
                item.CategoryID = varCategoryID;
				
                item.QuantityPerUnit = varQuantityPerUnit;
				
                item.UnitPrice = varUnitPrice;
				
                item.UnitsInStock = varUnitsInStock;
				
                item.UnitsOnOrder = varUnitsOnOrder;
				
                item.ReorderLevel = varReorderLevel;
				
                item.Discontinued = varDiscontinued;
				
		    item.IsNew = false;
		    if (System.Web.HttpContext.Current != null)
			    item.Save(System.Web.HttpContext.Current.User.Identity.Name);
		    else
			    item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
	    }

	    #endregion
	    #region Columns Struct
	    public struct Columns
	    {
		    
		    
            public static string ProductID = @"ProductID";
            
            public static string ProductName = @"ProductName";
            
            public static string SupplierID = @"SupplierID";
            
            public static string CategoryID = @"CategoryID";
            
            public static string QuantityPerUnit = @"QuantityPerUnit";
            
            public static string UnitPrice = @"UnitPrice";
            
            public static string UnitsInStock = @"UnitsInStock";
            
            public static string UnitsOnOrder = @"UnitsOnOrder";
            
            public static string ReorderLevel = @"ReorderLevel";
            
            public static string Discontinued = @"Discontinued";
            
	    }

	    #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)


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