KEMBAR78
Understanding The MVVM Pattern (TechDays Belgium) | PPTX
Understanding the
MVVM pattern
Laurent Bugnion
Senior User Experience Integrator
IdentityMine
http://[blog|www].galasoft.ch
What is MVVM?

 • A weird acronym
   • Model – View – ViewModel
   • « MVVM in a SLOOB with SLUT »


 • Woven into WPF/SL
 • Some confusion:
   • MVVM is really just the pattern
   • The rest are helpers, practices, etc
The MVC pattern




                  Model                View




                          Controller
The Passive View pattern




                 Model                  View




                           Controller
The Presentation Model pattern




                 Model                  View

                                        DataBinding


                         Presentation
                            Model
                         (ViewModel)
Blendability

  • Testability, Maintainability, Blendability

  • “The ability to be edited in Blend”

     • (and Visual Studio designer…)

  • Differentiate code in design and runtime

  • Create design time data
Hello MVVM (and Hi Blend)
Bridging the gap


                            View


                   DataBinding      Commands   Messages




                             ViewModel




                            Model
Bridging the gap

                                                View


            View
                            Messages       ViewModel


         ViewModel
                                         View

                   Messages
                   + callback

                                       ViewModel
Commands

 • “Point of entry” for a method”

 • Can be data bound

 • ICommand interface

   • Execute method

   • CanExecute method

   • CanExecuteChanged event handler
Commands
and RelayCommands
RelayCommands


  RelayCommand MyCommand
    = new RelayCommand(
      () => ...,
      () => true | false);

  RelayCommand<T> MyGenericCommand
    = new RelayCommand<T>(
      p => ...,
      p => true | false);
Messaging

 • Should be simple by default

 • Possible to open filtered channels

    • To a target Type (or an Interface)

    • With a Token

    • With various Messenger instances

 • No constraints at all
Messaging
Messaging


  Messenger.Default.Register<T>(
      this,
      p => DoSomething(p));

  Messenger.Default.Send<T>(p);
MVVM Light Toolkit

  • “Breaking the monotony”

  • http://www.galasoft.ch/mvvm/getstarted

  • http://mvvmlight.codeplex.com

  • V3 released at MIX10

  • Keep it small
  • Keep it simple
MVVM Light Libraries V3

  • ESSENTIALS
    • RelayCommand
    • Messenger
    • ViewModelBase
  • EXTRAS
    • EventToCommand
    • DispatcherHelper
MVVM Light Tools V3


 • Project templates (VS + Blend)


 • Item templates (VS + Blend)


 • Code snippets
MVVM Light Toolkit,
Unity,
Blendability
Flaws of MVVM

  • Too much code needed
    • INotifyPropertyChanged
    • Commands
  • Some operations are tricky (set focus…)
  • Not applicable to ASP.NET, etc
  • Message to community is not clear
Debunking the myths

  • “No code in the code behind”
    • “If you put code in the View's code-behind, the
     MVVM police will take your family away”
    • “No need for converters”

  • “MVVM is only suitable for big projects”
  • “MVVM costs performance”
  • “MVVM is complex / makes my head hurt”
Debunking the myths

  • “I don’t need separation because I don’t
   have a designer”
  • “You cannot use MVVM with other
   patterns”
  • “MVVM will make you rich and attractive”
MVVM on the
Windows Phone 7
http://blog.galasoft.ch
http://www.galasoft.ch/mvvm/getstarted
@LBugnion

Understanding The MVVM Pattern (TechDays Belgium)

  • 2.
    Understanding the MVVM pattern LaurentBugnion Senior User Experience Integrator IdentityMine http://[blog|www].galasoft.ch
  • 3.
    What is MVVM? • A weird acronym • Model – View – ViewModel • « MVVM in a SLOOB with SLUT » • Woven into WPF/SL • Some confusion: • MVVM is really just the pattern • The rest are helpers, practices, etc
  • 4.
    The MVC pattern Model View Controller
  • 5.
    The Passive Viewpattern Model View Controller
  • 6.
    The Presentation Modelpattern Model View DataBinding Presentation Model (ViewModel)
  • 7.
    Blendability •Testability, Maintainability, Blendability • “The ability to be edited in Blend” • (and Visual Studio designer…) • Differentiate code in design and runtime • Create design time data
  • 8.
    Hello MVVM (andHi Blend)
  • 9.
    Bridging the gap View DataBinding Commands Messages ViewModel Model
  • 10.
    Bridging the gap View View Messages ViewModel ViewModel View Messages + callback ViewModel
  • 11.
    Commands • “Pointof entry” for a method” • Can be data bound • ICommand interface • Execute method • CanExecute method • CanExecuteChanged event handler
  • 12.
  • 13.
    RelayCommands RelayCommandMyCommand = new RelayCommand( () => ..., () => true | false); RelayCommand<T> MyGenericCommand = new RelayCommand<T>( p => ..., p => true | false);
  • 14.
    Messaging • Shouldbe simple by default • Possible to open filtered channels • To a target Type (or an Interface) • With a Token • With various Messenger instances • No constraints at all
  • 15.
  • 16.
    Messaging Messenger.Default.Register<T>( this, p => DoSomething(p)); Messenger.Default.Send<T>(p);
  • 17.
    MVVM Light Toolkit • “Breaking the monotony” • http://www.galasoft.ch/mvvm/getstarted • http://mvvmlight.codeplex.com • V3 released at MIX10 • Keep it small • Keep it simple
  • 18.
    MVVM Light LibrariesV3 • ESSENTIALS • RelayCommand • Messenger • ViewModelBase • EXTRAS • EventToCommand • DispatcherHelper
  • 19.
    MVVM Light ToolsV3 • Project templates (VS + Blend) • Item templates (VS + Blend) • Code snippets
  • 20.
  • 21.
    Flaws of MVVM • Too much code needed • INotifyPropertyChanged • Commands • Some operations are tricky (set focus…) • Not applicable to ASP.NET, etc • Message to community is not clear
  • 22.
    Debunking the myths • “No code in the code behind” • “If you put code in the View's code-behind, the MVVM police will take your family away” • “No need for converters” • “MVVM is only suitable for big projects” • “MVVM costs performance” • “MVVM is complex / makes my head hurt”
  • 23.
    Debunking the myths • “I don’t need separation because I don’t have a designer” • “You cannot use MVVM with other patterns” • “MVVM will make you rich and attractive”
  • 24.
  • 25.

Editor's Notes

  • #9 Demo of connection to WCF service, fails in BlendShow how to attach to processIdentify problemIsInDesignMode
  • #13 Demo of connection to WCF service, fails in BlendShow how to attach to processIdentify problemIsInDesignMode
  • #16 Demo of connection to WCF service, fails in BlendShow how to attach to processIdentify problemIsInDesignMode
  • #21 Demo of connection to WCF service, fails in BlendShow how to attach to processIdentify problemIsInDesignMode
  • #25 Demo of connection to WCF service, fails in BlendShow how to attach to processIdentify problemIsInDesignMode