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

Presentation Model (MVVM) Good Practices

, 11 May 2010 CPOL
Showing some good practices that can be applied to the Presentation Model/MVVM pattern.
PresentationModelDemo.zip
App.Configuration
App.Configuration.Wpf
Interceptors
Properties
Wpf
Properties
Wpf
App.Core
Model
PresentationModel
Properties
Services
App.Data
Properties
libs
AttachedCommandBehavior.dll
Castle.Core.dll
Castle.DynamicProxy2.dll
Castle.MicroKernel.dll
Castle.Windsor.dll
PresentationModelBase
Properties
WPFUI
Properties
Settings.settings
View
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)

Share

About the Author

Software developer specialized in the .NET framework

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 11 May 2010
Article Copyright 2010 by Thiago de Arruda
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid