Prism provides guidance designed to help you more easily design and build rich, flexible, and easy-to-maintain Windows Presentation Foundation (WPF) desktop applications, Silverlight Rich Internet Applications (RIAs), and Windows Phone 7 applications. Using design patterns that embody important architectural design principles, such as separation of concerns and loose coupling, Prism helps you to design and build applications using loosely coupled components that can evolve independently but that can be easily and seamlessly integrated into the overall application. These types of applications are known as composite applications.
|Prism was the code name for the guidance formally known as the Composite Application Guidance for WPF and Silverlight. For brevity and conciseness, and due to customer demand, this guidance is now referred to simply as Prism.|
Prism is intended for software developers building WPF or Silverlight applications that typically feature multiple screens, rich user interaction and data visualization, and that embody significant presentation and business logic. These applications typically interact with multiple back-end systems and services and, using a layered architecture, may be physically deployed across multiple tiers. It is expected that the application will evolve significantly over its lifetime in response to new requirements and business opportunities. In short, these applications are "built to last" and "built for change." Applications that do not demand these characteristics may not benefit from using Prism.
Prism includes reference implementations, QuickStarts, reusable library code (the Prism Library), and extensive documentation. This version of Prism targets the Microsoft .NET Framework 4.0 and Silverlight 4 and includes new guidance around the Model-View-ViewModel (MVVM) pattern, navigation, and the Managed Extensibility Framework (MEF). Because Prism is built on the .NET Framework 4.0 (which includes WPF) and Silverlight 4, familiarity with these technologies is useful for evaluating and adopting Prism.
It should be noted that while Prism is not difficult to learn, developers must be ready and willing to embrace patterns and practices that may be new to them. Management understanding and commitment is crucial, and the project deadline must accommodate an investment of time up front for learning these patterns and practices.
Why Use Prism?
Designing and building rich WPF or Silverlight client applications that are flexible and easy to maintain can be challenging. This section describes some of the common challenges you might encounter when building WPF or Silverlight client applications, and describes how Prism helps you to address those challenges.
Client Application Development Challenges
Typically, developers of client applications face quite a few challenges. Application requirements can change over time. New business opportunities and challenges may present themselves, new technologies may become available, or even ongoing customer feedback during the development cycle may significantly affect the requirements of the application. Therefore, it is important to build the application so that it is flexible and can be easily modified or extended over time. Designing for this type of flexibility can be hard to accomplish. It requires an architecture that allows individual parts of the application to be independently developed and tested and that can be modified or updated later, in isolation, without affecting the rest of the application.
Most enterprise applications are sufficiently complex that they require more than one developer, maybe even a large team of developers that includes user interface (UI) designers and localizers in addition to developers. It can be a significant challenge to decide how to design the application so that multiple developers or subteams can work effectively on different pieces of the application independently, yet ensuring that the pieces come together seamlessly when integrated into the application.
Designing and building applications in a monolithic style can lead to an application that is very difficult and inefficient to maintain. In this case, "monolithic" refers to an application in which the components are very tightly coupled and there is no clear separation between them. Typically, applications designed and built this way suffer from problems that make the developer's life hard. It is difficult to add new features to the system or replace existing features, it is difficult to resolve bugs without breaking other portions of the system, and it is difficult to test and deploy. Also, it impacts the ability of developers and designers to work efficiently together.
The Composite Approach
An effective remedy for these challenges is to partition the application into a number of discrete, loosely coupled, semi-independent components that can then be easily integrated together into an application "shell" to form a coherent solution. Applications designed and built this way are often known as composite applications.
Composite applications provide many benefits, including the following:
- They allow modules to be individually developed, tested, and deployed by different individuals or subteams; they also allow them to be modified or extended with new functionality more easily, thereby allowing the application to be more easily extended and maintained. Note that even single-person projects experience benefits in creating more testable and maintainable applications using the composite approach.
- They provide a common shell composed of UI components contributed from various modules that interact in a loosely coupled way. This reduces the contention that arises from multiple developers adding new functionality to the UI, and it promotes a common appearance.
- They promote reuse and a clean separation of concerns between the application's horizontal capabilities, such as logging and authentication, and the vertical capabilities, such as business functionality that is specific to your application. This also allows you to more easily manage the dependencies and interactions between application components.
- They help maintain a separation of roles by allowing different individuals or subteams to focus on a specific task or piece of functionality according to their focus or expertise. In particular, it provides a cleaner separation between the UI and the business logic of the application—this means the UI designer can focus on creating a richer user experience.
Composite applications are highly suited to a range of client application scenarios. For example, a composite application is ideal for creating a rich end-user experience over disparate back-end systems. The following illustration shows an example of this type of a composite application.
In this type of application, the user can be presented with a rich and flexible user experience that provides a task-oriented focus over functionality that spans multiple back-end systems, services, and data stores, where each is represented by one or more dedicated modules. The clean separation between the application logic and the UI allows the application to provide a consistent and differentiated appearance across all constituent modules.
Additionally, a composite application can be useful when there are independently evolving components in the UI that heavily integrate with each other and that are often maintained by separate teams. The following illustration shows a screen shot of this type of application. Each of the areas highlighted represent independent components that are composed into the UI.
In this case, the composite application allows the UI to be dynamic composed. This delivers a flexible user experience. For example, it can allow new functionality to be dynamically added to the application at run time, which enables rich end-user customization and extensibility.
Challenges Not Addressed by Prism
Although Prism helps you to address many of the challenges you might face when building WPF or Silverlight applications, there are many other challenges that you might face, depending on your application scenario and requirements. For example, Prism does not directly address the following topics:
- Occasional connectivity and data synchronization
- Service and messaging infrastructure design
- Authentication and authorization
- Application performance
- Application versioning
- Error handling and fault tolerance
Getting Started with Prism
Prism assumes you have hands-on experience with WPF or Silverlight. There are a few important concepts that Prism uses heavily, and you should become familiar with them. They include the following:
- XAML (Extensible Application Markup Language). The language to declaratively define and initialize the user interface in WPF and Silverlight applications.
- Data binding. This is how UI elements are connected to components and data in WPF and Silverlight.
- Resources. These are how styles, data templates, and control templates are created and managed in WPF and Silverlight.
- Commands. These are how user gestures and input are connected to controls.
- User controls. These are components that provide custom behavior or custom appearance.
- Dependency properties. These are extensions to the common language runtime (CLR) property system to enable property setting and monitoring in support of data binding, routed commands, and events.
- Behaviors. Behaviors are objects that encapsulate interactive functionality that can be easily applied to controls in the user interface.
This section describes how to install Prism. It involves the following three steps:
- Install system requirements.
- Extract the Prism source code, binaries, and documentation.
- Register the Prism binaries.
Step 1: Install System Requirements
Prism was designed to run on the Microsoft Windows 7, Windows Vista, or Windows Server 2008 operating system. This version has been smoke tested on Windows XP Professional and Windows Server 2003, but it has not been exhaustively tested. WPF applications built using this guidance require the .NET Framework 4.0, and Silverlight applications require Silverlight 4.
Before you can use the Prism Library, the following must be installed:
- Microsoft .NET Framework 4.0 (installed with Visual Studio 2010)
- Microsoft Visual Studio 2010 Professional, Premium, or Ultimate editions
| Visual Studio 2010 Express Edition can be used to develop Prism applications using the Prism Library.|
If you are developing Silverlight applications, the following must be installed:
- Microsoft Silverlight 4 Tools for Visual Studio 2010 (required for Silverlight development; includes the developer Silverlight runtime)
| Although the Silverlight Tools for Visual Studio 2010 are not required, it is recommended for all WPF and Silverlight developers to download and use the latest version of the Silverlight Tools for Visual Studio 2010. |
The WPF and Silverlight Designer for Visual Studio is updated together with the Silverlight developer runtime and software development kit (SDK), which are included in the download. These updates come in the form of new features and bug fixes.
Optionally, you should consider also installing the following:
- Microsoft Expression Blend 4. A professional design tool for creating compelling user experiences and applications for WPF and Silverlight.
- Windows Phone Developer Tools SDK. For Windows Phone 7 development.
| For more information about using Prism on Windows Phone 7, see the Windows Phone 7 Developer Guide community site on CodePlex (http://wp7guide.codeplex.com/.)|
Step 2: Extract the Prism Source Code, Binaries, and Documentation
To install the Prism assets, right-click the Prismv4.exe file, and then click Run as administrator. This will extract the source code, binaries, and documentation into the folder of your choice.
Step 3: Register the Prism Binaries
Registering the Prism Library with Visual Studio is not required, but doing so simplifies the task of referencing the Prism Library assemblies in your projects. If you elect to register the binaries, they will be listed in the Visual Studio Add References dialog box when adding a reference. If you elect to not register the binaries, you will need to manually set a file reference to the Prism Library binaries in your projects. The Prism Library signed assemblies will be placed in the following folders:
To register the Prism Library binaries, launch the RegisterPrismBinaries.bat batch file located in the folder where you extracted Prism. This batch file creates a temporary .reg file with the information required to register the Desktop, Silverlight, and Phone folders containing the binaries and uses it to update the registry. Because updating the registry is a privileged operation, a User Account Control (UAC) prompt will appear if you do not have elevated privileges. For additional information about UAC, see "What is User Account Control."
|At most, one copy of the binaries can be registered using the script; if multiple copies of the Prism Library are registered, only the binaries for the last registered copy will be available in Visual Studio.|
What's New in This Release?
This release of Prism has been updated to target WPF 4.0 and Silverlight 4, and it contains several areas of new and significantly updated guidance, including the following:
- Managed Extensibility Framework (MEF). Prism now includes guidance on using MEF to manage dependencies and to maintain loose coupling between components. The use of MEF is explored in Chapter 3, "Managing Dependencies Between Components."
- The Model-View-View Model (MVVM) pattern. Previous versions of Prism have provided extensive guidance on the use of separated presentation patterns. This guidance has now been updated to focus on the MVVM pattern. Chapter 5 provides an overview of the MVVM pattern and describes how to implement it. Chapter 6 covers more advanced MVVM scenarios.
- Navigation. Prism now provides guidance on implementing navigation within your WPF or Silverlight application. This guidance covers both state-based navigation, which is used when you update the visual state of a single view, and view-switching navigation, which is used when navigating between views. Both approaches are covered in depth in Chapter 8, "Navigation."
Prism now also provides signed binary versions of the Prism Library assemblies. These assemblies are provided as a convenience for developers who want to use the Prism Library in their own applications without modification. In addition, all Visual Studio projects (for the Prism Library as well as the reference implementations and QuickStarts) have also been migrated to use Visual Studio 2010 and Silverlight 4.
For a list of the new features, assets, and API changes, see What's New in Prism 4.0.
What's in the Box?
Prism consists of the following:
- Prism Library source code. The source code for the Prism Library assemblies, including the core Prism functionality, plus Unity and MEF extensions, which provide additional components for using Prism with the Unity Application Block (Unity) and the Managed Extensibility Framework.
- Prism binary assemblies. Signed binary versions of the Prism Library assemblies. These assemblies are located in the bin folder and are provided as a convenient way for developers to use the Prism Library. The Prism binaries can be rebuilt and can be registered with Visual Studio using the provided script files. The binary assemblies also include the Unity Application Block and the Service Locator assemblies.
- Reference implementations. Comprehensive sample applications that illustrate how Prism can be used to implement real-world application scenarios. The reference implementations are intentionally incomplete, but they illustrate how many of the patterns in Prism can work together within a single application. Prism provides two reference implementations: the Stock Trader Reference Implementation (Stock Trader RI) and the MVVM Reference Implementation (MVVM RI).
- QuickStarts. Prism includes the source code for several small, focused sample applications that illustrate the MVVM pattern, navigation, UI composition, modularity, commanding, event aggregation, and multi-targeting.
- Documentation. The Prism 4 documentation provides an overview of the goals and concepts behind Prism and detailed guidance on using each of the capabilities and design patterns provided by Prism. The next section provides a chapter-by-chapter overview of the topics covered.
Exploring the Documentation
The Prism documentation spans a wide range of topics, including an overview of common development challenges and the composite application approach, an overview of the Prism Library and the design patterns that it implements, as well as step-by-step instructions for using the Prism Library during development. The documentation is intended to appeal to a broad technical audience to help the reader to understand and use Prism within their own applications. The documentation includes the following:
- Chapter 2, "Initializing Prism Applications." This chapter discusses what needs to happen to get a modular Prism application up and running.
- Chapter 3, "Managing Dependencies Between Components." Applications based on the Prism Library rely on a dependency injection container. Although Prism has the ability to work with nearly any dependency injection container, the Prism Library provides two default options for dependency injection containers: Unity or MEF. This chapter discusses the different capabilities and what you need to think about when working with a dependency injection container.
- Chapter 4, "Modular Application Development." This chapter discusses the core concepts, key decisions, and core scenarios when you create a modular client application with Prism.
- Chapter 5, "Implementing the MVVM Pattern." Using the MVVM pattern, you separate the UI of your application and the underlying presentation and business logic into three separate classes: the view, model, and view model. This chapter discusses the core concepts behind the MVVM pattern and describes how to implement it in your application using Prism.
- Chapter 6, "Advanced MVVM Scenarios." This chapter provides guidance on implementing more advanced scenarios using the MVVM pattern, including how to implement composite commands (commands that represent a group of commands), and how to handle asynchronous web service and user interactions. This chapter also provides guidance on using a dependency injection container, such as Unity or MEF, to handle the construction and wire-up of the MVVM classes.
- Chapter 7, "Composing the User Interface". Regions are placeholders that allow a developer to specify where views will be displayed in the application's UI. In Prism, there are two approaches to displaying views in a region: view discovery and view injection. This chapter describes how to work with regions and the UI. It also includes information for UI designers to understand composite applications.
- Chapter 8, "Navigation." Navigation is the process by which the application coordinates changes to its UI as a result of the user's interaction with the application or internal application state changes. This chapter provides guidance on implementing state-based navigation, where the state of the UI in a view is updated to reflect navigation, and view-switching navigation, where a new view is created and displayed in a region.
- Chapter 9, "Communicating Between Loosely Coupled Components." This chapter discusses the various options for communicating between components in different modules, using commanding, the EventAggregator, region context, and shared services.
- Chapter 10, "Sharing Code Between Silverlight and WPF." Multi-targeted code is targeting two different platforms with largely the same code base. In this case, the technologies that this topic describes are WPF and Silverlight. This chapter helps you understand what multi-targeting is and its advantages and disadvantages. Prism provides the Project Linker tool to help you to automatically create and maintain links from a source project to a target project to share code that is common between Silverlight and WPF.
- Chapter 11, "Deploying Prism Applications." This chapter addresses deployment considerations for Prism WPF and Silverlight applications.
- Appendix A, "Glossary." This appendix provides a concise summary of the terms, concepts, design patterns and capabilities provided by Prism.
- Appendix B, "Patterns in the Prism Library." This appendix describes the software design patterns applied in the Prism Library and the Stock Trader RI. This topic primarily targets architects and developers wanting to familiarize themselves with the patterns used to address the challenges in building composite applications.
- Appendix C, "Prism Library." This appendix provides an overview of the Prism Library.
The following topics are included with the source code and online:
- Appendix D, "Upgrading from Previous Versions." This appendix discusses what you need to know if you are upgrading from previous versions of Prism.
- Appendix E, "Extending Prism." This appendix discusses how you can extend Prism modularity, behaviors, and navigation.
- Appendix F, "Reference Implementations." This appendix describes the reference implementations included with Prism. For more information, see the section "Exploring the Reference Implementations."
- Appendix G, "QuickStarts." Prism includes the source code for several QuickStarts that demonstrate key concepts. For more information, see the next section, "Exploring the QuickStarts."
- Appendix H, "Prism Hands-On Labs." The hands-on labs demonstrate building a simple composite application, step-by-step, in WPF and Silverlight. This appendix primarily targets developers who want to understand the basic concepts of the Prism Library. It also includes a deployment hands-on lab for publishing and updating a Prism WPF application with ClickOnce.
Exploring the QuickStarts
The QuickStarts are small, focused applications that illustrate specific Prism-related concepts. QuickStarts are an ideal starting point if you want to gain an understanding of a key concept and you are comfortable learning new techniques by examining source code. Prism includes the following QuickStarts:
- Modularity QuickStarts for WPF and Modularity QuickStarts for Silverlight. These QuickStarts demonstrate how to build WPF and Silverlight applications composed of modules. The modules can be statically loaded, when the shell contains a reference to the module's assembly, or dynamically loaded, when modules are dynamically discovered and loaded at run time. The QuickStarts demonstrate using the Unity container and MEF.
- Basic MVVM QuickStart and MVVM QuickStart. The Basic MVVM QuickStart demonstrates how to build a very simple application that implements the MVVM presentation pattern. The MVVM QuickStart demonstrates how to build an application that implements the MVVM presentation pattern, showing some of the more common challenges that developers can face, such as validation, UI interactions, and data templates.
- UI Composition QuickStart. This QuickStart demonstrates how to build WPF and Silverlight UIs composed of different views that are dynamically loaded into regions and that interact with each other in a decoupled way. It illustrates how to use both the view discovery and view injection approaches for UI composition.
- State-Based Navigation QuickStart. This QuickStart demonstrates an approach to define the navigation of a simple application. The approach used in this QuickStart uses the Silverlight Visual State Manager (VSM) to define the different states that the application has and defines animations for both the states and the transitions between states.
- View-Switching Navigation QuickStart. This QuickStart demonstrates how to use the Prism Region Navigation API. The QuickStart shows multiple navigation scenarios, including navigating to a view in a region, navigating to a view in a region contained in another view (nested navigation), navigation journal support, just-in-time view creation, passing contextual information when navigating to a view, views and view models participating in navigation, and using navigation as part of an application built through modularity and UI composition.
- Commanding QuickStart. This QuickStart demonstrates how to build a WPF or Silverlight UI that uses commands provided by the Prism Library to handle UI actions in a decoupled way.
- Event Aggregation QuickStart. This QuickStart demonstrates how to build a WPF or Silverlight application that uses the Event Aggregator service. This service enables you to establish loosely coupled communications between components in your application.
- Multi-Targeting QuickStart. This QuickStart demonstrates the structure of a project created to multi-target WPF and Silverlight environments. It provides a desktop experience (on WPF) and a Rich Internet Application (RIA) experience (on Silverlight).
Exploring the Reference Implementations
The Prism reference implementations are example applications based on real-world challenges customers are facing. When you look at these applications, look at them as a reference for building applications with the Prism Library. The Prism reference implementations include:
- Stock Trader Reference Implementation. The Stock Trader RI is a composite application that demonstrates an implementation of the baseline architecture using the Prism Library.
- MVVM Reference Implementation. The MVVM RI demonstrates complex challenges that developers face when creating applications using the MVVM pattern.
Upgrading from Earlier Releases
If you are upgrading from the previous releases of this guidance, you should review "Upgrading from Previous Releases" in Appendix D to understand the major differences between this and the previous releases.
If you are upgrading from the Composite UI Application Block (which targeted WinForms) to the Prism Library, you should review "Upgrading from the Composite UI Application Block" in Appendix D so that you understand how the concepts in the Composite UI Application Block map to the Prism Library.
An Overview of Prism
Prism Design Goals
Prism was designed to help you design and build rich, flexible, and easy-to-maintain WPF and Silverlight applications. The Prism Library implements design patterns that embody important architectural design principles, such as separation of concerns and loose coupling. Using the design patterns and capabilities provided by the Prism Library, you can design and build applications using loosely coupled components that can evolve independently but that can be easily and seamlessly integrated into the overall application.
Prism is designed around the core architectural design principles of separation of concerns and loose coupling. This allows Prism to provide many benefits, including the following:
- Reuse. Prism promotes reuse by allowing components and services to be easily developed, tested and integrated into one or more applications. Reuse can be achieved at the component level through the reuse of unit-tested components that can be easily discovered and integrated at run time through dependency injection, and at the application level through the use of modules that encapsulate application-level capabilities that can be reused across applications.
- Extensibility. Prism helps to create applications that are easy to extend by managing component dependencies, allowing components to be more easily integrated or replaced with alternative implementations at run time, and by providing the ability to decompose an application into modules that can be independently updated and deployed. Many of the components in the Prism Library itself can also be extended or replaced.
- Flexibility. Prism helps to create flexible applications by allowing them to be more easily updated as new capabilities are developed and integrated. Prism also allows WPF and Silverlight applications to be developed using common services and components, allowing the application to be deployed and consumed in the most appropriate way. It also allows applications to provide different experiences based on role or configuration.
- Team Development. Prism promotes team development by allowing separate teams to develop and even deploy different parts of the application independently. Prism helps to minimize cross-team dependencies and allows teams to focus on different functional areas (such as UI design, business logic implementation, and infrastructure code development), or on different business-level functional areas (such as profile, sales, inventory, or logistics).
- Quality. Prism can help to increase the quality of applications by allowing common services and components to be fully tested and made available to the development teams. In addition, by providing fully tested implementations of common design patterns, and the guidance needed to fully leverage them, Prism allows development teams to focus on their application requirements instead of implementing and testing infrastructure code.
It is important to note that Prism was designed so that you can use any of Prism's capabilities and design patterns individually, or all together, depending on your requirements and your application scenario. Prism was designed so that it could be incrementally adopted, allowing you to use the capabilities and design patterns that make sense for your particular application without requiring major structural changes.
Finally, because software testing should be considered a first-class development activity and tightly integrated into the development process, Prism provides extensive support for various types of software testing, thereby allowing you to design and build applications that are easy to test. Prism itself was developed with testing in mind. It was developed to meet multiple strict quality gates to ensure that it meets Microsoft security standards and that it will function correctly on multiple operating systems, with multiple versions of Visual Studio, and with multiple programming languages. Unit tests were run after each check-in. In addition, the Prism library was tested against several additional quality gates, as listed in the following table.
Validates the application functionality using user scenarios to drive the test requirements. Tests can be executed manually or automated.
Application Building Exercises
Team members build applications consuming the deliverable software.
Black Box Testing
Manual acceptance tests perform from the user point of view.
Cross Browser Testing
All automated tests are run on multiple browsers.
Cross Platform Testing
All automated tests are run on multiple platforms.
All automated tests are run on multiple languages.
Measures how fast a particular aspect of a system performs under-load.
Internal Microsoft security audit standards that cover thread models, identifying attack factors and running the code though security analysis tools.
Measures stability of the system under extreme loads; specifically looking to drive out issues like memory leaks and threading issues.
White Box Testing
In-depth source code analysis validating the coding standards, structure and how it maps to the overall architecture.
The Prism Library source code includes unit and UI automation tests, as shown in the following table. You can use these as an educational resource, or you can run the tests against the Prism Library itself. This allows you to customize, re-compile, test and deploy a modified version of the Prism Library using similar quality gates as the Prism team.
UI Automation Tests
Limited range of acceptance testing; driving the application from the user perspective
Validates the implementation of a class
Prism Key Concepts
Prism provides capabilities and design patterns that may be unfamiliar to you, especially if you're new to design patterns and composite application development. This section provides a brief overview of the main concepts behind Prism and defines some of the terminology that you will see used throughout the documentation and code.
- Modules. Modules are packages of functionality that can be independently developed, tested, and (optionally) deployed. In many situations, modules are developed and maintained by separate teams. A typical Prism application is built from multiple modules. Modules can be used to represent specific business-related functionality (for example, profile management) and encapsulate all the views, services, and data models required to implement that functionality. Modules can also be used to encapsulate common application infrastructure or services (for example, logging and exception management services) that can be reused across multiple applications.
- Module catalog. In a composite application, modules must be discovered and loaded at run time by the host application. In Prism, a module catalog is used to specify which modules to are to be loaded, when they are loaded, and in what order. The module catalog is used by the ModuleManager and ModuleLoader components, which are responsible for downloading the modules if they are remote, loading the module's assemblies into the application domain, and for initializing the module. Prism allows the module catalog to be specified in different ways, including programmatically using code, declaratively using XAML, or using a configuration file. You can also implement a custom module catalog if you need to.
- Shell. The shell is the host application into which modules are loaded. The shell defines the overall layout and structure of the application, but it is typically unaware of the exact modules that it will host. It usually implements common application services and infrastructure, but most of the application's functionality and content is implemented within the modules. The shell also provides the top-level window or visual element that will then host the different UI components provided by the loaded modules.
- Views. Views are UI controls that encapsulate the UI for a particular feature or functional area of the application. Views are used in conjunction with the MVVM or Model-View-Presenter (MVP) patterns, which are used to provide a clean separation of concerns between the UI and the application's presentation logic and data. Views are used to encapsulate the UI and define user interaction behavior, thereby allowing the view to be updated or replaced independently of the underlying application functionality. Views use data binding to interact with view model and presenter classes.
- View models and presenters. View models are classes that encapsulate the application's presentation logic and state. They are part of the MVVM pattern. View models encapsulate much of the application's functionality. Presenters are similar to view models in that they encapsulate the presentation logic and state. They are used as part of the MVP pattern. Both view models and presenters define properties, commands, and events, to which controls in the view can data-bind.
- Models. Model classes encapsulate the application data and business logic. They are used as part of the MVVM or MVP patterns. Models encapsulate data and any associated validation and business rules to ensure data consistency and integrity.
- Commands. Commands are used to encapsulate application functionality in a way that allows them to be defined and tested independently of the application's UI. They can be defined as command objects or as command methods in the view model or presenter. Prism provides the DelegateCommand class and the CompositeCommand class. The latter is used to represent a collection of commands which are all invoked together.
- Regions. Regions are logical placeholders defined within the application's UI (in the shell or within views) into which views are displayed. Regions allow the layout of the application's UI to be updated without requiring changes to the application logic. Many common controls can be used as a region, allowing views to be automatically displayed within controls, such as a ContentControl, ItemsControl, ListBox, or TabControl. Views can be displayed within a region programmatically or automatically. Prism also provides support for implementing navigation with regions. Regions can be located by other components through the RegionManager component, which uses RegionAdapter and RegionBehavior components to coordinate the display of views within specific regions.
- Navigation. Navigation is defined as the process by which the application coordinates changes to its UI as a result of the user's interaction with the application or internal application state changes. Prism supports two styles of navigation: state-based navigation, where the state of an existing view is updated to implement simple navigation scenarios, and view-switching navigation, where new views are created and old views replaced within the application's UI. View-switching navigation uses a Uniform Resource Identifier (URI)–based navigation mechanism in conjunction with Prism regions to allow flexible navigation schemes to be implemented.
- EventAggregator. Components in a composite application often need to communicate with other components and services in the application in a loosely coupled way. To support this, Prism provides the EventAggregator component, which implements a pub-sub event mechanism, thereby allowing components to publish events and other components to subscribe to those events without either of them requiring a reference to the other. The EventAggregator is often used to allow components defined in different modules to communicate with each other.
- Dependency injection container. The Dependency Injection (DI) pattern is used throughout Prism to allow the dependencies between components to be managed. Dependency injection allows component dependencies to be fulfilled at run time, and it supports extensibility and testability. Prism is designed to work with Unity or MEF, or with any other dependency injection containers via the ServiceLocator.
- Services. Services are components that encapsulate non-UI related functionality, such as logging, exception management, and data access. Services can be defined by the application or within a module. Services are often registered with the dependency injection container so that they can be located or constructed as required and used by other components that depend on them.
- Controllers. Controllers are classes that are used to coordinate the construction and initialization of views that are to be displayed in a region within the application's UI. Controllers encapsulate the presentation logic that determines which views are to be displayed. The controller will use Prism's view-switching navigation mechanism, which provides an extensible URI-based navigation mechanism to coordinate the construction and placement of views within regions. The Application Controller pattern defines an abstraction that maps to this responsibility.
- Bootstrapper. The Bootstrapper component is used by the application to initialize the various Prism components and services. It is used to initialize the dependency injection container to register any application-level components and services with it. It is also used to configure and initialize the module catalog and the shell's view and view model or presenter.
- Multi-targeting. Prism supports the development of applications that can target both WPF and Silverlight. By adopting a separated presentation pattern, such as the MVVM or MVP patterns, you can separate the UI of your application from its presentation and business logic. View model, presenter, and model classes can be reused in both WPF and Silverlight versions of the same application. WPF-specific and Silverlight-specific views can then be defined in a way that encapsulates the UI for each.
Prism is designed so that you can use any of the preceding capabilities and design patterns individually, or all together, depending on your requirements and your application scenario. You can use the MVVM pattern, modularity, regions, commands, or events in any combination without having to adopt all of them. Of course, if you want to take full advantage of the benefits that separation of concerns and loose coupling offers, you will typically use many of Prism's capabilities and design patterns in conjunction with each other. The following illustration shows a typical Prism application architecture and shows how all the various capabilities of Prism can work together within a multi-module composite application.
Most Prism applications consist of a shell application that defines regions for displaying top-level views and shared services that can be accessed by the loaded modules. The shell defines a suitable catalog to specify which modules are to be loaded at startup time or on-demand, as appropriate. A dependency injection container is also defined, which allows component dependencies to be fulfilled at run time. Shared services and components are registered with the container by the Bootstrapper when the application starts.
Individual modules encapsulate a portion of the overall application's functionality and, using a separated presentation pattern such as MVVM, define views, view models, models, and service components. When the modules are loaded, views defined within the modules are displayed within the regions defined by the shell. After initialization completes, the user then navigates within the application using state-based or view-switching navigation to coordinate the visual update or display of new views within the application's regions.
Now that you've seen the major capabilities and design patterns that Prism supports, it's time to see how easily you can start to use Prism when developing a new application. This section provides an overview of the first few steps required to create a basic Prism application. You can extend this basic application to leverage the additional capabilities and design patterns provided by Prism, as required by your scenario.
|Although the Prism Library can be easily used to build new composite WPF or Silverlight applications (or applications that target both), you can also use Prism with existing applications that want to take advantage of one or more Prism capabilities or design patterns.|
A Prism application typically consists of a shell project and multiple module projects. The following illustration shows common activities needed when developing a composite application using the Prism Library.
A typical Prism application leverages most or all of the Prism capabilities and design patterns described earlier to be able to fully realize the benefits of the loose coupling and separation of concerns architectural design principles. However, for this example, the steps required to create a basic Prism application that consists of a single module that defines a single view are described.
Prism Library References
Most of your projects will need to reference the Prism Library assemblies. Prism provides signed binary versions of the Prism Library assemblies and a script that allows you to register them with Visual Studio so that you can use the Visual Studio Add References dialog box to add references to them. If you decide to not register the binaries, you will need to manually add references to the Prism Library binaries to your projects. You can also include the Prism Library projects in your solution and then use project references to them. The latter has the advantage of being able to use features like Go To Definition to step down into the Prism types, as well as being able to build and sign the Prism Library assemblies with your own strong name or certificate as part of your build process.
Define the Shell
The application shell provides the basic layout for the application. This layout is defined using regions that modules can use to place views. Views, like shells, can use regions to define discoverable areas that content can be added to, as shown in the following illustration. Shells typically set the appearance for the entire application and contain the styles that are used throughout the application.
Create the Bootstrapper
The bootstrapper is the glue that connects the application with the Prism Library services and the Unity or MEF containers. Each application creates an application-specific bootstrapper, which typically inherits from either UnityBootstrapper or MefBootstrapper, as shown in the following illustration. You will need to decide the approach you want to use to populate the module catalog. Minimally, each application will provide a module catalog and a shell.
By default, the bootstrapper logs events using the .NET Framework Trace class. Most applications will want to supply their own logging services, such as Enterprise Library logging. Applications can supply their logging service in their bootstrapper.
By default, the UnityBootstrapper and MefBootstrapper enable the Prism Library services. These can be disabled or replaced in your application-specific bootstrapper.
Create the Module
The module contains the views and services specific to a piece of the application's functionality. Frequently, these are contained in separate assemblies and developed by separate teams. A module is denoted by a class that implements the IModule interface. These modules, during initialization, register their views and services and may add one or more views to the shell. Depending on your module discovery approach, you may need to apply attributes to your module classes or define dependencies between your modules.
Add a Module View to the Shell
Modules take advantage of the shell's regions for placing content. During initialization, modules use the RegionManager to locate regions in the shell and add one or more views to those regions or register one or more view types to be created within those regions. The RegionManager is responsible for keeping track of regions throughout the application and is a core service initialized from the bootstrapper.
The remaining chapters in this guide provide details about Prism key concepts.
Prism assumes you have hands-on experience with WPF or Silverlight. If you need general information about WPF and Silverlight, see the following resources:
- Windows Presentation Foundation on MSDN.
- MacDonald, Matthew. Pro WPF in C# 2010: Windows Presentation Foundation in .NET 4, Apress, 2010.
- Nathan, Adam. WPF 4 Unleashed. Sams Publishing, 2010.
- Bugnion, Laurent. Silverlight 4 Unleashed, Sams Publishing, 2010.
- Brown, Pete. Silverlight 4 in Action, Manning Publications, 2010.
If you need general information about Silverlight, see the following resources:
- Microsoft Silverlight Home Page.
- Microsoft Silverlight Resources.
- Microsoft Silverlight Community.
- Silverlight Documentation on MSDN.
- Silverlight Books.
- Project Rosetta.
Prism's community site is http://www.codeplex.com/Prism. On this community site, you can post questions, provide feedback, or connect with other users for sharing ideas. Community members can also help Microsoft plan and test future offerings and download additional content, such as extensions and training material.
Last built: August 28, 2012