Chapter 1: What is Microsoft Prism 4?

Application Architecture

When most developers see the words, "Application Architecture," they either cringe or their eyes light up with anticipation. This love/hate relationship is due to the complexities involved when developing well-designed solutions.

As developers we tend see a broad range of applications ranging from well-designed architectures to solutions that seem to be patched together with no regard to good programming practices whatsoever. In a lot of cases we're stuck with the current architecture because that's what was designed before we even came into the picture. The arguments for refactoring these projects are strong, but not always practical.

We are going to talk about cases where we can refactor or build solutions from scratch; that's where application architecture comes into play. Most developers find the time to read at least one book on this subject and if you are interested in Prism 4, my assumption is that you are aware of the principles and design patterns that are used to define a well-designed solution. So, where does Prism 4 fit into this equation?

Microsoft Prism 4

Microsoft Prism 4 is a framework designed by the Patterns and Practices team at Microsoft. It is designed to serve as a template to help developers build solutions. It’s designed to be used with Extensible Application Markup Language (XAML) solutions that depend on the .NET framework. These solution types include Microsoft WPF, Silverlight, and Windows Phone 7 or 8 at this time. The Prism 4 framework has a library for each of these three solution types.

Note: In this book we are going to use WPF. WPF is the most feature-rich of the three libraries with Silverlight and Phone 7 or 8, exposing less features respectively.

Why Use Prism 4?

Microsoft designed Prism 4 to be used with enterprise solutions that use WPF, Silverlight, or Windows Phone.

After using Prism 4 in numerous solutions, I tend to take a broader view. Why use Prism 4 in just enterprise solutions? If the goal is to promote good programming practices, why relegate it to only large, enterprise-wide solutions? In your experience, how often has a seemingly simple project grown into a massive project? How often have you had to deal with shortcomings in these projects when it was necessary to modify or add features?

If Prism 4 promotes good programming practices with enterprise solutions, why should we relegate smaller projects to anything but the same good programming practices?

I wish that I could tell you that Prism 4 is the cure-all for developing well architected XAML-based solutions, but alas, that's not the case. Prism 4 will not automatically enforce good programming practices; it's necessary for us as developers to understand DI, OOP, SOLID, DRY, and other good practices to effectively use Prism 4. What you will find is that as you use Prism 4, you'll start to see how it can be used to reduce the amount of work needed to produce well-designed solutions.

In this book I'll explain some of the best practices for creating well architected solutions and we'll then correlate how Prism 4 allows us to implement each practice.

Microsoft Prism 4 Features

Prism 4 is actually composed of a number of features that in most cases can be used independently or with other features of the framework. You can, for instance, use Dependency Injection (DI), and exclude all of the other Prism 4 features. But Prism 4 is most effective when all of its features are used in conjunction with one another.

Here is a list of Prism 4 features:

  • Bootstrapper class
  • Dependency Injection Container (Unity, Managed Extensibility Framework (MEF), etc.).
  • Shell form
  • Regions
  • Modules
  • Navigation
  • Commanding
  • Event aggregation

Other patterns and applications that will be used:

  • The Model-View-ViewModel (MVVM) design pattern.
  • The NUnit (Unit Test Application).

The companion demo solutions for this book will use most of the features in this list to create three simple Prism 4 solutions. The Hello World solution will walk you through building a barebones WPF/Prism 4 solution. The Virtual Calculator will work with a more complex solution that is used to add and subtract two numbers. The Event Aggregation - View Based Navigation solution shows an example of using Prism 4 to communicate between modules and to show views in a loosely coupled manner. We'll use these three solutions to show Prism 4 integration with WPF solutions.

Any one of Prism 4's features is a strong motivation to use the framework. As we progress in this book, I show you why and how to combine these features to create reliable XAML-based solutions.