Click here to Skip to main content
12,623,776 members (28,726 online)
Click here to Skip to main content

Stats

16.3K views
309 downloads
22 bookmarked
Posted

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
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
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 Continent
    /// </summary>
    public class ContinentViewModel : IContinentViewModel
    {
        #region Members

        Continent m_Continent;        

        #endregion

        #region Properties

        /// <summary>
        /// A property for extracting the continent 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 Continent Continent
        {
            get { return m_Continent; }
            set { m_Continent = value; }
        }

        #endregion

        #region Methods

        #region Ctor

        public ContinentViewModel(Continent continent)
        {
            m_Continent = continent;
        }

        #endregion

        #region IContinentViewModel Members

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

        /// <summary>
        /// Since the name can only be changed in edit mode, the property is declared virtual and would be overriden in the Editable View model
        /// </summary>
        public virtual string ContinentName
        {
            get
            {
                return m_Continent.Name;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        /// Since the number of countries can only be changed in edit mode, the property is declared virtual and would be overriden in the Editable View model
        /// </summary>
        public virtual int NumberOfCountries
        {
            get
            {
                return m_Continent.NumberOfCountries;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        /// Since the best city to be in can only be changed in edit mode, the property is declared virtual and would be overriden in the Editable View model
        /// </summary>
        public virtual string BestCityToBeIn
        {
            get
            {
                return m_Continent.BestCityToBeIn;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #endregion        
    }

    /// <summary>
    /// This is the Continent read only view model
    /// </summary>
    public class ReadOnlyContinentViewModel : ContinentViewModel, IReadOnlyContinentViewModel
    {
        #region Members

        List<IReadonlyCountryViewModel> m_Countries;

        #endregion

        #region Methods

        #region Ctor

        public ReadOnlyContinentViewModel(Continent continent) : base(continent)
        {
            m_Countries = new List<IReadonlyCountryViewModel>();
            // Create a view model for each of the continents coutries
            for (int iter = 0; iter < continent.Countries.Count; iter++)
            {
                m_Countries.Add(new ReadOnlyCountryViewModel(continent.Countries[iter]));
            }
        }

        #endregion

        #endregion

        #region IReadOnlyContinentViewModel Members   
     
        public event SimpleOperationDelegate OnBestCityToBeInChanged;

        public IList<IReadonlyCountryViewModel> Countries
        {
            get 
            {
                return m_Countries;
            }
        }

        #endregion

        #region IReadOnlyObject Members

        /// <summary>
        /// Reload the data from repository
        /// </summary>
        public void Refresh()
        {
            DataManager.Instance.LoadDataFromStorage();
            this.Continent = DataManager.Instance.GetContinent(this.Id);
            m_Countries.Clear();
            for (int iter = 0; iter < this.Continent.Countries.Count; iter++)
            {
                m_Countries.Add(new ReadOnlyCountryViewModel(this.Continent.Countries[iter]));
            }
        }

        #endregion
    }

    /// <summary>
    /// This is the view model which enables to add a continent or edit it
    /// </summary>
    public class EditableContinentViewModel : ContinentViewModel, IEditableContinentViewModel
    {
        #region Members

        /// <summary>
        /// hold the original values of the continent, in case a cancelation is made
        /// </summary>
        Continent m_OrigContinent;
        /// <summary>
        /// A flag which determines if the mode is add or edit
        /// </summary>
        bool m_bEditMode;

        #endregion

        #region Methods

        #region Ctor

        public EditableContinentViewModel(Continent continent, bool bEditMode)
            : base(continent)
        {
            m_OrigContinent = new Continent(continent);
            m_bEditMode = bEditMode;
        }

        #endregion        

        #region IEditableContinentViewModel Members        

        /// <summary>
        /// The edit view model is the only place in which the property could be changed. Therefore it is overriden
        /// </summary>
        public override string ContinentName
        {
            get
            {
                return base.ContinentName;
            }
            set
            {
                this.Continent.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 NumberOfCountries
        {
            get
            {
                return base.NumberOfCountries;
            }
            set
            {
                this.Continent.NumberOfCountries = value;
            }
        }

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

        #endregion

        #region IEditableObject Members

        /// <summary>
        /// Save the continent into the repository
        /// </summary>
        public void Save()
        {
            if (!m_bEditMode)
            {
                DataManager.Instance.AddContinent(this.Continent);
            }
            DataManager.Instance.SaveDataIntoStorage();
            if (OnSaveCompleted != null)
            {
                OnSaveCompleted(true);
            }
        }

        /// <summary>
        /// Cancel the edit operation. If in edit mode, return the original values to the continent
        /// </summary>
        public void Cancel()
        {
            if (m_bEditMode)
            {
                this.Continent.Name = m_OrigContinent.Name;
                this.Continent.NumberOfCountries = m_OrigContinent.NumberOfCountries;
                this.Continent.BestCityToBeIn = m_OrigContinent.BestCityToBeIn;
            }
        }

        public event OperationCompletedDelegate OnSaveCompleted;

        #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

You may also be interested in...

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