Click here to Skip to main content
12,297,542 members (53,796 online)
Click here to Skip to main content

Stats

42.3K views
785 downloads
73 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.ComponentModel.Design;
using System.Reflection;

namespace PresentationModelBase
{
    /// <summary>
    /// Base class for all PresentationModels.
    /// </summary>
    public abstract class PresentationModel : IPresentationModel
    {
        static PresentationModel()
        {
            _serviceLocator = AppDomain.CurrentDomain.GetData("servicelocator") as IServiceProvider;
            if (_serviceLocator == null)
                _serviceLocator = CreateProvider();
        }

        private static IServiceProvider CreateProvider()
        {
            ServiceCreator creator = new ServiceCreator();
            return new ServiceContainer(creator);
        }

        #region Fields

        static IServiceProvider _serviceLocator;

        #endregion

        #region Methods

        protected T Get<T>()
        {
            return (T)_serviceLocator.GetService(typeof(T));
        }

        public virtual void InvokeMethod(string methodName, object[] parameters, params Type[] typeParameters)
        {
            MethodInfo currentMethod = this.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (currentMethod == null)
                throw new ArgumentException("Cannot find this method");

            Type returnType = currentMethod.ReturnType;
            if (returnType != typeof(void))
                throw new ArgumentException("Methods called by this command must return void");

            if (currentMethod.IsGenericMethodDefinition)
                currentMethod = currentMethod.MakeGenericMethod(typeParameters);

            currentMethod.Invoke(this, parameters);
        }

        #endregion

        #region Properties

        private string _displayName;
        public virtual string DisplayName
        {
            get { return _displayName; }
            set { _displayName = value; }
        }

        #endregion

        #region INotifyPropertyChanged Members

        private PropertyChangedEventHandler _handler;
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { _handler += value; }
            remove { _handler -= value; }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (_handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                _handler(this, e);
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            OnDispose();
        }

        protected virtual void OnDispose() { }

        #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

You may also be interested in...

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