Click here to Skip to main content
12,503,681 members (53,802 online)
Click here to Skip to main content

Stats

43.4K views
795 downloads
74 bookmarked
Posted

Presentation Model (MVVM) Good Practices

, 11 May 2010 CPOL
Showing some good practices that can be applied to the Presentation Model/MVVM pattern.
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
View
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)

Share

About the Author

Software developer specialized in the .NET framework

You may also be interested in...

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