Sie sind auf Seite 1von 34

Learning to Share with Prism

Rik Robinson Senior Consultant MCPD - Enterprise Applications MCTS - WPF/Web/Windows/Distributed

www.wintellect.com

What we do
Consulting | Debugging | Training

Who we are
Founded by top experts on Microsoft Jeffrey Richter, Jeff Prosise and John Robbins our mission is to help our customers achieve their goals through advanced software-based consulting and training solutions.

How we do it
Consulting & Debugging
Architecture, analysis, and design services Full Lifecycle custom software development Content creation Project management Debugging & performance tuning

Training
On-site instructor-led training Virtual instructor-led training Devscovery conferences August 18-20, 2009

Agenda
Composite Applications Introduce Prism Prerequisites Dependency Injection Container (Unity) Shell and Regions Bootstrapper Modules Views
- View Discovery - View Injection

Separated Presentation Pattern


- Model-View-ViewModel

Services Commanding Eventing Multi-Targeting Questions

What is a Composite Application?

What is a Composite Application?


Monolithic Applications
- tightly-coupled with no clear separation of concerns - hard to extend - hard to maintain - hard to test - hard to deploy - easy to introduce new bugs when fixing others

What is a Composite Application?


Monolithic Applications
- tightly-coupled with no clear separation of concerns - hard to extend - hard to maintain - hard to test - hard to deploy - easy to introduce new bugs when fixing others

Composite Applications
- loosely-coupled modules - common shell - common services - separated presentation patterns - mockability/testability

What is Prism?
Formal name is Composite Application Guidance A set of guidance (not a framework) to build flexible composite client applications Whats included: - Composite Application Library reusable library code - Documentation, Quick Starts, Hands-On Labs - Reference Implementation (StockTraderRI) Key Benefits - Dependency Injection Container (via Unity Application Block) - UI Composition - Modularity - Multi-Targeting (Silverlight and WPF)

Prism Assemblies
Microsoft.Practices.Composite
- Core components such as Modularity, Logging Services - Nothing UI-specific

Microsoft.Practices.Composite.Presentation
- Commands, Regions, Events

Microsoft.Practices.Unity Microsoft.Practices.ObjectBuilder2
- Unity Container Application Block

Microsoft.Practices.Composite.UnityExtensions
- Base and Utility classes for use with Unity - UnityBootstrapper

Microsoft.Practices.ServiceLocation
- Common Service Locator interface

Prerequisite Concepts
Databinding
- UI elements bound to data in WPF/Silverlight

Resources
- Styles, brushes, data templates, etc

Routed Commands
- Actions independent of their UI exposure

Routed Events
- Events can be handled by multiple listeners up and down the visual tree

Dependency Properties
- Dependent on multiple providers (animation, parent, etc) - Built-in change notification

XAML
- Declarative language for initialization of objects and layout

Dependency Injection (DI) Container


Specialized version of the Inversion of Control (IoC) Pattern Container reduces coupling between components by providing a collection of shared services

Dependencies get automatically injected into component (Module) via Constructor or Setter injection
Allows registration of Types, Instances, Services with the Container
container.RegisterType<ILogger, MyLogger>();

Can act as a Service Locator to Resolve registered Types


ILogger logger = container.Resolve<ILogger>();

Shell and Regions


Shell
Shell is main window of the composite application Contains named locations called Regions Developer defines where Regions appear and how they get filled Think of it like ASP.NET Master Pages

Shell and Regions


Shell
Shell is main window of the composite application Contains named locations called Regions Developer defines where Regions appear and how they get filled Think of it like ASP.NET Master Pages

Regions
Regions contain Views Regions can be ContentControl, ItemsControl, TabControl RegionAdapters allow you to create your own type of Regions Regions indicated by RegionName attached property
<ContentControl regions:RegionManager.RegionName="SelectionRegion />

Shell and Regions

Image source: CAL docs

Demo
Shell and Regions

Modules
Module represents a set of related concerns
- Contains Views, ViewModels, Presenters, Services, etc.

Designed to be discovered and loaded at run-time - ModuleCatalog can be loaded programmatically, from a config file,
from a directory (WPF), from a XAP file (SL), etc.

Communicates with other Modules and with the Shell in a loosely-coupled manner

