Table Of ContentCYAN YELLOW
MAGENTA BLACK
PANTONE 123 C
BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN WPF
Companion
eBook
Available
Pro WPF and Silverlight MVVM
Pro
WPF and Silverlight are unlike any other user interface technologies. They have
been built to a new paradigm that—if harnessed correctly—can yield unprec-
edented power and performance. This book shows you how to control that W Pro
power to produce clean, testable, maintainable code.
Design and development experts now recognize that any non-trivial WPF P
or Silverlight application needs to be designed around the Model-View-
ViewModel (MVVM) design pattern in order to unlock the technology’s full F
Gary McLean Hall data-binding potential. But many coders still rely solely on programmatic WPF Silverlight
interaction between controls, because it's been hard for many to trust the WPF
data-binding technologies. a and
n
Pro WPF and Silverlight MVVM shows you why that trust has been earned.
d
Inside, discover how to create fast, scalable, and maintainable applications
using Silverlight and WPF. With this book, you’ll learn how to:
S
MVVM
• Split view and model code apart to achieve a separation of concerns
i
• Implement a ViewModel layer that mediates between the view and model
l
• Apply automated unit tests to verify the quality of production code v
• Validate user input with a variety of techniques
e
• Integrate a data access layer for persistently storing objects
r
Effective Application Development with
l
i
THE APRESS ROADMAP g
Pro Model-View-ViewModel
WPF in C# 2010
h
Pro
ASP.NET MVC t
Introducing Framework
Silverlight 4 M
Pro
Silverlight 4 in C#
Companion eBook
Harness the power of WPF and Silverlight to
Pro V
WPF and produce clean, testable, maintainable code
Silverlight MVVM V
M
See last page for details
on $10 eBook version
Gary McLean Hall
SOURCE CODE ONLINE
www.apress.com ISBN 978-1-4302-3162-2
H
54999
a
l
l
Shelve in:
.NET
User level:
9 781430 231622
Intermediate–Advanced
this print for content only—size & color not accurate 7.5 x 9.25 spine = 0.75" 272 page count 360 PPI
>
m
o
ok.c
o
b
e
w
o
w
w.
w
w
<
ok
o
B
e
w!
o
W
m
o
d fr
a
o
nl
w
o
D
Pro WPF and
Silverlight MVVM
Effective Application Development with
Model-View-ViewModel
■ ■ ■
Gary McLean Hall
i
Pro WPF and Silverlight MVVM: Effective Application Development with Model-View-ViewModel
Copyright © 2010 by Gary McLean Hall
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-3162-2
ISBN-13 (electronic): 978-1-4302-3163-9
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol
with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of
the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject
to proprietary rights.
President and Publisher: Paul Manning
Lead Editor: Jonathan Hassell
Technical Reviewer: Nathan Kannan
Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick,
Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank
Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Coordinating Editor: Debra Kelly
Copy Editors: Mary Behr and Sharon Terdeman
Compositor: MacPS, LLC
Indexer: BIM Indexing & Proofreading Services
Artist: April Milne
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
orders-ny@springer-sbm.com, or visit www.springeronline.com.
For information on translations, please e-mail rights@apress.com, or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our
Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales.
The information in this book is distributed on an “as is” basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have
any liability to any person or entity with respect to any loss or damage caused or alleged to be caused
directly or indirectly by the information contained in this work.
The source code for this book is available to readers at www.apress.com.
ii
For my wife, Victoria
iii
Contents at a Glance
■Contents ................................................................................................................ v(cid:1)
■About the Author ................................................................................................... x(cid:1)
■About the Technical Reviewer .............................................................................. xi(cid:1)
■Acknowledgments ............................................................................................... xii(cid:1)
■Introduction ....................................................................................................... xiii
■Chapter 1: Overview of WPF and Silverlight ......................................................... 1(cid:1)
■Chapter 2: DataBinding ....................................................................................... 21(cid:1)
■Chapter 3: Model-View Separation ..................................................................... 55(cid:1)
■Chapter 4: The ViewModel .................................................................................. 81(cid:1)
■Chapter 5: Events and Commands .................................................................... 111(cid:1)
■Chapter 6: Validation ......................................................................................... 129(cid:1)
■Chapter 7: Unit Testing ...................................................................................... 145(cid:1)
■Chapter 8: Data Access Layer ........................................................................... 163(cid:1)
■Chapter 9: Application Support ......................................................................... 185(cid:1)
■Chapter 10: Sample Application ........................................................................ 205(cid:1)
■Index ................................................................................................................. 243(cid:1)
iv
Contents
■Contents at a Glance ............................................................................................ iv(cid:1)
■About the Author ................................................................................................... x(cid:1)
■About the Technical Reviewer .............................................................................. xi(cid:1)
■Acknowledgments ............................................................................................... xii(cid:1)
■Introduction ........................................................................................................ xiii
■Chapter 1: Overview of WPF and Silverlight ......................................................... 1(cid:1)
WPF and Silverlight ......................................................................................................... 1(cid:1)
What Is WPF? ......................................................................................................................................... 1(cid:1)
What Is Silverlight? ................................................................................................................................ 5(cid:1)
Differences Between WPF and Silverlight .............................................................................................. 7(cid:1)
Multi-Target Platforms ........................................................................................................................... 9(cid:1)
XAML ............................................................................................................................. 13(cid:1)
Declarative User Interfaces .................................................................................................................. 13(cid:1)
XAML Elements .................................................................................................................................... 14(cid:1)
XAML Features ..................................................................................................................................... 16(cid:1)
User Experience vs. User Interface ............................................................................... 18(cid:1)
Summary ....................................................................................................................... 19(cid:1)
■Chapter 2: DataBinding ....................................................................................... 21(cid:1)
The Power of DataBinding with XAML .......................................................................... 21(cid:1)
Dependency Objects ............................................................................................................................ 21(cid:1)
Dependency Properties ........................................................................................................................ 23(cid:1)
Binding Sources ................................................................................................................................... 26(cid:1)
v
■ CONTENTS
Binding Modes ..................................................................................................................................... 28(cid:1)
The DataContext ................................................................................................................................... 30(cid:1)
Advanced DataBinding .................................................................................................. 31(cid:1)
Binding Parameters ............................................................................................................................. 31(cid:1)
Data Conversion ................................................................................................................................... 35(cid:1)
ObjectDataProvider .............................................................................................................................. 42(cid:1)
Debugging DataBindings ...................................................................................................................... 46(cid:1)
Templating ........................................................................................................................................... 49(cid:1)
Summary ....................................................................................................................... 53(cid:1)
■Chapter 3: Model-View Separation ..................................................................... 55(cid:1)
Separation of Concerns ................................................................................................. 55(cid:1)
Dependencies ...................................................................................................................................... 55(cid:1)
MVVM Alternatives ............................................................................................................................... 59(cid:1)
The Model ..................................................................................................................... 66(cid:1)
Encapsulation ....................................................................................................................................... 66(cid:1)
Don’t Repeat Yourself (DRY) ................................................................................................................. 67(cid:1)
You Ain’t Gonna Need It (YAGNI) .......................................................................................................... 69(cid:1)
The Law of Demeter ............................................................................................................................. 70(cid:1)
Test-Driven Development .................................................................................................................... 71(cid:1)
The View ....................................................................................................................... 73(cid:1)
Data Representation ............................................................................................................................ 73(cid:1)
User Input ............................................................................................................................................. 73(cid:1)
Data Binding ......................................................................................................................................... 74(cid:1)
Separating Model from View ......................................................................................... 75(cid:1)
The Mediator Pattern ........................................................................................................................... 75(cid:1)
Other Model-View-X Implementations ................................................................................................. 77(cid:1)
You Gotta Keep ’Em Separated ............................................................................................................ 78(cid:1)
Summary ....................................................................................................................... 79(cid:1)
■Chapter 4: The ViewModel .................................................................................. 81(cid:1)
First ViewModel ............................................................................................................ 81(cid:1)
vi
■ CONTENTS
The MVVM Template Project ................................................................................................................ 81(cid:1)
.NET Framework Interfaces and Classes ...................................................................... 90(cid:1)
Observer Pattern .................................................................................................................................. 90(cid:1)
INotifyPropertyChanged Interface ........................................................................................................ 91(cid:1)
Observable Collections ......................................................................................................................... 92(cid:1)
CollectionViewSource .......................................................................................................................... 94(cid:1)
Constructing ViewModels .................................................................................................................... 96(cid:1)
Handling Concurrency ................................................................................................. 100(cid:1)
Threading ........................................................................................................................................... 100(cid:1)
Threading Problems In WPF and Silverlight ....................................................................................... 104(cid:1)
Updating the UI .................................................................................................................................. 107(cid:1)
Summary ..................................................................................................................... 110(cid:1)
■Chapter 5: Events and Commands .................................................................... 111(cid:1)
Events ......................................................................................................................... 111(cid:1)
Events in .NET .................................................................................................................................... 111(cid:1)
Events in WPF and Silverlight ............................................................................................................ 112(cid:1)
Commands .................................................................................................................. 115(cid:1)
Command Pattern .............................................................................................................................. 116(cid:1)
The RelayCommand ........................................................................................................................... 119(cid:1)
Attached Command Behavior ............................................................................................................. 126(cid:1)
Avoiding Events Using Dependency Injection .................................................................................... 126(cid:1)
Summary ..................................................................................................................... 128(cid:1)
■Chapter 6: Validation ......................................................................................... 129(cid:1)
The Validation Process ................................................................................................ 130(cid:1)
Binding Validation Rules ............................................................................................. 131(cid:1)
ValidationRule Class ........................................................................................................................... 131(cid:1)
Exceptions for Validation .................................................................................................................... 133(cid:1)
A Validation Framework ..................................................................................................................... 135(cid:1)
IDataErrorInfo Interface ...................................................................................................................... 135(cid:1)
DataErrorValidationRule Class ........................................................................................................... 139(cid:1)
vii
■ CONTENTS
Validation in Silverlight ............................................................................................... 139(cid:1)
Visually Formatting Validation Errors .......................................................................... 143(cid:1)
Validation.ErrorTemplate Attached Property ...................................................................................... 143(cid:1)
Validation.HasError Attached Property ............................................................................................... 143(cid:1)
Summary ..................................................................................................................... 144(cid:1)
■Chapter 7: Unit Testing ...................................................................................... 145(cid:1)
The Importance of Testing .......................................................................................... 145(cid:1)
Traditional Testing ............................................................................................................................. 145(cid:1)
What Is Unit Testing? ......................................................................................................................... 149(cid:1)
Why Unit Test? ................................................................................................................................... 151(cid:1)
How to Unit Test .......................................................................................................... 152(cid:1)
Unit Testing with Visual Studio 2010 ................................................................................................. 152(cid:1)
Writing Tests ...................................................................................................................................... 158(cid:1)
Summary ..................................................................................................................... 162(cid:1)
■Chapter 8: Data Access Layer ........................................................................... 163(cid:1)
Object-Relational Dichotomy ...................................................................................... 163(cid:1)
DAL Implementations .................................................................................................. 172(cid:1)
Manual Implementation ..................................................................................................................... 172(cid:1)
Third-Party Implementations ............................................................................................................. 176(cid:1)
Supporting Patterns .................................................................................................... 180(cid:1)
The Repository Pattern ....................................................................................................................... 180(cid:1)
Unit of Work ....................................................................................................................................... 181(cid:1)
Creating the Data Schema .......................................................................................... 182(cid:1)
Generate, Not Create .......................................................................................................................... 182(cid:1)
Summary ..................................................................................................................... 183(cid:1)
■Chapter 9: Application Support ......................................................................... 185(cid:1)
Serialization ................................................................................................................ 186(cid:1)
Serializing POCOs ............................................................................................................................... 186(cid:1)
Extensibility ................................................................................................................. 192(cid:1)
viii