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

MVC, MVP, ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.39/5 (18 votes)
31 Oct 2008CPOL12 min read 129.8K   338   98  
MVC Design pattern in comparison with MVP in the ASP.NET world
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfMvc.Views.Main;
using WpfMvc.Models.Main;
using WpfMvc.Entities;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.ObjectModel;

namespace WpfMvc.Controllers.Main
{
    public class MainController: ControllerBase, IMainController
    {
        // Create few Persons at the startup
        public override void Init()
        {
            //Model.Persons = new List<Person>()
            //Model.Persons = new ObservableCollection<Person>()
            Model.Persons = new NotifyingCollection<Person>()
            { 
                new Person () { Code="001", Name = "David" , City = "Dublin"}
              , new Person () { Code="003", Name = "Martin", City = "Prague"}
              , new Person () { Code="005", Name = "Alain" , City = "Frankfurt" }
              , new Person () { Code="007", Name = "Andy"  , City = "Bratislava"} 
            };            
        }

        public void Detail()    // Creates new Person for editing, based on selected Entity
        {
            Model.Person = new Person(CollectionViewSource.GetDefaultView(Model.Persons).CurrentItem as Person);
        }

        public void Update()
        {
            Model.Persons           // Updates the real Person nested in Model.Persons
                .Where(p => p.Code == Model.Person.Code)  // with properties according
                .First()                                  // to currently edited Model.Person
                .Adjust(Model.Person);            
        }

        public void Cancel()
        {
            Detail(); // Creates new Person instance for edint using selected Entity
        }

        public void Add()
        {
            Model.Persons.Add(new Person(Model.Person){ Code = Model.Person.Code + "1" });
        }
        
        public new IMainWindow View
        {                           // casts the base View to IMainWindow
            get { return base.View as IMainWindow; }
            set { base.View = value; }
        }        
        public new IMainModel Model
        {                           // casting the base Model to IMainModel
            get { return base.Model as IMainModel; }
            set { base.Model = value; }
        }
    }
}

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
Czech Republic Czech Republic
Web developer since 2002, .NET since 2005

Competition is as important as Cooperation.

___

Comments and Discussions