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

Model View Presenter via .NET

Rate me:
Please Sign up or sign in to vote.
4.14/5 (24 votes)
10 Oct 2009CPOL7 min read 81.7K   1.9K   79  
An article outlining an implementation of the Model View Presenter pattern in .NET, contrasting it with existing implementations of MVP, MVC, and using co-dependant interfaces to allow for abstract coordination.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CobaltSoftware.Foundation.ModelViewPresenter;
using ExampleProject.Contracts;
using System.ComponentModel;
using System.Diagnostics;

namespace ExampleProject.Presenters
{
    /// <summary>
    ///     The ClockPresenter is a simple IClockPresenter implementation
    ///     that controls and coordinates the display of date/time on
    ///     it's associated views.
    /// </summary>
    /// <remarks>
    ///     <para>
    ///         <list>
    ///             <listheader>Version History</listheader>
    ///             <item>10 October, 2009 - Steve Gray - Initial Draft</item>
    ///         </list>
    ///     </para>
    /// </remarks>
    public class ClockPresenter : PresenterBase<IClockPresenter, IClockView>, IClockPresenter
    {
        #region Private Fields

        private ITimeModel _ModelInstance;

        #endregion

        #region Constructor(s)

        /// <summary>
        ///    Initialize a new clock presenter, showing some aspect of a model.
        /// </summary>
        /// <param name="timeModel">Time model to observe/update</param>
        public ClockPresenter(ITimeModel timeModel)
        {
            // Validate arguments
            if (timeModel == null)
                throw new ArgumentNullException("timeModel");

            // Set local copy
            _ModelInstance = timeModel;

            // Start observing
            _ModelInstance.PropertyChanged += new PropertyChangedEventHandler(ModelInstance_PropertyChanged);
        }

        #endregion

        #region Model Observation

        /// <summary>
        ///     A property of the model has changed
        /// </summary>
        /// <param name="sender">Sending Object</param>
        /// <param name="e">Event arguments</param>
        void ModelInstance_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {                
                case "CurrentTimezone":
                    // Time-zone has been updated on the model
                    lock (Views)
                    {
                        // Set the time-zone on each view.
                        Views.ToList().ForEach(x => x.SelectedTimeZone = _ModelInstance.CurrentTimezone);
                    }
                    break;

                case "CurrentTime":
                    lock (Views)
                    {
                        // Set the time on each view
                        Views.ToList().ForEach(x => x.Time = _ModelInstance.CurrentTime);
                    }

                    break;

                default:
                    // We're not listening for this property to change!
                    break;

            }
        }

        #endregion

        #region IClockPresenter Implementation

        /// <summary>
        ///     When a view has changed it's timezone, update the model.
        /// </summary>
        /// <param name="view">View that performed the update</param>
        /// <param name="newZone">New time-zone</param>
        public void ChangeTimeZone(TimeZoneInfo timeZone)
        {
            // Test invariants
            Debug.Assert(_ModelInstance != null, "Model should always be available.");
            
            // Update model
            _ModelInstance.CurrentTimezone = timeZone;
        }

        #endregion
        
        #region PresenterBase Implementation

        /// <summary>
        ///     Refresh a view with the initial state.
        /// </summary>
        /// <param name="viewInstance">View instance being wired up</param>
        protected override void RefreshView(IClockView viewInstance)
        {
            // Validate arguments
            if (viewInstance == null)
                throw new ArgumentNullException("viewInstance");

            // Push the list of all time-zones through
            viewInstance.TimeZones = _ModelInstance.AllTimeZones;

            // Set the current timezone, as per the model
            viewInstance.SelectedTimeZone = _ModelInstance.CurrentTimezone;
        }

        /// <summary>
        ///     Obtain a reference to ourselves.
        /// </summary>
        /// <remarks>
        ///     Required by the PresenterBase class, since it cannot supply
        ///     it's own subclassed generic type. It's a curious perversion
        ///     of generics for some.
        /// </remarks>
        /// <returns>This instance (as IClockPresenter)</returns>
        protected override IClockPresenter GetPresenterEndpoint()
        {
            return this;
        }

        #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
Software Developer (Senior) Insurance Industry
United Kingdom United Kingdom
Steve Gray is a Senior Developer at a British insurance company, working on a popular aggregator. When he's not writing ASP .NET, it's because there's SQL or WCF to write instead.

Comments and Discussions