Click here to Skip to main content
15,895,142 members
Articles / Desktop Programming / WPF

MVVM for Multi Platforms

Rate me:
Please Sign up or sign in to vote.
4.13/5 (6 votes)
22 Mar 2010CPOL2 min read 26.5K   354   22  
How to implement MVVM when developing a view model whose view implementation language is not certain
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GeographicRepresentation.Interfaces;
using GeographicRepresentation.Lib.Model;

namespace GeographicRepresentation.Lib.ViewModel
{
    /// <summary>
    /// This is a base view model for a city
    /// </summary>
    public class CityViewModel : ICityViewModel
    {
        #region Members        
        /// <summary>
        /// A Model object of the city
        /// </summary>
        City m_City;

        #endregion

        #region Properties

        /// <summary>
        /// A property for extracting the city value out of the view model for saving and changing the value once
        /// a refresh is called. It is for usuage only within the current namespace
        /// </summary>
        internal City City
        {
            get
            {
                return m_City;
            }
            set
            {
                m_City = value;
            }
        }

        #endregion

        #region Methods

        #region Ctor

        public CityViewModel(City city) 
        {
            m_City = city;
        }

        #endregion

        #region ICityViewModel Members

        public Guid Id
        {
            get 
            {
                return m_City.Id;
            }
        }

        /// <summary>
        /// The City name can only be edited via the Edit mode, so it is declared virtual and would be overriden in the edit mode.
        /// </summary>
        public virtual string CityName
        {
            get
            {
                return m_City.Name;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        /// The Population can only be edited via the Edit mode, so it is declared virtual and would be overriden in the edit mode.
        /// </summary>
        public virtual int Population
        {
            get
            {
                return m_City.Population;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        /// The Time zone can only be edited via the Edit mode, so it is declared virtual and would be overriden in the edit mode.
        /// </summary>
        public virtual short TimeZone
        {
            get
            {
                return m_City.TimeZone;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        /// Calculate the current time and add the differences in the time zone to return an accurate state of the current time
        /// </summary>
        /// <returns></returns>
        public DateTime GetCurrentTime()
        {
            DateTime current = DateTime.Now;
            current = current.AddHours(m_City.TimeZone);
            return current;
        }

        #endregion

        #endregion
        
    }

    /// <summary>
    /// This is the read only view model for the city
    /// </summary>
    public class ReadOnlyCityViewModel :  CityViewModel, IReadOnlyCityViewModel
    {
        #region Methods

        #region Ctor

        public ReadOnlyCityViewModel(City city) : base(city)
        {
        }

        #endregion

        #region IReadOnlyObject Members

        /// <summary>
        /// Reload the data from the repository
        /// </summary>
        public void Refresh()
        {
            DataManager.Instance.LoadDataFromStorage();
            this.City = DataManager.Instance.GetCity(this.Id);
        }

        #endregion

        #endregion
    }

    /// <summary>
    /// This is the view model which enables editing data regarding a city
    /// </summary>
    public class EditableCityViewModel : CityViewModel, IEditableCityViewModel
    {
        #region Members

        /// <summary>
        /// The countries id, in case this is a new city
        /// </summary>
        Guid m_CountryId;
        /// <summary>
        /// Keep a replica of the original city, if cancelation was made
        /// </summary>
        City m_OrigCity;

        #endregion

        #region Methods

        #region Ctor

        public EditableCityViewModel(City city) : base(city)
        {
            m_CountryId = Guid.Empty;
            m_OrigCity = new City(city);
        }

        public EditableCityViewModel(City city, Guid countryId)
            : base(city)
        {
            m_CountryId = countryId;
        }

        #endregion

        #region IEditableObject Members

        /// <summary>
        /// Save the data in the repository
        /// </summary>
        public void Save()
        {
            // Add the City into the country
            if (m_CountryId != Guid.Empty)
            {
                Country country = DataManager.Instance.GetCountry(m_CountryId);
                country.Cities.Add(this.City);
            }
            DataManager.Instance.SaveDataIntoStorage();
            if (OnSaveCompleted != null)
            {
                OnSaveCompleted(true);
            }
        }

        /// <summary>
        /// Upon cancelation of the operation, return the original values to the city
        /// </summary>
        public void Cancel()
        {
            this.City.Name = m_OrigCity.Name;
        }

        public event OperationCompletedDelegate OnSaveCompleted;

        /// <summary>
        /// The edit view model is the only place in which the property could be changed. Therefore it is overriden
        /// </summary>
        public override string CityName
        {
            get
            {
                return base.CityName;
            }
            set
            {
                this.City.Name = value;
            }
        }

        /// <summary>
        /// The edit view model is the only place in which the property could be changed. Therefore it is overriden
        /// </summary>
        public override int Population
        {
            get
            {
                return base.Population;
            }
            set
            {
                base.City.Population = value;
            }
        }

        /// <summary>
        /// The edit view model is the only place in which the property could be changed. Therefore it is overriden
        /// </summary>
        public override short TimeZone
        {
            get
            {
                return base.TimeZone;
            }
            set
            {
                base.City.TimeZone = value;
            }
        }

        #endregion

        #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
Israel Israel
Software Developer in a promising Clean-Tech company

Comments and Discussions