Click here to Skip to main content
15,891,033 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.2K   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 System.ComponentModel;
using System.Reflection;

namespace App.Core
{
    class EntityViewPropertyDescriptor : PropertyDescriptor
    {
        private Type _entityType;
        private Type _entityViewType;
        private PropertyInfo _entityProperty;
        private string _entityViewPropertyName;
        private FieldInfo _entityField;
        MethodInfo _propertyChangeNotification; 

        static Type GetPropertyType(string propertyName, Type entityType)
        {
            return entityType.GetProperty(propertyName).PropertyType;
        }

        static string GetPropertyName(string entityPropertyName, Type entityType)
        {
            return entityType.Name + entityPropertyName;
        }

        public EntityViewPropertyDescriptor(string entityPropertyName, Type entityType, Type entityViewType)
            : base(
            TypeDescriptor.CreateProperty
            (entityViewType, GetPropertyName(entityPropertyName, entityType), GetPropertyType(entityPropertyName, entityType), null))
        {
            _entityType = entityType;
            _entityProperty = _entityType.GetProperty(entityPropertyName);
            _entityViewPropertyName = entityType.Name + entityPropertyName;
            _entityViewType = entityViewType;
            _entityField = _entityViewType.GetField("_entity", BindingFlags.NonPublic | BindingFlags.Instance);
            _propertyChangeNotification = _entityViewType.GetMethod("OnPropertyChanged", BindingFlags.NonPublic | BindingFlags.Instance);
        }

        public override bool CanResetValue(object component)
        {
            return false;
        }

        public override Type ComponentType
        {
            get { return _entityViewType; }
        }

        public override object GetValue(object component)
        {
            object innerEntity = _entityField.GetValue(component);
            return _entityType.GetProperty(_entityProperty.Name).GetValue(innerEntity, null);
        }

        public override bool IsReadOnly
        {
            get { return !_entityProperty.CanWrite; }
        }

        public override Type PropertyType
        {
            get { return _entityProperty.PropertyType; }
        }

        public override void ResetValue(object component)
        {
            throw new NotImplementedException();
        }

        public override void SetValue(object component, object value)
        {
            if (_entityProperty.CanWrite)
            {
                object innerEntity = _entityField.GetValue(component);
                _entityProperty.SetValue(innerEntity, value, null);
                _propertyChangeNotification.Invoke(component, new object[] { _entityViewPropertyName });
            }
        }

        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }


    }
}

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