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

An Introduction to the Model-View-ViewModel Pattern

Embed Size (px)

DESCRIPTION

An Introduction to the Model-View-ViewModel Pattern. Seriously? Another MV* Pattern? [email protected]. How I Got Into MVVM. The Healthcare Application. Screenshot of WebStation. The Generalized Problem. What We Want. How Do We Achieve that?. View. ViewModel. Model. Bindings. - PowerPoint PPT Presentation

Citation preview

An Introduction to the Model-View-ViewModel

Pattern

An Introduction to the Model-View-ViewModel

Pattern

Seriously? Another MV* Pattern?

[email protected]

Seriously? Another MV* Pattern?

[email protected]

How I Got Into MVVMHow I Got Into MVVM

QuickTime™ and a decompressor

are needed to see this picture.

The Healthcare Application

The Healthcare Application

Screenshot of WebStationScreenshot of WebStation

QuickTime™ and a decompressor

are needed to see this picture.

QuickTime™ and a decompressor

are needed to see this picture.

The Generalized Problem

The Generalized Problem

QuickTime™ and a decompressor

are needed to see this picture.

What We WantWhat We Want

How Do We Achieve that?

How Do We Achieve that?

View

ViewModel

Model

A More Complete Diagram

A More Complete Diagram

ViewXAML, Code Behind

Unit TestsIntegration Tests

ViewModelProperties, Commands, View Logic

Bindings

Actions

Model

Service Proxies

Web

Data

Events

Behavior

QuickTime™ and a decompressor

are needed to see this picture.

Emerging TechnologyEmerging Technology

We’re still trying to figure out the Best Practices

We’re still trying to figure out the Best Practices

QuickTime™ and a decompressor

are needed to see this picture.

Standing on the Shoulders of

Giants

Standing on the Shoulders of

GiantsMVCMVPPresenter Model

MVCMVPPresenter Model

Martin FowlerJosh SmithJohn GossmanSean Wildermuth

Martin FowlerJosh SmithJohn GossmanSean Wildermuth

QuickTime™ and a decompressor

are needed to see this picture.

Mini PatternsMini Patterns

The Gears That DriveThe Machine

The Gears That DriveThe Machine

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 Pattern

Event Aggregator Pattern

Attached BehaviorsAttached Behaviors<TextBlock FontSize=“32” Foreground=“Red” Width=“200” Behaviors:Trimming.Text=“…”/><TextBlock FontSize=“32” Foreground=“Red” Width=“200” Behaviors:Trimming.Text=“…”/>

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 logic into the ViewModel OR Controller

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 logic into the ViewModel OR Controller

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

PrismCaliburnSilverlight FXMVVM ToolkitSL3?

PrismCaliburnSilverlight FXMVVM ToolkitSL3?

Demo -- Recipe BoxDemo -- Recipe Box

QuickTime™ and a decompressor

are needed to see this picture.

QuickTime™ and a decompressor

are needed to see this picture.

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]

TestingNunit/MoqSilverlight UT

Nunit/MoqSilverlight UT

Contact InfoContact Info

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

http://speakerrate.com/briangenisio

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

http://speakerrate.com/briangenisio

QuickTime™ and a decompressor

are needed to see this picture.