📜 ⬆️ ⬇️

Prism Developer Guide - Part 1, Introduction

If you are developing applications for XAML-based platforms, such as WPF, Silverlight, Windows Phone, or Windows Store, then Prism will certainly help you greatly to simplify their development. After creating several projects using Prism, I was convinced of the efficiency of this approach and decided to prepare a translation of the official Prism manual. So part one, what is Prism.
Table of contents
  1. Introduction
  2. Initializing Prism Applications
  3. Manage dependencies between components
  4. Modular Application Development
  5. Implementation of the MVVM pattern
  6. Advanced MVVM scripts
  7. Creating user interface
    1. User Interface Design Recommendations
  8. Navigation
    1. View-Based Navigation (View-Based Navigation)
  9. The interaction between loosely coupled components

Prism is a guide designed to help design and create rich, flexible, and easily supported Windows Presentation Foundation (WPF) applications, Silverlight Rich Internet Applications (RIAs), and programs for Windows Phone 7-8, as well as Windows Store applications. Using design patterns that embody important principles of architectural design, such as separation of concerns (SoC) and weak coupling (Loose coupling) , Prism helps you design and write applications with loosely coupled components that can be independently developed and then combined into one piece with minimal effort. This type of application is known as composite applications.


The note:
The word Prism was the code name for the manual, officially known as the Composite Application Guidance for WPF and Silverlight. For brevity and meaningfulness, and due to user requirements, this guide is now simply called Prism.

Prism is intended for developers of WPF, or Silverlight applications with multiple screens, enhanced user interaction, data visualization, and complex interaction between views and business logic. These applications typically interact with multiple back-end systems and services and, using a multi-tier architecture, can be physically deployed at several levels . It is expected that the application will evolve significantly over its lifetime, in response to new business requirements. In short, these applications are built for a long time and for changes. Applications that do not require these features may not benefit from using Prism.

Prism includes references to examples of implementation, QuickStarts, reusable library code (Prism library) and extensive documentation. This version of Prism is aimed at Microsoft. NET Framework 4.0 (currently 4.5) and Silverlight 4 and includes a new guide on the Model-View-ViewModel design pattern (MVVM), navigation, and the Managed Extensibility Framework (MEF). Since Prism is built on the .NET Framework 4.0 (which includes WPF) and Silverlight 4, knowledge of these technologies is useful for evaluating and understanding Prism.
')
It should be noted that although Prism is not difficult to learn, developers should be ready to adopt new models and practices for them. If you are going to study these models and practices, do not forget to estimate the time spent on it in the terms of your projects.

Why use Prism


Designing and developing rich WPF or Silverlight applications that are flexible and supported can be challenging. This section describes some common problems that may arise during their creation and how Prism helps in solving them.

Problems in developing client applications

As a rule, developers of client applications encounter a lot of problems. Application requirements may change over time. New business opportunities and challenges may arise, new technologies may become available, or even customer feedback during the development cycle can significantly affect application requirements. Therefore, it is important to write applications so that they are flexible and can be easily modified or expanded in the future. Creating this level of flexibility can be difficult to achieve. This may require an architecture that allows individual parts of the application to be developed and tested independently, and which can be modified or updated later, in isolation, without prejudice to the rest of the application.

Most enterprise applications are complex and require a whole team of programmers and designers to develop. How to develop applications in such a way that several developers or teams can work effectively on different parts of the application independently and be sure that parts of the application are seamlessly connected during integration can be a serious problem.

Design and development in a monolithic style can lead to an application that is very difficult and inefficient to maintain. In this case, the word “monolithic” indicates an application in which the components are very closely related, and there is no clear separation between them. As a rule, applications designed and built in this way suffer from problems that make a developer’s life difficult and full of suffering. It is difficult to add new functions to the system, or replace existing functions, it is difficult to correct errors without affecting other parts of the system. Such an application is difficult to test and deploy. In addition, all this affects the efficiency of interaction between developers and designers.

Composite approach

An effective means to solve these problems is to divide the application into a number of discrete, loosely coupled, semi-independent parts, which can then be easily integrated into the shell application to form a complete solution. Applications designed and built in this way are often called composite applications.

Composite applications have many advantages, including the following:
  • Allow modules to be developed, tested, and deployed to various individuals or subcommands. These modules can also be easily modified, or supplemented with new functionality, which allows the application to be highly extensible and supported. Note that with this approach, even a single person’s project can benefit from creating a well-tested and supported application.
  • They have a common shell containing user interface components that are provided by various modules that are loosely coupled to each other. This reduces the discord that occurs when several developers add new functionality to the user interface and provides a solid appearance.
  • Promote code reuse and division of responsibility between horizontal application levels, such as authorization and authentication, and vertical levels, such as business functions specific to your application. It also makes it easy to manage dependencies and interactions between application components.
  • Helps support role separation by allowing different individuals or teams to focus on a particular task, or part of the functionality, according to their specialization or experience. In particular, it provides a clearer separation between the user interface and the business logic of the application. This means that the designer can focus on creating a more user-friendly user interface, and the programmer can create more complex business logic.

Composite applications are ideal for a number of client scenarios. For example, a composite application is ideal for creating rich client applications on top of dissimilar back-end systems. The following figure shows an example of this type of application.

Composite application with multiple application systems

In this type of application, the user can be presented with a rich and flexible user interface focused on functionality. These applications can consist of several server systems, services, and data warehouses, each of which is represented by one or several dedicated modules. A clear separation between application logic and user interface allows you to provide a consistent and separate view of all composite modules.

In addition, a composite application can be useful when there are independently developing user interface components that are poorly integrated with each other and supported by separate commands. The following figure shows an example of the screen for this type of application. Each of the selected areas represents independent components that are combined in the user interface.

Composite application Stock Trader Reference Implementation

In this case, the composite application allows the user interface to be composed dynamically at run time. This provides flexible options for the user. For example, this allows new functions to be dynamically added at runtime, which provides rich customization options and easy extensibility.

Problems that are not solved when using Prism

While Prism can help you solve many of the problems you may encounter when creating a WPF or Silverlight application, there are many other problems that you may encounter depending on the scenarios and requirements. For example, Prism does not directly concern the following topics:
  • Intermittent connection and data synchronization.
  • Creating distributed and cloud applications.
  • Authentication and authorization.
  • Application performance.
  • Application Version Management.
  • Error handling and fault tolerance.


Getting Started with Prism


Prism suggests that you have hands-on experience with WPF or Silverlight. There are several important concepts that Prism uses especially often, and you should be familiar with them. It:
  • XAML (Extensible Application Markup Language) . A language that declaratively describes the user interface in WPF, Silverlight, Windows Phone, and Windows Store applications.
  • Data binding , or data binding. How UI elements are connected to components and data in WPF and Silverlight.
  • Resources . How styles, data templates, and controls are created and used in WPF and Silverlight.
  • Commands . The way custom gestures and input are connected to controls.
  • User controls (user controls). Components that provide custom behavior or appearance.
  • Dependency properties (dependent properties). Common Language Runtime Extensions (CLR) that allow you to set and control property values ​​that support data binding, routed commands, and events.
  • Behaviors (behaviors). Objects that encapsulate interactive functionality that can be easily applied to controls in the user interface.


Prism installation

This section describes how to install Prism. It includes the following three steps:
  • Install the necessary system applications.
  • Extract Prism source code, executables and documentation.
  • Register Prism libraries in the system.


Step 1: Install the Required Applications

Before using the Prism library, the following must be installed:
  • Microsoft .NET Framework 4.0 or higher (installed with Visual Studio)
  • Microsoft Visual Studio 2010/2012 Professional, Premium, or Ultimate version.

The note
Visual Studio Express Edition can also be used to create Prism applications.

If you are developing a Silverlight application, then the following should be installed:

The note
Although Silverlight Tools for Visual Studio are not necessary, it is recommended that all WPF and Silverlight developers download and use the latest version.
The WPF and Silverlight Designer for Visual Studio is updated with the Silverlight runtime and Software Development Kit (SDK), which are included in the download. These updates add new features and fix bugs.

Optionally, you can also install:

