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

Model View Presenter with ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.93/5 (201 votes)
16 Oct 200724 min read 1.5M   27.1K   606  
This article describes using the Model-View-Presenter pattern within ASP.NET 2.0 to encourage proper separation of concerns between presentation and business logic
using System;
using MvpSample.Core.DataInterfaces;
using MvpSample.Presenters.ViewInterfaces;

namespace MvpSample.Presenters
{
    /// <summary>
    /// There's not a lot of reason to separate the "Edit" presenter from the "Add" presenter; but alas, 
    /// it makes for a more thorough example.
    /// </summary>
    public class EditCustomerPresenter
    {
        public EditCustomerPresenter(IEditCustomerView view, ICustomerDao customerDao) {
            if (view == null) throw new ArgumentNullException("view may not be null");
            if (customerDao == null) throw new ArgumentNullException("customerDao may not be null");

            this.view = view;
            this.customerDao = customerDao;
        }

        /// <summary>
        /// Raised when the user wants to cancel editing a customer.  The containing ASPX page should 
        /// listen for this event.
        /// </summary>
        public EventHandler CancelUpdateEvent;

        /// <summary>
        /// Raised after the customer has been successfully committed to the database.
        /// </summary>
        public EventHandler UpdateCustomerCompleteEvent;

        public void InitViewForEditing(string customerId, bool isPostBack) {
            if (string.IsNullOrEmpty(customerId))
                throw new ArgumentNullException("customerId may not be null or empty");

            if (! isPostBack) {
                view.CustomerToUpdate = customerDao.GetById(customerId, false);
            }
        }

        /// <summary>
        /// Called by the view; this grabs the updated customer from the view and commits it to the DB.
        /// </summary>
        public void UpdateCustomer(bool isPageValid) {
            // Here's an example of taking into account if Page.IsValid.  Of course, if it fails, 
            // you'd want to send a message back to the view stating that there was a problem with 
            // the inputs.  For an example of having the presenter send a message back to the view,
            // see ListCustomersPresenter.
            if (isPageValid) {
                customerDao.SaveOrUpdate(view.CustomerToUpdate);

                // You could certainly pass in more than just null for the event args
                UpdateCustomerCompleteEvent(this, null);
            }
        }
        
        /// <summary>
        /// In this example solution, this is simply a pass-through method for the event
        /// to be raised.  In such a simple example, you may consider having the user control raise
        /// the event directly.  Conversely, there are times when other actions are taken before
        /// the event is raised.  In these situations, the presenter should definitely manage 
        /// raising the event after the other actions have been taken.
        /// </summary>
        public void CancelUpdate() {
            CancelUpdateEvent(this, null);
        }
        
        private IEditCustomerView view;
        private ICustomerDao customerDao;
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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