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

MVVM for Multi Platforms

, 22 Mar 2010 CPOL
How to implement MVVM when developing a view model whose view implementation language is not certain
GeographicRepresentation.zip
GeographicRepresentation
bin
Release
GeographicRepresentation.Interfaces.dll
GeographicRepresentation.Interfaces.pdb
GeographicRepresentation.Lib.dll
GeographicRepresentation.Lib.pdb
GeographicRepresentation.TestLib.dll
GeographicRepresentation.TestLib.pdb
GeographicRepresentation.UILib.exe
GeographicRepresentation.UILib.pdb
GeographicRepresentation.UILib.vshost.exe
GeographicRepresentation.UILib.vshost.exe.manifest
Resources
GeographicRepresentation.suo
GeographicRepresentation.TestLib
bin
Debug
GeographicRepresentation.Interfaces.dll
GeographicRepresentation.Lib.dll
GeographicRepresentation.TestLib.dll
Release
Properties
GeographicRepresentation.vsmdi
Interfaces
bin
Debug
GeographicRepresentation.Interfaces.dll
Release
Properties
Lib
bin
Debug
GeographicRepresentation.Interfaces.dll
GeographicRepresentation.Lib.dll
Release
Model
Properties
Resources
ViewModel
LocalTestRun.testrunconfig
TestResults
UILib
bin
Debug
GeographicRepresentation.UILib.exe
WpfApplication1.vshost.exe.manifest
Release
GeographicRepresentation.UILib.vshost.exe.manifest
Properties
Settings.settings
UILib.csproj.user
View
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)

Share

About the Author

Izhar Lotem
Software Developer
Israel Israel
Software Developer in a promising Clean-Tech company

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 22 Mar 2010
Article Copyright 2010 by Izhar Lotem
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid