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 System.ComponentModel;
using PresentationModelBase;

namespace App.Core
{
    public abstract class EntityViewPresentationModel<T> : SelectableViewPresentationModel, IEntityViewPresentationModel<T>
        where T : new()
    {
        static EntityViewPresentationModel()
        {
            TypeDescriptor.AddProvider(
                new EntityViewTypeDescriptorProvider(
                    TypeDescriptor.GetProvider(typeof(
                    EntityViewPresentationModel<T>)), typeof(T)),
                    typeof(EntityViewPresentationModel<T>));
        }


        public EntityViewPresentationModel()
        {
            _entityDao = Get<IDao<T>>();
            _mainView = Get<IMainViewPresentationModel>();
        }

        protected IList<PropertyDescriptor> GetEncapsulationProperties()
        {
            IList<PropertyDescriptor> properties = Get<IList<PropertyDescriptor>>();
            foreach (PropertyDescriptor item in TypeDescriptor.GetProperties(this))
            {
                if (item is EntityViewPropertyDescriptor)
                    properties.Add(item);
            }
            return properties;
        }

        protected object GetPropertyValue(string propertyName)
        {
            PropertyDescriptor property = TypeDescriptor.GetProperties(this.GetType()).Find(propertyName, false);
            return property.GetValue(this);
        }

        protected void SetPropertyValue(string propertyName, object value) 
        {
            PropertyDescriptor property = TypeDescriptor.GetProperties(this.GetType()).Find(propertyName, false);
            property.SetValue(this, value);
        }

        #region Fields

        protected IMainViewPresentationModel _mainView;
        protected IDao<T> _entityDao;
        protected T _entity;

        #endregion

        T IEntityViewPresentationModel<T>.Entity
        {
            get { return _entity; }
            set
            {
                _entity = value;
                foreach (EntityViewPropertyDescriptor item in GetEncapsulationProperties())
                {
                    OnPropertyChanged(item.Name);
                }
                OnPropertyChanged("DisplayName");
            }
        }

        public void SaveOrUpdate()
        {
            if (AskQuestion("Are you sure you want to save?") == QuestionResult.Yes) 
            {
                _entityDao.SaveOrUpdate(_entity);
                Close();
            }
        }

        public void OpenEditView() 
        {
            _mainView.EditExisting<T>(_entity);
        }
    }
}

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
Web02 | 2.8.1411023.1 | Last Updated 11 May 2010
Article Copyright 2010 by Thiago de Arruda
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid