Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
EMBARCADERO)TECHNOLOGIES)
Introduction to MVVM
in Delphi
With thanks to Malcolm Groves
www.malcolmgroves.com
)
EMBARCADERO...
EMBARCADERO)TECHNOLOGIES)
Three)types)of)logic)in)an)App)
•  Presenta(on*Logic*
•  Domain*Logic*
•  Data*Access*Logic*
•  ...
EMBARCADERO)TECHNOLOGIES)
TradiAonal)Delphi)Approach)
<  Advantages*
<  Quick*to*Develop*
<  Well*understood*
<  Disadvant...
EMBARCADERO)TECHNOLOGIES)
MVVM)is)about)the)PresentaAon)Layer)
•  Model*is*ignorant*of*Views*and*View*Models*
•  View*Mode...
EMBARCADERO)TECHNOLOGIES)
MVVM)–)View)Model)
•  View*Model*–*literally*the*Model*of*your*View*(UI)*
–  The*data*that*is*ac...
EMBARCADERO)TECHNOLOGIES)
MVVM)
•  1*View*has*1*ViewModel*
•  1*ViewModel*might*be*used*by*mul(ple*Views*
Presenta(on** Do...
EMBARCADERO)TECHNOLOGIES)
Example):)AcAons)
•  ViewModel*decides*if*it*is*OK*to*Save*given*the*
current*state*
•  View*dec...
EMBARCADERO)TECHNOLOGIES)
Things)to)think)about:)
–  Pure*Views*
•  The*less*code*you*write*in*your*View,*the*less*there*i...
EMBARCADERO)TECHNOLOGIES)
More)InformaAon)
•  Series*of*more*detailed*blog*posts*on*MVVM*in*Delphi**
www.malcolmgroves.com...
Upcoming SlideShare
Loading in …5
×

Introduction to mvvm

526 views

Published on

Introduction to MVVM in Delphi, thanks to Malcolm Groves, at 2015 UK Spring Delphi Community Conference

Published in: Software
  • Be the first to comment

  • Be the first to like this