The note
For more information about using Prism on the Windows Phone 7, see the Windows Phone 7 Developer Guide community site on the CodePlex ( http://wp7guide.codeplex.com/ .)

Step 2: Extract the Prism sources, executables and documentation

To install Prism, right-click on the Prismv4.exe file, and then select Run as administrator . This will extract the source code, executable files and documentation in a folder of your choice.

Step 3: Register Libraries

Registering the Prism library is not necessary, but by doing this, you will simplify adding references to Prism assemblies to your projects. If you select library registration, they will be visible in the list of the Visual Studio Add References dialog box. If you do not register them, you will have to manually add links to the Prism library files in your project. Signed Prism assemblies are located in the following folders:
  • {prism} \ Bin \ Desktop
  • {prism} \ Bin \ Silverlight
  • {prism} \ Bin \ Phone

To register the Prism library files, run the RegisterPrismBinaries.bat command file located in the folder where you unpacked Prism. It will create a temporary .reg file with the information needed to register the Desktop, Silverlight, and Phone folders containing binary files, and use it to update the system registry. Since the register update is a privileged operation, a User Account Control (UAC) message may appear if you do not have the necessary privileges. For more information about UAC, see " What is User Account Control ."
The note
Only one copy of binary files can be registered using a script; if multiple copies of the Prism library are registered, only binary files for the last registered copy will be available in Visual Studio.

The note
The easiest way to install Prism is to use the nuget package manager , which is installed by default in VS2012. To install, type the command PM> Install-Package Prism in the nuget console or use the visual interface.

Prism Review


What's new in this release?

This release has been updated to take advantage of the features of WPF 4.0 and Silverlight 4. It contains several areas with new and significantly updated tutorials, including the following:
  • Managed Extensibility Framework (MEF) . Prism now includes guidance on how to use MEF to manage dependencies and maintain loose coupling between components. The use of MEF is discussed in Chapter 3, “Managing Dependencies Between Components.”
  • Model-View-View Model (MVVM) pattern (model - view - view model). Previous versions of Prism provided detailed instructions on the use of presentation split templates. This tutorial now focuses on the MVVM pattern. Chapter 5 provides an overview of the MVVM pattern and describes its implementation. Chapter 6 covers more advanced MVVM scripts.
  • Navigation Prism now provides guidance on how to implement navigation in your WPF or Silverlight application. This guide covers both state-based navigation, which is used when updating a visual state in one window, and view-based navigation, which is used when navigating between views. Both approaches are discussed in Chapter 8, “Navigation.”

Prism now also provides signed versions of the Prism library builds. This is done for the convenience of developers who want to use the Prism library in their applications without modification. In addition, all Visual Studio projects (for the Prism library, as well as links to implementations and QuickStarts) were also transferred to VisualStudio 2010 and Silverlight 4.

What comes out of the box

Prism contains:
  • Source codes of the Prism library . The source code includes the basic functionality of Prism plus Unity and MEF extensions, which provide additional components for using Pirsm with the Unity Application Block (Unity) and Managed Extensibility Framework (MEF) .
  • Prism library builds . Signed compiled Prism builds. They are located in the bin folder and are a convenient way to use the Prism library. Assemblies can be rebuilt and registered in Visual Studio using the provided script file. They also include Unity Application Block and Service Locator assemblies.
  • Examples of implementation . Comprehensive sample applications that illustrate how Prism can be used to implement real-world application scenarios. Examples are intentionally incomplete, but they show how many Prism patterns can work together in a single application. Prism provides two examples: Stock Trader Reference Implementation (Stock Trader RI) and MVVM Reference Implementation (MVVM RI).
  • QuickStarts . Prism includes source code for several small, simplicity-oriented examples that illustrate the MVVM pattern, navigation, composite UI, modularity, commands, event aggregation, and the creation of multi-purpose applications.
  • Documentation Prism 4 documentation provides an overview of the goals and concepts of Prism and includes detailed instructions for using each tool and design pattern provided by Prism. The following section provides a step-by-step overview of the topics covered.


Documentation review

Prism documentation covers a wide range of topics, including an overview of common development problems and an approach to developing composite applications, an overview of the Prism library and design patterns that it implements, as well as step-by-step instructions on how to use the Prism library during the development process. The documentation is addressed to a wide technical audience and is designed to help the reader understand and use Prism in their own applications. The documentation includes the following (hereinafter referred to as MSDN links):
  • Part 2, Initializing Prism Applications . This chapter discusses what needs to be done to get and run a modular Prism application.
  • Part 3, Managing Dependencies Between Components . "Applications based on the Prism library rely on the dependency injection container (DI). Although Prism can work with almost any container, the Prism library provides two default options: Unity or MEF. This chapter discusses various development techniques and things to look for when working with DI containers.
  • Part 4, Modular Application Development . This chapter discusses the basic concepts, key decisions and basic scenarios when creating modular client applications with Prism.
  • Part 5, Implementing the MVVM Pattern . Using the MVVM pattern, you divide the interface of your application and the underlying layers of the view and business logic into three separate classes: view, model, and view model. This chapter discusses the basic concepts underlying the MVVM pattern and describes how to implement it in an application using Prism.
  • Part 6, Advanced MVVM Scenarios . This chapter provides guidance on how to implement more complex scenarios using the MVVM pattern, including the implementation of composite commands (commands that represent a group of commands) and how to handle web service requests and user interaction asynchronously. This chapter also contains recommendations for using DI containers such as Unity or MEF to construct and integrate MVVM classes.
  • Part 7, Composing the User Interface . Regions are placeholders that allow the developer to specify where the views will be displayed in the user interface. In Prism, there are two approaches to displaying species in a region: the study of a species and the introduction of a species. This chapter describes how to work with regions and user interface. It also contains information for user interface designers to help them understand composite applications.
  • Part 8, Navigation . Navigation is the process by which coordinated changes occur in the user interface as a result of user interaction with the application, or changes in its internal state. This chapter provides guidance on the implementation of state-based navigation, where the state of the user interface view is updated according to the navigation and the view-based navigation in which new views are created and displayed in the region.
  • Part 9, Communicating Between Loosely Coupled Components . This part discusses various ways to provide communication between components in different modules using commands, event aggregation, context of regions, and common services.
  • Part 10, Sharing Code Between Silverlight and WPF . Multi-platform applications target two multiple platforms using common code. In this case, this is WPF and Silverlight. This part will help you understand what such applications are, their advantages and disadvantages. Prism provides a Project Linker tool that helps you automatically create and maintain links from the root project to the target, to share common code between Silverlight and WPF.
  • Part 11, Deploying Prism Applications . This part covers the deployment of WPF and Silverlight applications.
  • Appendix A, Glossary . "This app provides a short list of terms, concepts, design patterns, and features provided by Prism.
  • Appendix B, Patterns in the Prism Library . This application describes the design patterns of applications used in the Prism library and the Stock Trader RI application. This topic is intended primarily for architects and developers who want to familiarize themselves with the patterns that help overcome difficulties in designing composite applications.
  • Appendix C, Prism Library . This application provides an overview of the Prism library.

The following topics include source code and interactive content:
  • Appendix D, Upgrading from Previous Versions . This article discusses what you need to know to upgrade from previous versions of Prism.
  • Appendix E, Extending Prism . This appendix discusses how you can extend the Prism library.
  • Appendix F, Reference Implementations . Here are some sample applications shipped with Prism. For more information, see the “Exploring the Reference Implementations.”
  • Appendix G, QuickStarts . Prism includes source code for several quick-start applications that demonstrate key ideas. For more information, see the “Exploring the QuickStarts.”
  • Appendix H, Prism Hands-On Labs . Labs show the process of building the simplest composite WPF and Silverlight applications step by step. This addition is aimed primarily at developers who want to understand the basic principles of the Prism library. It also includes laboratories for publishing and updating Prism WFP applications using ClickOnce.

QuickStarts Overview

QuickStarts are small applications that illustrate certain aspects of Prism. They are the ideal starting point if you want to gain an understanding of key ideas and are well trained in new technologies through reading the 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 consisting of modules. Modules can be loaded statically when the shell contains references to module assemblies, or dynamically when modules are dynamically detected and loaded at run time. QuickStarts demonstrate the use of Unity and MEF containers.
  • Basic MVVM QuickStart and MVVM QuickStart . Basic MVVM QuickStart demonstrates how to create a very simple application that implements the MVVM pattern. MVVM QuickStart demonstrates how to create an application that implements the MVVM pattern, showing some of the more common problems that developers may encounter, such as validation, user interface interaction, and data patterns.
  • UI Composition QuickStart . This QuickStart demonstrates how to create WPF and Silverlight interfaces, consisting of various views that are dynamically loaded into regions and that interact with each other in various ways. It shows how to use both user interface composition approaches: view exploration and view embedding.
  • State-Based Navigation QuickStart . This QuickStart demonstrates an approach to creating navigation in a simple application. The approach used in this QuickStart uses the Silverlight Visual State Manager (VSM) to define various application states and defines animations for all states and transitions between them.
  • View-Switching Navigation QuickStart . This QuickStart demonstrates the use of the Prism Region Navigation API. QuickStart shows several navigation types, including navigation to a view in a region, navigation to a view in a region contained in a different view (nested navigation), navigation log support, creating views in real time, passing contextual information to view navigation, views and species models involved in navigation and the use of navigation as part of an application built using modularity and user interface composition.
  • Commanding QuickStart . This QuickStart demonstrates how to create a WPF or Silverlight user interface that uses the commands provided by the Prism library to handle user interface events in various ways.
  • Event Aggregation QuickStart . This QuickStart demonstrates how to create a WPF or Silverlight application that uses an event aggregation service. This service allows you to weaken the connections between components in an application.
  • Multi-Targeting QuickStart . This QuickStart demonstrates the structure of a project created using the multiplatform WPF and Silverlight environments. It provides experience creating desktop applications (WPF) and Rich Internet Application (RIA) applications (Silverlight).

Overview of Implementation Examples

Examples of implementations of Prism (reference implementation) are exemplary applications based on real situations that customers face. Look at them as a basis for building applications with the Prism library. They include:

Upgrade from previous versions


If you are upgrading from previous versions of the tutorial, you can check " Upgrading from Previous Releases " in Appendix D for the main differences between this and previous releases.

A quick review of Prism


Prism creation goals

Prism was designed to help you design and build rich, flexible, and easy-to-use WPF and Silverlight applications. Prism library implements design patterns that embody important principles of architectural design, such as sharing responsibility and loose coupling. Using the design patterns and capabilities provided by Prism will help you design and write applications with loosely coupled components that can evolve independently and then can be easily integrated into one application.

Prism is designed around the core principles of architectural design, such as sharing responsibility and loose coupling. This allows you to get many benefits, such as:
  • Reuse code . Prism , , . , , , .
  • . Prism , , , , , . Prism .
  • . Prism , . Prism WPF Silverlight , , . , .
  • . Prism , . Prism ( , - ) - ( , , , ).
  • < . Prism , . , , , Prism , .

, Prism , Prism , , . Prism , , , .

, , Prism , , . Prism , Microsoft, , VisualStudio, . . , Prism , .
Test.Description.
Acceptance Testing., . .
Application Building Exercises., .
Black Box Testing., .
Cross Browser Testing..
Cross Platform Testing..
Globalization Testing..
Performance Testing., .
Security Review.Microsoft, , .
Stress Testing., , .
White Box Testing., , , .

Prism UI, . . , , Prism .
Test.Description.
UI Automation Tests.; .
Unit Tests.

Prism

Prism , , . Prism , .
  • (Modules) . – , , , ( ) . . Prism . , (, ) , , , . , (, ), .
  • (Module catalog) . . , , , . ModuleManager ModuleLoader , , , . - : , XAML, . , .
  • (Shell) . — -, . , , . , . , UI, .
  • (Views) . – UI, . MVVM MVP , UI, . , UI , , . , .
  • (View models) (Presenters) . — , . – MVVM. . , . MVP . , , .
  • (Models) . -. MVVM MVP. , - , .
  • (Commands) . , UI. . Prism DelegateCommand CompositeCommand . , .
  • (Regions) . – , ( ), . UI, . , ContentControl, ItemsControl, ListBox, TabControl, . . Prism . RegionManager , ( RegionAdapter ) ( RegionBehavior ) .
  • (Navigation) . , UI , . Prism : state-based , , view-switching , UI. View-switching Uniform Resource Identifier (URI) Prism, .
  • (EventAggregator) . . , Prism EventAggregator , pub-sub , , , . EventAggregator , , , .
  • (Dependency injection container) . (Dependency Injection, DI) – , Prism . DI , . Prism , Unity MEF, ServiceLocator .
  • (Services) . — , UI , , , . . , , .
  • (Controllers) . — , , , . , , . view-swithing , , URI , . (Application Controller) , .
  • (Bootstrapper) . Prism. DI , . , Presenter.
  • (Multi-targeting) . Prism , WPF, Silverlight. , MVVM MVP, UI -. , Presenter WPF Silverlight . WPF Silverlight , UI .

Prism , , . MVVM, , , , . , , Prism . Prism , Prism .

Typical architecture of composite Prism application.

Prism -, , . , , , . DI , . .

, , MVVM, , , , . , , , , . , , , state-based view-switching .

Prism

, , Prism, , Prism . , Prism. , , Prism, .
The note
Prism , WPF Silverlight ( , ), Prism , Prism .

Prism . , Prism.

The steps required to create a composite application.

Prism Prism , , . , , , Prism, , .
The note
Prism. Prism Prism , Visual Studio , Add References . , . Prism . Go To Definition , Prism, Prism , .

Shell creation

. , . , , , , , . , .

Shell, views and regions.


, Prism Unity, MEF. , UnityBootstrapper MefBootstrapper , . , . , .

, , .NET Framework Trace . , Enterprise Library. .

, UnityBootstrapper MefBootstrapper Prism. .

The diagram shows the connection to the Prism library.


, . , . , IModule . , , . , , .


, . RegionManager, , , . RegionManager , .

Prism.

Additional Information


Prism , WPF Silverlight. WPF Silverlight, :
  • Windows Presentation Foundation 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:

Community


Prism community site http://www.codeplex.com/Prism . On this site, you can ask questions, get feedback, or contact other users to exchange ideas. Community members can also help Microsoft plan and test future offers and download additional content, such as extensions and training material.

Source: https://habr.com/ru/post/176851/


All Articles