Getting Started With PRISM
-
Upload
circuit-bhai -
Category
Documents
-
view
222 -
download
0
Transcript of Getting Started With PRISM
-
8/3/2019 Getting Started With PRISM
1/62
Click to edit Master subtitle style
5/4/12
Getting started withPRISM
-
8/3/2019 Getting Started With PRISM
2/62
5/4/12
Agenda
Introduction to PRISMInitializing Prism Applications
Managing Dependencies Between Components
Modular Application DevelopmentImplementing the MVVM Pattern
Composing the User Interface
NavigationCommunicating Between Loosely Coupled
Components
-
8/3/2019 Getting Started With PRISM
3/62
5/4/12
Introduction
Prism is intended for software developers building WPF orSilverlight applications that typically feature multiplescreens, rich user interaction and data visualization, andthat embody significant presentation and business logic
These applications typically interact with multiple back-endsystems and services and, using a layered architecture, maybe physically deployed across multiple tiers
It is expected that the application will evolve significantlyover its lifetime in response to new requirements andbusiness opportunities
In short, these applications are "built to last" and "built forchan e"
-
8/3/2019 Getting Started With PRISM
4/62
5/4/12
Why Use Prism?
Designing and building rich WPF or Silverlight clientapplications that are flexible and easy to maintain can bechallenging
Application requirements can change over time
New business opportunities and challenges may presentthemselves, new technologies may become available, oreven ongoing customer feedback during the developmentcycle may significantly affect the requirements of theapplication
This requires an architecture that allows individual parts of
the application to be independently developed and testedand that can be modified or u dated later in isolation
-
8/3/2019 Getting Started With PRISM
5/62
5/4/12
The Composite Approach
An effective remedy for these challenges is topartition the application into a number ofdiscrete, loosely coupled, semi-independentcomponents that can then be easily
integrated together into an application "shell"to form a comprehensible solution
Composite applications provide manybenefits, including the following:
They allow modules to be individually developed,tested, and deployed by different individuals or
subteams; they also allow them to be modifiedor extended with new functionality more easily,
-
8/3/2019 Getting Started With PRISM
6/62
5/4/12
a enges NotAddressed by PrismPrism does not directly address the followingtopics:
Occasional connectivity and data synchronization
Service and messaging infrastructure design
Authentication and authorization
Application performance
-
8/3/2019 Getting Started With PRISM
7/62
5/4/12
Building blocks of PRISM
ModulesModule catalog
Shell
Views
View models and presenters
Models
Commands
Regions
Navigation
EventAggregator
-
8/3/2019 Getting Started With PRISM
8/62
5/4/12
application architecture
with the Prism Library
A t t t
-
8/3/2019 Getting Started With PRISM
9/62
5/4/12
Act v t es or creat ng acomposite application
-
8/3/2019 Getting Started With PRISM
10/62
5/4/12
Define the Shell
-
8/3/2019 Getting Started With PRISM
11/62
5/4/12
Create the Bootstrapper
-
8/3/2019 Getting Started With PRISM
12/62
5/4/12
Create the Module
The module contains the views and servicesspecific to a piece of the application'sfunctionality
Frequently, these are contained in separate
assemblies and developed by separate teams
A module is denoted by a class thatimplements the IModule interface
These modules, during initialization, registertheir views and services and may add one ormore views to the shell
Depending on your module discovery
approach, you may need to apply attributes to
Add M d l Vi t th
-
8/3/2019 Getting Started With PRISM
13/62
5/4/12
Add a Module View to theShell
Modules take advantage of the shell's regionsfor placing content
During initialization, modules use theRegionManager to locate regions in the shell
and add one or more views to those regions orregister one or more view types to be createdwithin those regions
The RegionManager is responsible forkeeping track of regions throughout theapplication and is a core service initializedfrom the bootstrapper
These modules, during initialization, registertheir views and services and ma add one or
-
8/3/2019 Getting Started With PRISM
14/62
5/4/12
What Is a Bootstrapper?
A bootstrapper is a class that is responsiblefor the initialization of an application builtusing the Prism Library
By using a bootstrapper, you have more
control of how the Prism Library componentsare wired up to your application
The Prism Library includes a default abstractBootstrapper base class that can bespecialized for use with any container
Many of the methods on the bootstrapperclasses are virtual methods
You can override these methods as
B t t
-
8/3/2019 Getting Started With PRISM
15/62
5/4/12
Bas c stages o t ebootstrapping process
-
8/3/2019 Getting Started With PRISM
16/62
5/4/12
Dependency Injection
Applications built with the Prism Library rely on dependencyinjection provided by a container
The library provides assemblies that work with the UnityApplication Block (Unity) or Managed ExtensibilityFramework (MEF), and it allows you to use otherdependency injection containers
Part of the bootstrapping process is to configure this
container and register types with the container
The Prism Library includes the UnityBootstrapper andMefBootstrapper classes
-
8/3/2019 Getting Started With PRISM
17/62
5/4/12
Creating the Shell
In a traditional Windows Presentation Foundation (WPF)application, a startup Uniform Resource Identifier (URI) isspecified in the App.xaml file that launches the main window
In an application created with the Prism Library, it is thebootstrapper's responsibility to create the shell or the mainwindow
This is because the shell relies on services, such as the
Region Manager, that need to be registered before the shellcan be displayed
-
8/3/2019 Getting Started With PRISM
18/62
5/4/12
Key Decisions
After you decide to use the Prism Library in your application,there are a number of additional decisions that need to bemade:
You will need to decide whether you are using MEF, Unity, or another
container for your dependency injection container
You should think about the application-specific services you want in yourapplication. These will need to be registered with the container.
Determine whether the built-in logging service is adequate for your needsor if you need to create another logging service.
Determine how modules will be discovered by the application: via explicit
code declarations, code attributes on the modules discovered viadirectory scanning, configuration, or XAML.
-
8/3/2019 Getting Started With PRISM
19/62
5/4/12
Core Scenarios
Creating a startup sequence is an important part of buildingyour Prism application
Creating a Bootstrapper for Your Application
Implementing the CreateShell Method
Implementing the InitializeShell Method
Creating and Configuring the Module Catalog
Creating and Configuring the Container
t B t t
-
8/3/2019 Getting Started With PRISM
20/62
5/4/12
reat ng a Bootstrapperfor Your Application
If you choose to use either Unity or MEF as your dependencyinjection container, creating a simple bootstrapper for yourapplication is easy
You will need to create a new class that derives from eitherMefBootstrapper or UnityBootstrapper
Then,implement the CreateShell method. Optionally, youmay override the InitializeShell method for shell specific
initialization
I t t
-
8/3/2019 Getting Started With PRISM
21/62
5/4/12
Imp ement ng t eCreateShell Method
The CreateShell method allows a developer to specify thetop-level window for a Prism application
The shell is usually the MainWindow or MainPage
Implement this method by returning an instance of yourapplication's shell class
In a Prism application, you can create the shell object, orresolve it from the container, depending on yourapplication's requirements
I t t
-
8/3/2019 Getting Started With PRISM
22/62
5/4/12
Imp ement ng t eInitializeShell Method
After you create a shell, you may need to run initializationsteps to ensure that the shell is ready to be displayed
Create the shell application object and set it as theapplication's main window
reat ng an on g r ng
-
8/3/2019 Getting Started With PRISM
23/62
5/4/12
reat ng an on gur ngthe Module CatalogIf you are building a module application, you
will need to create and configure a modulecatalog
Prism uses a concrete IModuleCataloginstance to keep track of what modules areavailable to the application, which modulesmay need to be downloaded, and where themodules reside
The Bootstrapper provides a protected
ModuleCatalog property to reference the
-
8/3/2019 Getting Started With PRISM
24/62
5/4/12
Demo
Basic implementation of PRISM
reat ng an on gur ng
-
8/3/2019 Getting Started With PRISM
25/62
5/4/12
reat ng an on gur ngthe Container
Containers play a key role in an application created with thePrism Library
Both the Prism Library and the applications built on top of itdepend on a container for injecting required dependenciesand services
During the container configuration phase, several coreservices are registered
In addition to these core services, you may have application-specific services that provide additional functionality as itrelates to composition
-
8/3/2019 Getting Started With PRISM
26/62
5/4/12
Core ServicesService interface Description
IModuleManager Defines the interface for the service that will retrieveand initialize the application's modules.
IModuleCatalog Contains the metadata about the modules in theapplication. The Prism Library provides severaldifferent catalogs.
IModuleInitializer Initializes the modules.
IRegionManager Registers and retrieves regions, which are visual
containers for layout.
IEventAggregator A collection of events that is loosely coupled betweenthe publisher and the subscriber.
ILoggerFacade A wrapper for a logging mechanism, so you canchoose your own logging mechanism. The loggingservice is registered with the container by thebootstrapper's Run method, using the value returned
by the CreateLogger method. Registering anotherlogger with the container will not work; insteadoverride the CreateLogger method on thebootstrapper.
IServiceLocator Allows the Prism Library to access the container. If youwant to customize or extend the library, this may beuseful.
Manag ng Depen enc es
-
8/3/2019 Getting Started With PRISM
27/62
5/4/12
Manag ng Depen enc esBetween ComponentsApplications based on the Prism Library are
composite applications that potentially consistof many loosely coupled types and services
They need to interact to contribute contentand receive notifications based on useractions
Because they are loosely coupled, they needa way to interact and communicate with oneanother to deliver the required business
functionality
A vantages o us ng a
-
8/3/2019 Getting Started With PRISM
28/62
5/4/12
A vantages o us ng acontainer
A container removes the need for a component to locate itsdependencies or manage their lifetimes
A container allows swapping of implemented dependencieswithout affecting the component
A container facilitates testability by allowing dependenciesto be mocked
A container increases maintainability by allowing newcomponents to be easily added to the system
-
8/3/2019 Getting Started With PRISM
29/62
5/4/12
application based on the PrismLibrary
A container injects module dependencies into the modulewhen it is loaded
A container is used for registering and resolving viewmodels and views
A container can create the view models and injects the view
A container injects the composition services, such as theregion manager and the event aggregator
A container is used for registering module-specific services,
which are services that have module-specific functionality
-
8/3/2019 Getting Started With PRISM
30/62
5/4/12
Injection Container Unity orMEF (Similarities)
They both register types with the container
They both register instances with the container
They both imperatively create instances of registered types
They both inject instances of registered types intoconstructors
They both inject instances of registered types into properties
They both have declarative attributes for marking types and
-
8/3/2019 Getting Started With PRISM
31/62
5/4/12
capabilities that MEF
does not
It resolves concrete types without registration
It resolves open generics
It uses interception to capture calls to objects and addadditional functionality to the target object
-
8/3/2019 Getting Started With PRISM
32/62
5/4/12
capabilities that Unity
does not
It discovers assemblies in a directory
It uses XAP file download and assembly discovery
It recomposes properties and collections as new types arediscovered
It automatically exports derived types
It is deployed with the .NET Framework
-
8/3/2019 Getting Started With PRISM
33/62
5/4/12
Core Scenarios
Containers are used for two primary purposesRegistering
Resolving
-
8/3/2019 Getting Started With PRISM
34/62
5/4/12
Registering
Before you can inject dependencies into an object, the typesof the dependencies need to be registered with thecontainer
Registering a type typically involves passing the containeran interface and a concrete type that implements thatinterface
There are primarily two means for registering types and
objects: through code or through configuration
Reg ster ng Types w t
-
8/3/2019 Getting Started With PRISM
35/62
5/4/12
Reg ster ng Types w tthe Unity ContainerDuring initialization, a type can register othertypes, such as views and services
Registration allows their dependencies to beprovided through the container and allowsthem to be accessed from other types
To do this, the type will need to have thecontainer injected into the module constructor
-
8/3/2019 Getting Started With PRISM
36/62
5/4/12
Resolving
After a type is registered, it can be resolved or injected as adependency
When a type is being resolved, and the container needs tocreate a new instance, it injects the dependencies into theseinstances
In general, when a type is resolved, one of three thingshappens:
If the type has not been registered, the container throws anexception
If the type has been registered as a singleton, the containerreturns the singleton instance
If the type has not been registered as a singleton, the containerreturns a new instance
Mo u ar App cat on
-
8/3/2019 Getting Started With PRISM
37/62
5/4/12
Mo u ar App cat onDevelopmentA modular application is an application that isdivided into a set of functional units (named
modules) that can be integrated into a largerapplication
A client module encapsulates a portion of theapplication's overall functionality and typicallyrepresents a set of related concerns
It can include a collection of relatedcomponents, such as application features,
including user interface and business logic, or
-
8/3/2019 Getting Started With PRISM
38/62
5/4/12
Module composition
Benefits of Building Modular
-
8/3/2019 Getting Started With PRISM
39/62
5/4/12
Benefits of Building ModularApplicationsThe modular application approach can help you to identify
the large scale functional areas of your application and allowyou to develop and test that functionality independently
It can also make your application more flexible and easier to
extend in the future
It allows you to break your application into manageablepieces
Each piece encapsulates specific functionality, and eachpiece is integrated through clear but loosely coupledcommunication channels
Prism's Support for Modular
-
8/3/2019 Getting Started With PRISM
40/62
5/4/12
Prism s Support for ModularApplication Development
A module catalog for registering named modules and eachmodule's location; you can create the module catalog in thefollowing ways:
By discovering modules in a directory so you can load all your moduleswithout explicitly defining in a centralized catalog
By defining modules in a configuration file
Declarative metadata attributes for modules to support initializationmode and dependencies
Integration with dependency injection containers to support loosecoupling between modules
For module loading:
IModule: The Building Block of
-
8/3/2019 Getting Started With PRISM
41/62
5/4/12
IModule: The Building Block ofModular Applications
Each module has a central class that is responsible for initializing the module
and integrating its functionality into the application
That class implements the IModule interface
The presence of a class that implements the IModule interface is enough toidentify the package as a module
The IModule interface has a single method, named Initialize, within whichyou can implement whatever logic is required to initialize and integrate the
module's functionality into the application
Depending on the purpose of the module, it can register views into compositeuser interfaces, make additional services available to the application, orextend the application's functionality
-
8/3/2019 Getting Started With PRISM
42/62
5/4/12
Module Lifetime
The module loading process in Prism includes thefollowing:
1. Registering/discovering modules. The modules to be loaded at run-time for a particular application are defined in a Module catalog. Thecatalog contains information about the modules to be loaded, theirlocation, and the order in which they are to be loaded.
2. Loading modules. The assemblies that contain the modules areloaded into memory. This phase may require the module to bedownloaded from the web or otherwise retrieved from some remotelocation or local directory.
3. Initializing modules. The modules are then initialized. This means
creating instances of the module class and calling the Initializemethod on them via the IModule interface.
M d l l di
-
8/3/2019 Getting Started With PRISM
43/62
5/4/12
Module loading process
-
8/3/2019 Getting Started With PRISM
44/62
5/4/12
Module Catalog
The ModuleCatalog holds information aboutthe modules that can be used by theapplication
The catalog is essentially a collection ofModuleInfo classes
Each module is described in a ModuleInfoclass that records the name, type, andlocation, among other attributes of themodule
Controlling When to Load a
-
8/3/2019 Getting Started With PRISM
45/62
5/4/12
Controlling When to Load aModule
Prism applications can initialize modules as soon as possible, knownas "when available," or when the application needs them, known as"on-demand
Modules required for the application to run must be downloaded with
the application and initialized when the application runs.
Modules containing features that are almost always used in typicalusage of the application can be downloaded in the background andinitialized when they become available
Modules containing features that are rarely used (or are supportmodules that other modules optionally depend upon) can bedownloaded in the background and initialized on-demand
Communicate Between
-
8/3/2019 Getting Started With PRISM
46/62
5/4/12
Communicate BetweenModules Loosely coupled events: A module can broadcast that a certain event has
occurred. Other modules can subscribe to those events so they will benotified when the event occurs. Loosely coupled events are a lightweightmanner of setting up communication between two modules; therefore, theyare easily implemented
Shared services: A shared service is a class that can be accessed through acommon interface. Typically, shared services are found in a shared assemblyand provide system-wide services, such as authentication, logging, orconfiguration
Shared resources: If you do not want modules to directly communicatewith each other, you can also have them communicate indirectly through ashared resource, such as a database or a set of web services
Partition Your Application into
-
8/3/2019 Getting Started With PRISM
47/62
5/4/12
Partition Your Application intoModules
Structure the application into separate clientmodules that can be individually developed,tested, and deployed
Each module will encapsulate a portion ofyour application's overall functionality
One of the first design decisions you will haveto make is to decide how to partition yourapplication's functionality into discretemodules
-
8/3/2019 Getting Started With PRISM
48/62
5/4/12
organized around verticalslices
An application with modules
-
8/3/2019 Getting Started With PRISM
49/62
5/4/12
An application with modulesorganized around horizontal layers
-
8/3/2019 Getting Started With PRISM
50/62
5/4/12
Demo
Modularity
Implementing the MVVM
-
8/3/2019 Getting Started With PRISM
51/62
5/4/12
Implementing the MVVMPattern Using the MVVM pattern, the UI of the application and the underlying
presentation and business logic is separated into three separateclasses
The view, which encapsulates the UI and UI logic
The view model, which encapsulates presentation logic and state
The model, which encapsulates the application's business logic and data
The MVVM classes and their
-
8/3/2019 Getting Started With PRISM
52/62
5/4/12
The MVVM classes and theirinteractions
-
8/3/2019 Getting Started With PRISM
53/62
5/4/12
Demo
Basic MVVM using PRISM
C it C d
-
8/3/2019 Getting Started With PRISM
54/62
5/4/12
Composite Commands
In some cases, you may want to be able to invokecommands on one or more view models from a control in aparent view in the application's UI
For example, if your application allows the user to edit
multiple items at the same time, you may want to allow theuser to save all the items using a single commandrepresented by a button in the application's toolbar orribbon
The CompositeCommand class maintains a list of childcommands (DelegateCommand instances).
Child commands are registered or unregistered using theRe isterCommand and Unre isterCommand methods
Implementing the SaveAll
-
8/3/2019 Getting Started With PRISM
55/62
5/4/12
Implementing the SaveAllcomposite command
-
8/3/2019 Getting Started With PRISM
56/62
5/4/12
Demo
Commanding
C i th U I t f
-
8/3/2019 Getting Started With PRISM
57/62
5/4/12
Composing the User Interface
An application user interface (UI) can be built by using oneof the following paradigms:
All required controls for a form are contained in a single ExtensibleApplication Markup Language (XAML) file, composing the form at designtime.
Logical areas of the form are separated into distinct parts, typically usercontrols. The parts are referenced by the form, and the form is composedat design time.
Logical areas of the form are separated into distinct parts, typically usercontrols. The parts are unknown to the form and are dynamically added tothe form at run time. Applications that use this methodology are known ascomposite applications.
-
8/3/2019 Getting Started With PRISM
58/62
5/4/12
Demo
UI Composition
N i ti i P i
-
8/3/2019 Getting Started With PRISM
59/62
5/4/12
Navigation in Prism
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 orinternal application state changes
UI updates can be accomplished by adding or removing elements from theapplication's visual tree, or by applying state changes to existing elements
within the visual tree
Navigation accomplished via state changes to existing controls in the visualtree is referred to as state-based navigation
Navigation accomplished via the addition or removal of elements from thevisual tree is referred to as view-based navigation
Prism provides guidance on implementing both styles of navigation, focusing
on the case where the application is using the Model-View-ViewModel (MVVM)pattern to separate the UI from the presentation logic and data
St t B d N i ti
-
8/3/2019 Getting Started With PRISM
60/62
5/4/12
State-Based Navigation
This style of navigation is suitable in thefollowing situations:
The view needs to display the same data or functionalityin different styles or formats.
The view needs to change its layout or style based on theunderlying state of the view model.
The view needs to initiate limited modal or non-modal
interaction with the user within the context of the view.
Vi B d N i ti
-
8/3/2019 Getting Started With PRISM
61/62
5/4/12
View-Based Navigation
-
8/3/2019 Getting Started With PRISM
62/62
Demo
Navigation