Click here to Skip to main content
15,895,740 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.4K   944   76  
Showing some good practices that can be applied to the Presentation Model/MVVM pattern.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Core;
using Castle.Windsor;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
using App.Core;
using App.Data;
using System.Collections.ObjectModel;
using PresentationModelBase;
using Castle.DynamicProxy;
using System.Reflection;
using App.Configuration.Wpf;
using System.Collections.Specialized;
using Castle.MicroKernel.ModelBuilder.Inspectors;



namespace App.Configuration
{
    public class ConfigurationManager
    {
        internal static WindsorContainer _windsor = new WindsorContainer();

        public static void Configure()
        {
            WpfConfiguration();
            InsertStubData();
        }

        static void WpfConfiguration()
        {
            _windsor.Kernel.ComponentModelCreated += ComponentModelCreated;
            GenericConfiguration();

            _windsor.Register
                (
                Component.For<DispatchInterceptor>(),
                Component.For<IDialogSystem>().ImplementedBy<WpfDialogSystem>().LifeStyle.Is(LifestyleType.Singleton),
                Component.For(typeof(ObservableCollection<>), typeof(ICollection<>)).LifeStyle.Is(LifestyleType.Transient)
                );
            
            //Inserting the command properties on the presentation models.
            System.ComponentModel.TypeDescriptor.AddProvider(
                new PresentationModelTypeDescriptionProvider(
                    System.ComponentModel.TypeDescriptor.GetProvider(
                    typeof(PresentationModel))),
                    typeof(PresentationModel));
        }

        static void GenericConfiguration()
        {
            AppDomain.CurrentDomain.SetData("servicelocator", _windsor);
            var propertyDIContributor = _windsor.Kernel.ComponentModelBuilder.Contributors.OfType<PropertiesDependenciesModelInspector>().Single() ;
            _windsor.Kernel.
                ComponentModelBuilder.
                RemoveContributor(propertyDIContributor);
            _windsor.Kernel.ComponentModelCreated += ComponentModelCreated;
            _windsor.Register
                (
                Component.For<BackgroundWorkInterceptor>(),
                Component.For<MainViewPresentationModel, IMainViewPresentationModel>().LifeStyle.Is(LifestyleType.Singleton),
                Component.For<ProductsViewPresentationModel, IEntityCollectionViewPresentationModel<Product>>().LifeStyle.Is(LifestyleType.Singleton),
                Component.For(typeof(DummyDao<>), typeof(IDao<>)).LifeStyle.Is(LifestyleType.Singleton),
                Component.For(typeof(List<>), typeof(IList<>)).LifeStyle.Is(LifestyleType.Transient),
                Component.For<CustomActionPresentationModel, ICustomActionPresentationModel>().LifeStyle.Is(LifestyleType.Transient),
                Component.For<ProductEditViewPresentationModel, IEntityViewPresentationModel<Product>>().LifeStyle.Is(LifestyleType.Transient)
                );
        }

        static void ComponentModelCreated(ComponentModel model)
        {
            if (typeof(PresentationModel).IsAssignableFrom(model.Implementation))
            {
                model.Interceptors.Add(InterceptorReference.ForType<BackgroundWorkInterceptor>());
            }
            if (typeof(PresentationModel).IsAssignableFrom(model.Implementation) ||
                typeof(ObservableCollection<>) == model.Implementation)
            {
                model.Interceptors.Add(InterceptorReference.ForType<DispatchInterceptor>());
            }
        }
       
        private static void InsertStubData()
        {
            IDao<Product> dao = _windsor.Resolve<IDao<Product>>();
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            {
                Product p = new Product();
                p.Name = string.Format("Product {0}", i + 1);
                p.Price = r.Next(2, 523);
                dao.SaveOrUpdate(p);
            }
        }
    }
}

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