Click here to Skip to main content
15,897,518 members
Articles / Desktop Programming / WPF

Presentation Model (MVVM) Good Practices

Rate me:
Please Sign up or sign in to vote.
4.81/5 (13 votes)
11 May 2010CPOL16 min read 68.6K   944   76  
Showing some good practices that can be applied to the Presentation Model/MVVM pattern.
using System;
using System.Collections.Generic;
using System.Text;
using PresentationModelBase;
using System.Threading;
using System.ComponentModel;

namespace App.Core
{
    public class EntityCollectionViewPresentationModel<T> : SelectableViewPresentationModel, IEntityCollectionViewPresentationModel<T>
        where T : class, new()
    {
        public EntityCollectionViewPresentationModel()
        {
            _mainView = Get<IMainViewPresentationModel>(); //getting a reference to the main window.
            _entityDao = Get<IDao<T>>();
            _entityCollection = Get<ICollection<IEntityViewPresentationModel<T>>>();
            _entityDao.Changed += _entityDao_Changed;
        }

        void _entityDao_Changed(object sender, ListChangedEventArgs e)
        {
            T item = (T)sender;
            switch (e.ListChangedType)
            {
                case ListChangedType.ItemAdded:
                    var newView = Get<IEntityViewPresentationModel<T>>();
                    newView.Entity = item;
                    _entityCollection.Add(newView);
                    break;
                case ListChangedType.ItemChanged:
                    var view = Find(item);
                    view.Entity = null;
                    view.Entity = item;
                    break;
            }
        }

        IEntityViewPresentationModel<T> Find(T entity)
        {
            foreach (var item in _entityCollection)
            {
                if (entity == item.Entity)
                    return item;
            }
            return null;
        }

        IMainViewPresentationModel _mainView;
        IDao<T> _entityDao;

        public void LoadData()
        {
            Thread.Sleep(3000);
            _entityCollection.Clear();
            foreach (T item in _entityDao.SelectAll())
            {
                var entityView = Get<IEntityViewPresentationModel<T>>();
                entityView.Entity = item;
                _entityCollection.Add(entityView);
            }
        }

        #region IEntityCollectionViewPresentationModel<T> Members

        private ICollection<IEntityViewPresentationModel<T>> _entityCollection;
        public ICollection<IEntityViewPresentationModel<T>> EntityCollection
        {
            get { return _entityCollection; }
        }

        private IEntityViewPresentationModel<T> _selected;
        public virtual IEntityViewPresentationModel<T> Selected
        {
            get { return _selected; }
            set
            {
                if (_selected == value)
                    return;
                _selected = value;
                OnPropertyChanged("Selected");
                OnPropertyChanged("CanRemove");
            }
        }    

        public void CreateNew()
        {
            _mainView.CreateNew<T>();
        }

        public void RemoveSelected()
        {
            if (AskQuestion("Are you sure you remove this item?") == QuestionResult.Yes)
            {
                _entityDao.Remove(_selected.Entity);
                _entityCollection.Remove(_selected);
            }
        }

        public bool CanRemove
        {
            get { return Selected != null; }
        }

        #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
Brazil Brazil
Software developer specialized in the .NET framework

Comments and Discussions