Design Patterns for MVVM Unit Testing Testability Benjamin
Design Patterns for MVVM Unit Testing & Testability Benjamin Day
Benjamin Day • Consultant, Coach, Trainer • Scrum. org Classes § Professional Scrum Developer (PSD) § Professional Scrum Foundations (PSF) • • Tech. Ed, VSLive, Dev. Teach, O’Reilly OSCON Visual Studio Magazine, Redmond Developer News Microsoft MVP for Visual Studio ALM Team Foundation Server, TDD, Testing Best Practices, Silverlight, Windows Azure • http: //blog. benday. com • benday@benday. com
Agenda • My assumptions • Super-fast overview § Model-View. Model (MVVM) § Unit testing • How to build stuff and test stuff.
Assumptions • Automated tests are required for “done” • Unit tests are written by developers. • QA testing is different from developer testing. • MVVM in Silverlight is harder than WPF § (My demos will be in Silverlight. )
Design for testability? • Way of architecting your application • Easy to write & run automated tests
Things that need to be architected. • Requirement: design for testability • Requirement: testability in isolation § They call them unit tests for a reason. § Helps to remember Single Responsibility Principle (SRP) • In Silverlight, figure out async first. § Not planning for async will crush SRP.
SOLID Principles of Class Design http: //butunclebob. com/Article. S. Uncle. Bob. Principles. Of. Ood Principle Purpose Single Responsibility Principle A class should have one, and only one, reason to change. Open Closed Principle You should be able to extend a class’s behavior without modifying it. Liskov Substitution Principle Derived classes must be substitutable for their base classes. Interface Segregation Principle Make fine grained interfaces that are client specific. Dependency Inversion Principle Depend on abstractions, not on concretions.
Single Responsibility Principle • http: //tinyurl. com/ahap 3 j • Posters by Derick Bailey
Things that need to be tested. Goal: test your application without running the UI • Combo. Box / List. Box § Population of lists § Selection logic • Field-based logic § Value, Visibility, Validation § Dependencies between fields • Message. Boxes § Alerts and exceptions • Progress. Bar logic • Model to Data Access • View. Model to Model
Overview of unit testing.
What is a Unit Test? • Piece of code that verifies that another piece of code • Test code verifies application code
Why Write Unit Tests? • High-quality code § Fewer bugs § Clean design § Clean code • Professional Responsibility § Proof that your code works § Notification when your code is broken § Quality focus throughout the development cycle • Side Effects § Code is easier to maintain, refactor § Self-documenting
Plan for testability? • • If you build it, it needs to be tested. If you can test it with an automated test, it’s better. When you build, think of how to test it. The architecture changes when you think about how to test. • It is important to remember the “Single Responsibility Principle”
So what is this MVVM thing?
Overview of MVVM.
What is MVVM? • • Model-View. Model User interface interaction design pattern Cousin of Model-View-Controller (MVC) Enabled by data binding in WPF, Silverlight, WP 7
Why use MVVM? • …or MVC or MVP? • Keep code organized • Separate UI implementation from the logic • Keep code out of the “code behind” (*. xaml. cs) § Hint: this enables Design for Testability
Our “To Do” list • Architect the Silverlight Async solution • Re-usable fields § Values, Visibility, and Validation • List-based fields § Combo. Box and List. Box • • Message. Boxes Progress. Bars View. Model to Data Access
Tip: If you’re writing Silverlight, figure out your async solution early.
Network traffic in Silverlight • It has to be async. • If it isn’t, the UI thread locks…forever.
My initial client-side architecture.
My architecture after Async WCF beat me up and ate my lunch.
Async Kills • Your Repository methods can’t return populated objects must return void • Exception handling is hard § Work happens on a different thread § Exceptions can’t “bubble up” the stack • You could have your *. xaml. cs handle the callbacks § Ugly § Violates “separation of concerns” § Not very testable
Longer discussion of Silverlight async • http: //blog. benday. com/archive/2010/12/24/23300. aspx
Our “To Do” list • Architect the Silverlight Async solution • Re-usable fields § Values, Visibility, and Validation • List-based fields § Combo. Box and List. Box • • Message. Boxes Progress. Bars View. Model to Data Access
Primitive Obsession in your View. Model.
Primitive Obsession • James Shore’s “Primitive Obsession” § Too many plain scalar values § Phone number isn’t really just a string § http: //www. jamesshore. com/Blog/ • Validation in the get / set properties is ok but is phone number validation really the responsibility of the Person class?
Coarse-Grained vs. Fine-Grained Object Model • James Shore blog entry talks about Responsibilities § Fine-grained = More object-oriented § Data and properties are split into actual responsibilities • I’m concerned about § Responsibilities § Code Duplication § Simplicity
View. Model. Field<T> • Provides common functionality for a property on a View. Model
With & Without View. Model. Field<T>
Are your View. Model properties Coarse or Fine? • Fine-grained gives you room to grow • View. Model. Field<T> • Create custom controls that know how to talk to your View. Model. Fields § Simplified binding expressions • Add features later § Field validation later § Security
Demo VIEWMODELFIELD<T>
Demo COMBOBOX & LISTBOX
Demo MESSAGE BOXES
Demo PROGRESS BARS
Our “To Do” list • Architect the Silverlight Async solution • Re-usable fields § Values, Visibility, and Validation • List-based fields § Combo. Box and List. Box • • Message. Boxes Progress. Bars View. Model to Data Access
Focus your testing on stuff that tends to be buggy.
Calls to data access are buggy. • The goal: Data access should take/return Model objects. • Databases § ADO. NET objects don’t look like your Model § Make the db call, convert the data to Models § Take the Model, convert it to a db call • WCF Services § Service Reference classes *are not* your model § Make a WCF call, convert the data to Models § Take the Model, make a WCF call • This stuff is always buggy.
Repository & Adapter Patterns are your friend
What is Repository?
The Repository Pattern • “Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects. ” § http: //martinfowler. com/eaa. Catalog/repository. html • Encapsulates the logic of getting things saved and retrieved
Synchronous Repository
Synchronous SQL Server & WCF
A Big Picture
What is Adapter?
Adapter Pattern • “…converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. ” • from “Head First Design Patterns” by Elisabeth & Eric Freeman
My version of Adapter Pattern • Take object of Type A and convert it in to object of Type B
Why are these patterns your friend? • Help focus your mind • Better design • Help contain bugs § These conversions to/from will be buggy • Help localize change § Service endpoint designs will change often • Unit test the conversions separately § (Remember it’s a “unit” test. )
Keep the Adapt separated from the Retrieve • Two classes § Repository knows how to talk to the WCF service § Adapter knows how to turn the Service Reference types into Models • Single Responsibility Principle (SRP)
demo REPOSITORY & ADAPTER
Our “To Do” list • Architect the Silverlight Async solution • Re-usable fields § Values, Visibility, and Validation • List-based fields § Combo. Box and List. Box • • Message. Boxes Progress. Bars View. Model to Data Access
No shortcuts: Keep your View. Models & Models separate.
No shortcuts: Keep your View. Models & Models separate. • It will be tempting to have your Repository/Adapter layer create View. Models § (Don’t. ) • There’s a reason why it’s called Model-View. Model
Why keep Model and View. Model separated? • View. Model is a user interface design • Model is the state of your application § aka. “Domain Model” pattern • View. Model advocates for the UI § 1 -to-1 between a View. Model and a *. xaml file § Might reference multiple Models • Don’t have the View. Model fields directly update the Model.
It’s all about the Cancel button. • If you’re “two way” data bound, How do you undo?
Cancel: View. Model wraps Model • View. Model populates itself from the Model • User edits the screen, View. Model gets updated • Model doesn’t get changed until Save button is clicked. • Model is The Boss.
demo VIEWMODEL TO MODEL ADAPTER
Summary: Our “To Do” list • Architect the Silverlight Async solution • Re-usable fields § Values, Visibility, and Validation • List-based fields § Combo. Box and List. Box • • Message. Boxes Progress. Bars View. Model to Data Access
Thank you. blog. benday. com | www. benday. com | benday@benday. com
Other Resources • http: //tinyurl. com/3 d 2 soe 8
Other Resources • http: //tinyurl. com/ln 248 h
Other Resources • http: //tinyurl. com/3 pf 8 vxd
- Slides: 62