Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Model View Presenter via .NET

, 10 Oct 2009
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.
ExampleProject.zip
ExampleProject.Contracts
bin
Debug
CobaltSoftware.Foundation.ModelViewPresenter.dll
ExampleProject.Contracts.dll
Properties
ExampleProject.Model
bin
Debug
CobaltSoftware.Foundation.ModelViewPresenter.dll
ExampleProject.Contracts.dll
ExampleProject.Model.dll
Properties
ExampleProject.Presenters
bin
Debug
CobaltSoftware.Foundation.ModelViewPresenter.dll
ExampleProject.Contracts.dll
ExampleProject.Presenters.dll
Properties
ExampleProject.Views
bin
Debug
CobaltSoftware.Foundation.ModelViewPresenter.dll
ExampleProject.Contracts.dll
ExampleProject.Views.dll
Properties
CobaltSoftware.Foundation.ModelViewPresenter
bin
Debug
CobaltSoftware.Foundation.ModelViewPresenter.dll
Core
Properties
Support
ExampleProject.Application
bin
Debug
CobaltSoftware.Foundation.ModelViewPresenter.dll
ExampleProject.Application.exe
ExampleProject.Application.vshost.exe
ExampleProject.Application.vshost.exe.manifest
ExampleProject.Contracts.dll
ExampleProject.Model.dll
ExampleProject.Presenters.dll
ExampleProject.Views.dll
Properties
Settings.settings
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)

Share

About the Author

Steven James Gray
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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 10 Oct 2009
Article Copyright 2009 by Steven James Gray
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid