an introduction to the model-view-viewmodel pattern srsly? another mv* pattern?...

22
An Introduction to the Model- View-ViewModel Pattern Srsly? Another MV* Pattern? [email protected]

Upload: dorthy-henry

Post on 17-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

An Introduction to the Model-View-ViewModel

Pattern

An Introduction to the Model-View-ViewModel

Pattern

Srsly? Another MV* Pattern?

[email protected]

Srsly? Another MV* Pattern?

[email protected]

How I Got Into MVVMHow I Got Into MVVM

The Healthcare ApplicationThe Healthcare Application

Screenshot of WebStationScreenshot of WebStation

The Generalized ProblemThe Generalized Problem

What We WantWhat We Want

How Do We Achieve that? How Do We Achieve that?

View

ViewModel

Model

A More Complete DiagramA More Complete Diagram

ViewXAML, Code Behind

Unit TestsIntegration Tests

ViewModelProperties, Commands, View Logic

Bin

din

gs

Actio

ns

Model

Service Proxies

Web

Data

Events

Behavio

r

Twitter Search Demo Using MVVM

Twitter Search Demo Using MVVM

Emerging TechnologyEmerging Technology

We’re still trying to figure out the Best Practices

We’re still trying to figure out the Best Practices

Standing on the Shoulders of Giants

Standing on the Shoulders of GiantsMVCMVPPresenter Model

MVCMVPPresenter Model

Martin Fowler Josh Smith John GossmanSean Wildermuth

Martin Fowler Josh Smith John GossmanSean Wildermuth

Compared to…Compared to…

Mini PatternsMini Patterns

NotifyPropertyChangedNotifyPropertyChanged

public interface INotifyPropertyChanged

{

event PropertyChangedEventHandler

PropertyChanged;

}

public interface INotifyPropertyChanged

{

event PropertyChangedEventHandler

PropertyChanged;

}

Command PatternCommand Patternpublic interface ICommand{ bool CanExecute(object param); void Execute(object param); event EventHandler CanExecuteChanged;}

public interface ICommand{ bool CanExecute(object param); void Execute(object param); event EventHandler CanExecuteChanged;}

public class DelegateCommand : ICommand{ public DelegateCommand( Action<object> command, Predicate<object> canExecute);}

public class DelegateCommand : ICommand{ public DelegateCommand( Action<object> command, Predicate<object> canExecute);}

Event Aggregator PatternEvent Aggregator Pattern

Attached BehaviorsAttached Behaviors<TextBlock Foreground=“Red” Width=“200” Behaviors:Update.WhenTextChanged=“True”/><TextBlock Foreground=“Red” Width=“200” Behaviors:Update.WhenTextChanged=“True”/>

TextBlock

FontSize

Foreground

Width

TrimmingBehavior

MVVM GuidelinesMVVM Guidelines

1. Reduce or eliminate your code-behind

2. Bind all of your UI inputs/outputs to your ViewModel

3. Implement INotifyPropertyChanged on your ViewModel

4. Put your view behavior into the ViewModel

5. Do not put any view state into the model

6. Only bind to a model object if there is no view-specific info

7. When testing, treat ViewModel as the Real UI

8. Avoid events.  Use commands instead

1. Reduce or eliminate your code-behind

2. Bind all of your UI inputs/outputs to your ViewModel

3. Implement INotifyPropertyChanged on your ViewModel

4. Put your view behavior into the ViewModel

5. Do not put any view state into the model

6. Only bind to a model object if there is no view-specific info

7. When testing, treat ViewModel as the Real UI

8. Avoid events.  Use commands instead

Third Party SupportThird Party Support

PrismMVVM LightCaliburnSilverlight FX

PrismMVVM LightCaliburnSilverlight FX

Demo – CodeMash AppDemo – CodeMash App

Who Creates the ViewModel?Who Creates the ViewModel?

XAMLView ConstructorOther ViewModel

MarriageDependency Injection

XAMLView ConstructorOther ViewModel

MarriageDependency Injection

Silverlight [Unit|Integration|Functional] Testing

Silverlight [Unit|Integration|Functional] Testing

Nunit/MoqSilverlight UTWhiteTelerik WebAI

Nunit/MoqSilverlight UTWhiteTelerik WebAI

Contact InfoContact [email protected]://HouseOfBilz.comhttp://Twitter.com/BrianGenisio

OPEN SPACEShttp://speakerrate.com/briangenisio

[email protected]://HouseOfBilz.comhttp://Twitter.com/BrianGenisio

OPEN SPACEShttp://speakerrate.com/briangenisio