1 / 17

What’s this mvvm thing & why should I care?

Entire article, and much more, is available at www.mknopf.com/topics/code-camp-orlando.html. What’s this mvvm thing & why should I care?. By Michael Knopf Software Developer Software Services for NASA @ Kennedy Space Center. How to contact me. My Blog: www.mknopf.com/contact-me.html

axelle
Download Presentation

What’s this mvvm thing & why should I care?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Entire article, and much more, is available at www.mknopf.com/topics/code-camp-orlando.html What’s this mvvm thing & why should I care? By Michael Knopf Software Developer Software Services for NASA @ Kennedy Space Center www.mknopf.com/topics/code-camp-orlando.html

  2. How to contact me • My Blog: www.mknopf.com/contact-me.html • Personal Email: mike @ mknopf.com www.mknopf.com/topics/code-camp-orlando.html

  3. Items covered in this session • Defining the minimum: what we shouldexpect a successful pattern to do for us • Diving in head-first: looking at code • Say what? I must be missing something • Its an Umbrella • We need some help over here • Commanding & Messaging: a very simple introduction • Conclusion: Does MVVM “fit” you? www.mknopf.com/topics/code-camp-orlando.html

  4. Defining the minimum A successful design pattern must provide at the minimum the following: • Easy to understand • Take little (<= 10% more) to no extra time to implement when compared to alternative methods • Live up to the hype (“cleaner code”, “more testable”, “re-usable”) • Utilize technologies that are likely to stick around for awhile (don’t disappear overnight) www.mknopf.com/topics/code-camp-orlando.html

  5. Are you already using it? probably • The tools for Silverlight & WPF in Visual Studio are built using MVVM (at least that’s what I’ve been told) • Expect confusion!!! Everywhere you look someone shows a different way to implement MVVM, often using a combination of patterns and technologies (i.e. Commanding, Dependency Injection, etc…) that differ greatly from one implementation to another www.mknopf.com/topics/code-camp-orlando.html

  6. Interaction between layers Model View Presentation Model (ViewModel) www.mknopf.com/topics/code-camp-orlando.html

  7. Diving in head-first • Note to Self: Show a code sample dude! www.mknopf.com/topics/code-camp-orlando.html

  8. Say what? • That’s it? What do you mean? I must be missing something, that can’t be all there is? • Exactly! You are missing something, in fact you’re missing key components that are necessary to make any project useful. • This is where the truth comes out www.mknopf.com/topics/code-camp-orlando.html

  9. Mvvm: it takes more than you think for it to work • MVVM, in its pure form doesn’t give us what we need to make real applications. • It’s a one-way street, from the Model (your data source or Business Logic Layer in most cases) to the View (your actual User Interface) via the ViewModel (which really is just like the “controller” in MVC). • Like how does the ViewModel know if a button on the View has been clicked? (see below) • You must combine MVVM with other technologies and patterns (a.k.a. “helpers”) in order to fill in the “gaps”. • This includes “Commanding” through the use of the “DelegatingCommand” (sometimes called the “RelayingCommand”) pattern. This is what you need to communicate from the View back to the ViewModel, like when a button was clicked. www.mknopf.com/topics/code-camp-orlando.html

  10. Mvvm: it’s an umbrella MVVM MESSAGING COMMANDING System.Windows.Interactivity MVVM Lite Expression.Samples.Interactivity Prism / Composite App www.mknopf.com/topics/code-camp-orlando.html

  11. Views => viewmodels View1 View2 ViewModel1 ViewModel2 • Usually there is one ViewModel for each View • Communication directly from a ViewModel to a View or from one ViewModel to another can be difficult, a “Helper” is needed (a.k.a. “messaging”) www.mknopf.com/topics/code-camp-orlando.html

  12. we need a little help over here • Commanding: It’s the magic that maps the events in our View(s) such as a Button Click to a method/event in our ViewModel • Messaging: allows communication back to our view (and between ViewModels) www.mknopf.com/topics/code-camp-orlando.html

  13. commanding • Commanding is what enables us to bind to methods located in our ViewModel(s) • Command can only be executed by “Buttons” (checkbox, button, combo box, ListBox, etc…). There are ways around this • Implement the ICommand Interface • Execute method: actual execution of the method • CanExecute method: True/False indicating if a command can be executed • CanExecuteChanged event: Fires when the CanExecute response is changed (i.e. if your Methods criteria is met and can now be executed you change the value returned from CanExecute and then fire the CanExecutedChanged event www.mknopf.com/topics/code-camp-orlando.html

  14. Commanding, cont. • CODE-BEHIND: • public MyClassConstructor() • { • InitializeComponent(); • this.DataContext = new ViewModel.PeopleViewModel(); • } • IN XAML: • xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" • xmlns:si="clr-namespace:Expression.Samples.Interactivity;assembly=Expression.Samples.Interactivity" • <ListBox • Name="Names" • ItemsSource="{Binding People}" • DisplayMemberPath="Name" • Background="Beige" • Width="200" • Height="250" • HorizontalAlignment="Center" • VerticalAlignment="Center"> • <i:Interaction.Triggers> • <i:EventTrigger EventName="SelectionChanged"> • <si:CallDataMethod Method="HandleSelectionChanged"></si:CallDataMethod> • </i:EventTrigger> • </i:Interaction.Triggers> • </ListBox> The ListBox is DataBound to a collection of People objects. Then the Interaction Trigger sets the EventName to “SelectionChanged” and indicates which method to fire in the ViewModel. www.mknopf.com/topics/code-camp-orlando.html

  15. messaging • There are many different flavors (Prism’s “event aggregator”, MVVMLite’s Messenger class has a “relay” command) • Messages can be “broadcast” (everyone in the View intercepts it) or “targeted” to a specific type of control • You can also use a “token” to target a specific object in the view • It’s somewhat confusing to set up and work with. Getting your head around it will require a little investment in brain power. www.mknopf.com/topics/code-camp-orlando.html

  16. Does it fit you? It depends • If you come from an Model View Controller / Model View Present background then MVVM is exactly what your doing now but implemented on a different technology stack • It’s not the “right way”, it’s also not “overkill”. It all depends on your Core Values. • If it matches your mental-model of the problem space better then an alternative method they use it, otherwise don’t, either way your going to be just fine www.mknopf.com/topics/code-camp-orlando.html

  17. Resource to help you • Laurent Bugnion’s Mix10 talk on Understanding the MVVM pattern & his episode on Silverlight TV • MVVM Litetoolkit • John Papa’s article: 5 steps to simple commanding • Jessie Liberties articles: It’s Not Kool-aid www.mknopf.com/topics/code-camp-orlando.html

More Related