Can be independently extended, maintained, tested, and deployed


Allows for code re-use as modules can be used across projects and between Silverlight and WPF

Bootstrapper
UnityBootstrapper default bootstrapper base class
- Handles initialization with Unity - Can be replaced with non-Unity bootstrapper

Configures the Container (Unity by default)


Configures Region Adapter Mappings

Creates the Shell


Creates and loads the ModuleCatalog

Initializes any available Modules

Demo
Modules and Bootstrapper

Views
Main unit of UI construction in Composite Applications
- Usually a UserControl but could be a DataTemplate

Views implemented in Modules in Prism application


Views can have child Regions

Generally implemented using a Separated Presentation pattern such as Presentation Model, MVVM, MVP
View Injection - Created and displayed programmatically View Discovery - Created and displayed automatically

View Injection
Views added/removed from Regions directly by the Module

Allows more explicit control than View Discovery

Image source: CAL docs

View Discovery
Views are registered by the Module and then automatically created and loaded View Discovery is simpler and preferred over View Injection

Image source: CAL docs

Demo
Adding a View

Separated Presentation Patterns


Presentation Model Pattern
- MVC, MVP, MVVM, etc - See Martin Fowlers books or website for gory details - There are many opinions about the various flavors of Presentation Model

Separated Presentation Patterns


Presentation Model Pattern
- MVC, MVP, MVVM, etc - See Martin Fowlers books or website for gory details - There are many opinions about the various flavors of Presentation Model

Model-View-ViewModel
- specialized version of Presentation Model Pattern - works well with Silverlight & WPF due to their databinding capabilities - ViewModel exposes the data and actions needed by the View - ViewModel sits below the UI layer - Views bind their DataContext to the ViewModel - Views tend to have very little code in code-behind files

Demo
Adding the ViewModel

Services
Services not always web services

Core shared services provided by Prism


- IModuleManager, IRegionManager, ILoggerFacade, etc

Modules can contain application-specific services Modules register Services with the Container Some Services may be common across Modules
- Often in an Infrastructure or Common module

Demo
Adding a Service

Commanding
Provide a way to bind the UI to the action WPF Routed Commands flow up/down the visual tree Silverlight contains ICommand but has no implementation

Two types of commands in Prism


- Command Delegation - Command Composition

Prism command implementation similar to WPF


Provides for passing of CommandParameter CanExecute
- Automatic enable/disable for buttons

Demo
Adding a Command

Eventing
Allows decoupling of publishers and subscribers EventAggregator is a container for events EventAggregator is created/injected upon first use by Prism

Only one Event class provided by Prism


- CompositePresentationEvent<T> - Base class for custom Events - The generic parameter specifies the Type of object supplied by the publisher and expected by the subscriber

The Subscribe method allows for three ThreadOptions

Subscribers can supply a delegate to filter Events


There is an Unsubscribe method

Event Aggregation

Image source: CAL docs

Demo
Adding an Event

Multi-Targeting
Silverlight and WPF are not binary-compatible Prism provides Project Linker Project Linker automatically syncs shared code and compiles for specific environment Non-UI code and components are easiest to share Separated Presentation Patterns are essential for multi-targeting Generally best to start with Silverlight since it is a subset Use #if SILVERLIGHT to force conditional compile

Partial classes and methods are useful


Use the same namespaces in both Silverlight and WPF

Resources
Composite Application Guidance (Prism) on CodePlex Unity on CodePlex Unity - Dependency Injection and Inversion of Control Container UI Composition Patterns The Dependency Injection Pattern What is it and why do I care? Prism for Silverlight - An Intro to Composite Applications (Video) How Commanding Works in Prism and MVVM (Video) Creating a modular application using Prism V2 Channel 9 videos Designing Composite Applications (Presentation slides) Using WCF + Silverlight 2 + PRISM : Gotchas Josh Smith on MVVM in WPF Brian Noyes on Understanding Routed Events and Routed Commands in WPF Martin Fowlers description of Presentation Model pattern XAML Guidelines for Creating a Composite UI Guidance on Differences between WPF and Silverlight

Questions?
Rik Robinson
Senior Consultant Wintellect
rrobinson@wintellect.com

Das könnte Ihnen auch gefallen