Click here to Skip to main content
15,881,709 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 67.7K   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;

namespace App.Core
{
    public class MainViewPresentationModel : ClosableViewPresentationModel, IMainViewPresentationModel
    {
        public MainViewPresentationModel()
        {
            _navigationItems = Get<ICollection<ICustomActionPresentationModel>>();
            _workspaces = Get<ICollection<ISelectableViewPresentationModel>>();

            CreateNavigationItems();
        }

        private void CreateNavigationItems()
        {
            ICustomActionPresentationModel openNewProductView = Get<ICustomActionPresentationModel>();
            openNewProductView.Owner = this;
            openNewProductView.ActionName = "CreateNew";
            openNewProductView.TypeParameters = new Type[] { typeof(Product) };
            openNewProductView.DisplayName = "Create new product";

            ICustomActionPresentationModel openProductsView = Get<ICustomActionPresentationModel>();
            openProductsView.Owner = this;
            openProductsView.ActionName = "ViewAll";
            openProductsView.TypeParameters = new Type[] { typeof(Product) };
            openProductsView.DisplayName = "View all products";

            _navigationItems.Add(openNewProductView);
            _navigationItems.Add(openProductsView);
        }

        private ICollection<ICustomActionPresentationModel> _navigationItems;
        public ICollection<ICustomActionPresentationModel> NavigationItems
        {
            get { return _navigationItems; }
        }

        private ICollection<ISelectableViewPresentationModel> _workspaces;
        public ICollection<ISelectableViewPresentationModel> Workspaces
        {
            get { return _workspaces; }
        }

        public void CreateNew<T>()
            where T : new()
        {
            IEntityViewPresentationModel<T> workspace = Get<IEntityViewPresentationModel<T>>();
            workspace.Entity = new T();
            AddWorkspace(workspace);
        }

        public void EditExisting<T>(T item)
            where T : new()
        {
            IEntityViewPresentationModel<T> workspace = Get<IEntityViewPresentationModel<T>>();
            workspace.Entity = item;
            AddWorkspace(workspace);
        }

        public void ViewAll<T>()
            where T : new()
        {
            IEntityCollectionViewPresentationModel<T> workspace = Get<IEntityCollectionViewPresentationModel<T>>();
            AddWorkspace(workspace);
        }

        void AddWorkspace(ISelectableViewPresentationModel workspace)
        {
            if (!_workspaces.Contains(workspace))
            {
                workspace.RequestClose +=
                       (sender, e) => OnWorkspaceRequestClose(sender);
                Workspaces.Add(workspace);
            }
            SetActiveWorkspace(workspace);
        }

        void OnWorkspaceRequestClose(object sender)
        {
            ISelectableViewPresentationModel workspace = sender as SelectableViewPresentationModel;
            Workspaces.Remove(workspace);
        }

        protected virtual void SetActiveWorkspace(ISelectableViewPresentationModel workspace)
        {
            workspace.IsSelected = true;
        }
    }
}

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