Introduction
The Windows Presentation Framework (WPF), Silverlight, and Windows Phone 7 are the latest technologies for building flexible user interfaces (UI) for applications built with Microsoft technology. All three rely on the XAML markup language to describe UI elements and layout, and you can program applications for all three platforms with the most common of Microsoft .NET Framework languages: Visual C# or Visual Basic .NET. If you are a .NET developer planning to create a new Line of Business (LOB) application using the .NET Framework, you should consider adopting one of these technologies as your UI technology. At the same time, as you start planning to build an application based on one of these technologies, you should also seriously consider learning and applying the Model View ViewModel (MVVM) presentation pattern, a design pattern created specifically for these technologies.
And thats what this book is about. You might be wondering, Why another book on WPF? Or, if you have already looked at the Table of Contents, you might be thinking, Why another book about layering and design patterns?
To answer those questions, let me start by saying that over the years, I have noticed that what developers ask for the most is not the Bible of patterns or the Bible of how to layer an application; instead, they want a simple, straightforward book that guides them through the development criteria for a real-world, yet simple, application that uses and explains patternsbut that is also reusable in future projects as a template for other applications.
WPF and Silverlight are young technologies, and the percentage of developers moving to this new way of designing the UI is still small. There are several reasons for this. First, the learning curve is relatively high. If youre used to Windows Forms, Java Swing, or Delphi, the way you design and structure an application using XAML and WPF is significantly differentin fact, I would call it revolutionary.
In the past, I have used well-known patterns to build applications, including the Model View Presenter pattern with Windows Forms applications, and the Model View Controller pattern with ASP.NET applications. But with WPF, these two approaches are now obsolete, because they cant take advantage of the powerful engine provided by XAML. Of course, you can still take advantage of the binding engine of WPF using the Model View Presenter pattern, but the effort required is usually too large. Fortunately, MVVM provides an alternative.
Microsoft, in collaboration with some architects, has revised the original Presentation Model that was proposed years ago by Martin Fowler. This revision (named the Model View ViewModel pattern) is the perfect approach for WPF and Silverlight because, well, it was designed specifically for them! Unfortunately, like XAML, MVVM is a relatively new technology, so at the moment, there isnt a lot of information about implementing it. There are a few bloggers trying the MVVM approach and blogging about it; others are involved in building MVVM-specific toolkits. But nearly everything is still experimental, and there are few truly concrete examples.
Therein lies the rationale for a book about building a LOB application using MVVM. As you proceed through this book, you will see examples that show how to build a straightforward Customer Relationship Management (CRM) program with WPF 4, Silverlight 4, and the MVVM pattern. The book guides you through the entire architectural process, illustrating the correct approach to using MVVM. Youll also use some other new technologies delivered with Microsoft .NET 4, such as Managed Extensions, Windows Workflow Foundation 4, and of course, the Entity Framework.
First, you are introduced to the tools. Next, you move ahead to build the CRM, starting with the domain model, applying a simple technique to persist the data in a relational database by using two of the most popular Object-Relational Mapper (O/RM) systems available for .NET: the Entity Framework and NHibernate. Then, see how to make everything more flexible using the MEF framework.
Following that, you learn to apply business logic and data validation to this model in a way that fulfills the requirements of the MVVM pattern. In this phase, you also look at Windows Workflow Foundation (WF) 4.0, the powerful, new workflow engine by Microsoft, and study the steps required to build a simple workflow engine.
The remaining chapters all focus on MVVM. There are four major concepts that you must learn to use MVVM correctly: commanding , the template , the binding engine , and how to orchestrate everything together. At the end of this process, you will have visited all the layers required to complete a classic LOB application, but more importantly, you will be able to recycle the parts described here as a template for building future applications. Of course, there are some differences between WPF and Silverlight, so this book will try to cover those gaps where possible.
Finally, you will take a brief tour of the MVVM toolkits that are already available, such as PRISM, a composite application framework for WPF and Silverlight. This will help you to determine when and how you should use each as part of the process of building a small and flexible MVVM framework.
Overall, the key goal of this book is to provide a complete step-by-step guide for using WPF/Silverlight in conjunction with MVVM for creating generic code that you will be able to use and reuse in the future.