Introduction to mvvm

  1. 1. EMBARCADERO)TECHNOLOGIES) Introduction to MVVM in Delphi With thanks to Malcolm Groves www.malcolmgroves.com ) EMBARCADERO)TECHNOLOGIES) Agenda •  What’s the problem we’re trying to solve? •  Different approaches to solving it. •  MVVM –  What’s a Model? –  What’s a View? –  What’s a View Model? •  Demo –  Common Issues •  Things to think about •  More Resources https://github.com/malcolmgroves/menialtasks
  2. 2. EMBARCADERO)TECHNOLOGIES) Three)types)of)logic)in)an)App) •  Presenta(on*Logic* •  Domain*Logic* •  Data*Access*Logic* •  Where*you*put*it*all*ma8ers.*A*LOT.* EMBARCADERO)TECHNOLOGIES) TradiAonal)Delphi)Approach) <  Presenta(on*Logic*and*Domain*Logic*mixed*together* in*On…*events* <  Data*Access*logic*either*in*Form*or*Data*Modules*or* both* Data Access Logic Presentation Logic Domain Logic
  3. 3. EMBARCADERO)TECHNOLOGIES) TradiAonal)Delphi)Approach) <  Advantages* <  Quick*to*Develop* <  Well*understood* <  Disadvantages* <  Hard*to*maintain.*Bri8le.* <  Hard*to*replace*UI*with*a*new*one.* <  Hard*to*test.* Data Access Logic Presentation Logic Domain Logic EMBARCADERO)TECHNOLOGIES) Not)a)new)problem) •  Lots*of*Presenta(on*Pa8erns* –  MVC*–*Model<View<Controller* –  MVP*–*Model<View<Presenter* –  MVVM*–*Model<View<ViewModel* •  Based*on*Presenta(on*Model*by*Mar(n*Fowler* –  In*turn,*based*on*Applica(on*Model*from*SmallTalk*
  4. 4. EMBARCADERO)TECHNOLOGIES) MVVM)is)about)the)PresentaAon)Layer) •  Model*is*ignorant*of*Views*and*View*Models* •  View*Models*are*ignorant*of*Views* Presenta(on** Domain* Data*Access* Views* View* Models* Model* LiveBindings* EMBARCADERO)TECHNOLOGIES) MVVM)H)Model) •  Model*–*Business*Domain*of*your*App* –  In*iTunes*it’s*all*about*ar(sts,*songs,*plays,*ra(ngs,*etc* –  In*an*Accoun(ng*App*it’s*Accounts,*Transac(ons,*etc* –  In*SalesForce*it’s*Customers,*Ac(vi(es,*Forecasts,*Opportuni(es,*etc* •  Important:*Model*can*be*Unit*Tested!* * Presenta(on** Domain* Data*Access* Views* View* Models* Model* LiveBindings*
  5. 5. EMBARCADERO)TECHNOLOGIES) MVVM)–)View)Model) •  View*Model*–*literally*the*Model*of*your*View*(UI)* –  The*data*that*is*acted*upon*in*this*View* –  The*workflows/processes*that*are*invoked*in*this*View* –  The*UI*rules*that*are*built*into*this*View* •  Important:*ViewModels*can*be*Unit*Tested!* Presenta(on** Domain* Data*Access* Views* View* Models* Model* LiveBindings* EMBARCADERO)TECHNOLOGIES) MVVM)H)View) •  View*–*The*visual*rendering*of*a*View*Model* –  LiveBindings*to*bind*the*ViewModel*data*to*the*UI* –  Minimum*code*needed*to*invoke*methods*in*the*ViewModel* –  Code*specific*to*THIS*View* •  Important:*Views*are*hard*to*Unit*Test* –  So,*the*only*Good*View*is*a*Dumb*View!* Presenta(on** Domain* Data*Access* Views* View* Models* Model* LiveBindings*
  6. 6. EMBARCADERO)TECHNOLOGIES) MVVM) •  1*View*has*1*ViewModel* •  1*ViewModel*might*be*used*by*mul(ple*Views* Presenta(on** Domain* Data*Access* Views* View* Models* Model* LiveBindings* EMBARCADERO)TECHNOLOGIES) Quick)Example)Tour)H)MenialTasks) •  Disclaimers:* –  Example*is*focused*on*introducing*MVVM,*nothing*else* –  Purposely*simple* •  Bare*minimum*of*features*to*show*key*MVVM*topics* –  No*Persistence* –  No*IoC*
  7. 7. EMBARCADERO)TECHNOLOGIES) Example):)AcAons) •  ViewModel*decides*if*it*is*OK*to*Save*given*the* current*state* •  View*decides*how*to*represent*that*in*the*UI,*eg.* Disabling*the*bu8on* TTaskView* TTaskViewModel* TTask* procedure Save; property CanSave : boolean property IsValid: boolean EMBARCADERO)TECHNOLOGIES) *****TMainViewModel******TMainView* Example:)Displaying)a)child)Form) 1.  At startup, View provides the ViewModel an Anonymous Method to call when a Child View is needed 2.  Sometime later, the Add Task button is clicked 3.  ViewModel creates the child ViewModel, and calls the Anonymous Method, passing the child ViewModel as a parameter 4.  The anonymous method creates the child View, passing it the child ViewModel, then shows it. 5.  When the child View closes, the anonymous method cleans up, refreshes bindings, etc, as needed. 6.  When the anonymous method returns, the ViewModel still has access to the child ViewModel, so can access its data if required ViewModel.AddNewTask OnEditTask returns OnEditTask invoked
  8. 8. EMBARCADERO)TECHNOLOGIES) Things)to)think)about:) –  Pure*Views* •  The*less*code*you*write*in*your*View,*the*less*there*is*untested* –  View*First*vs*View*Model*First?* –  View*<>*Form* •  A*View*could*be*a*Panel* •  A*View*could*be*a*wrapper*around*ShowMessage* EMBARCADERO)TECHNOLOGIES) Things)to)think)about:) –  MVVM*doesn’t*dictate*IoC,*but*it*can*simplify*your*code* and*your*tests.* –  You*don’t*need*a*MVVM*Framework* •  But*you’ll*probably*end*up*wri(ng*one.*Everyone*else*does.* –  Try*it.*Everything*is*clumsy*at*first.*If*MVVM*doesn’t*work* for*you,*try*MVC,*MVP,*etc.*
  9. 9. EMBARCADERO)TECHNOLOGIES) More)InformaAon) •  Series*of*more*detailed*blog*posts*on*MVVM*in*Delphi** www.malcolmgroves.com* •  MenialTasks*source*online*at*h8ps://github.com/malcolmgroves**** •  Other*Resources*(list*online*at*h8p://mgrov.es/mvvmlist*)* –  Books* •  Developer’s*Guide*to*Microsoc*Prism** h8p://msdn.microsoc.com/en<us/library/gg406140.aspx* •  Advanced*MVVM*h8p://amzn.com/B0038KX9FW* –  Ar(cles* •  WPF*Apps*with*the*Model<View<ViewModel*Pa8ern*–*Josh*Smith*h8p://msdn.microsoc.com/en<us/magazine/ dd419663.aspx* –  Videos* •  Build*Your*Own*MVVM*Framework*–*Rob*Eisenberg*(MIX10*Conference)* h8p://channel9.msdn.com/Events/MIX/MIX10/EX15* •  Thank*You!*

×