Click here to Skip to main content
12,630,025 members (27,550 online)
Click here to Skip to main content

Stats

44.1K views
802 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.Linq;
using System.Text;
using System.Windows.Input;
using PresentationModelBase;
using System.Reflection;

namespace App.Configuration
{
    class MethodCallCommand : ICommand
    {
        public MethodCallCommand(PresentationModel target) 
        {
            _target = target;
        }

        PresentationModel _target;

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            MethodInfo currentMethod = _target.GetType().GetMethod(parameter.ToString(), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (currentMethod == null)
                throw new ArgumentException("Cannot find this method");

            ParameterInfo[] parameters = currentMethod.GetParameters();
            if (parameters != null && parameters.Length > 0)
                throw new ArgumentException("Methods called by this command must have no parameters");

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

            _target.InvokeMethod(currentMethod);
        }

        protected virtual void Execute(MethodInfo mi) 
        {
            mi.Invoke(_target, null);
        }

        #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...

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