【C5】D l hi/C B ild テク カルセ シ ン【C5】Delphi/C++Builderテクニカルセッション

エンバカデロ・テクノロジーズ
Malcolm Groves

Agenda What’s the pproblem we’re trying
y g to solve?
 Different approaches to solving it.
 MVVM What’s a Model?
 What’s a View?
 WhatWhat’ss a View Model?
 Demo Common Issues
 Things to think about More ResourcesThree types of logic in an App
 Presentation Logic
 Domain Logic Data Access Logic Where you put it all matters. A LOT.

Traditional Delphi Approach
Data Access LogicPresentation Logic
Domain Logic

Presentation Logic and Domain Logic mixed
together in On…
On events- Data Access logic either in Form or Data
Modules or both-Traditional Delphi Approach
Data Access Logic
Presentation Logic
Domain Logic-Advantages- Quick
Qu c to Develope e op- Well understood-Disadvantages- Hard to maintain. Brittle.
- Hard to replace UI with a new one.
- Hard to test.
test

Not a new problem
 Lots of Presentation Patterns

 MVC – Model-View-Controller
 MVP – Model
Model-View-Presenter
View Presenter MVVM – Model-View-ViewModel○ Based on Presentation Model byy Martin Fowler
 In turn, based on Application Model from SmallTalk

MVVM is about the Presentation Layer
Presentation

DomainView
Models

Views

Data AccessM d lModelLiveBindings

 Model is ignorant
gof Views and View Models
 View Models are ignorant of Views

MVVM - Model
Presentation

DomainView
Models

Views

Data Access

M d l
Model

LiveBindingsModel – Business Domain of your AppImportant:pModel can be Unit Tested!

 In iTunes it
it’ss all about artists,
artists songs
songs, plays,
plays ratings,ratings etc
 In an Accounting App it’s Accounts, Transactions, etc
 In SalesForce it’s Customers, Activities, Forecasts, Opportunities, etc

MVVM – View Model
Presentation

DomainView
ModelsViews

M d lModelLiveBindings

View Model – literally the Model of your View (UI)

Important: ViewModels can be Unit Tested!

 TheTh datad t thatth t isi acted
t d upon ini thisthi View
Vi The workflows/processes that are invoked in this View
 The UI rules that are built into this View

Data AccessMVVM - View
Presentation

DomainView
Models

Views

M d l
Model

LiveBindings

View – The visual rendering of a View ModelImportant: Views are hard to Unit Test

 LiveBindings to bind the ViewModel data to the UI
 Minimum code needed to invoke methods in the ViewModel Code specific to THIS View
 So,
So the only Good View is a Dumb View!

Data AccessQuick Example Tour - MenialTasks
 Disclaimers: Example is focused on introducing MVVM, nothingelse Purposely simple○ Bare minimum of features to show key MVVM topics

 No PersistenceExample : Actions
TTaskView

TTaskViewModel

TTask

procedure Save;

property CanSave :booleanproperty IsValid:boolean ViewModel decides if it is OK to Save given thecurrentt statet t View decides how to represent that in the UI, eg.
Di bli thDisabling
the bbuttonttView First / Dominant View
TMainView
Model

TTaskView
ModelViewModel First / Dominant
ViewModel
id lTMainView
Model

TTaskView
ModelExample: Displaying a child Form
TMainViewModel

TMainView

1. At startup, View provides theViewModel an Anonymous Method tocall when a Child View is needed
2 Sometime later
2.
later, the Add Task button
is clicked

4. The anonymous method creates thechild View
View, passing it the child
ViewModel, then shows it.
5. When the child View closes, theanonymous method cleans up,
refreshes bindings, etc, as needed.

3. ViewModel creates the child
ViewModel, and calls the AnonymousMethod, passing the child ViewModelas a parameter

OnEditTask returns

6. When the anonymous method
returns,
still has
etu s, tthee ViewModeleode stasaccess to the child ViewModel, so
can access its data if requiredViewModel First vs View First
Less obvious at first- A few more hoops tojump through+ Views stay dumb
+ More logic becomesunit testable-+ More closely
resembles traditional
Delphi model.
+ Simplerp to understand- Views end up smarter
than I like
- Hard to Unit Test View
interactions

Things to think about: Pure Views○ The less code you write in your View, the less there isuntested View First vs View Model First?
 View <> Form
○ A View could be a Panel, a Frame, a control…

Things to think about: MVVM doesn’t
doesn t dictate IoC, but it can simplify yourcode and your tests.
 You don’t need a MVVM Framework○ But you’ll probably end up writing one. Everyone

else does.

 TryT it.
it EverythingE
thi isi clumsy
latt first.
fi t If MVVM

doesn’t work for you, try MVC, MVP, etc.

More Information
 Series of more detailed blog posts on MVVM in Delphi

over theh comingi monthsh at www.malcolmgroves.com
 MenialTasks source online at https://github.com/malcolmgroves Other Resources (list online at http://mgrov.es/mvvmlist)pg Books○ Developer’s Guide to Microsoft Prism

http://msdn.microsoft.com/en-us/library/gg406140.aspx○ Advanced MVVM http://amzn.com/B0038KX9FW
htt ///B0038KX9FW

 Articles○ WPF Apps with the Model-View-ViewModel Pattern – Josh
Smith http://msdn.microsoft.com/en
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
us/magazine/dd419663.aspx
 Videos○ Build Your Own MVVM Framework – Rob Eisenberg (MIX10Conference) http://channel9.msdn.com/Events/MIX/MIX10/EX15