17
9/16/2010 1 ©2010 Brian Noyes, IDesign Inc. All rights reserved ©2010 Brian Noyes, IDesign Inc. All rights reserved Brian Noyes www.idesign.net Prism – Composite Application Guidance Prism Developed by Microsoft patterns and practices Old name: Composite Application Guidance for WPF and Silverlight Guidance for building composite applications Loosely coupled, modular applications Patterns-based documentation, samples, re-usable code Designed as a successor to CAB/SCSF for WPF General design goals: Light weight – use any combination of features Use any container Strong typing

Prism – Composite Application Guidance - Software …softinsight.com/downloads/INETA/PrismCompositeApp...Used to decompose your window from one big monolithic blob into a bunch of

  • Upload
    letruc

  • View
    217

  • Download
    1

Embed Size (px)

Citation preview

9/16/2010

1©2010 Brian Noyes, IDesign Inc. All rights reserved 

©2010 Brian Noyes, IDesign Inc. All rights reserved

Brian Noyeswww.idesign.net

Prism – Composite Application Guidance

Prism

Developed by Microsoft patterns and practices

Old name:

Composite Application Guidance for WPF and Silverlight

Guidance for building composite applications

Loosely coupled, modular applications

Patterns-based documentation, samples, re-usable code

Designed as a successor to CAB/SCSF for WPF

General design goals:

Light weight – use any combination of features

Use any container

Strong typing

9/16/2010

2©2010 Brian Noyes, IDesign Inc. All rights reserved 

Prism

Consists of:

Prism Library The library formerly known as CAL

Stock Trader Reference Implementation (RI)

Quickstarts

Documentation

How-Tos

Current Released Version: 2.2

prism.codeplex.com

Prism Version History

Prism 1 – June 2008 – WPF Only

Prism 2 – Feb 2009 – Added support for Silverlight 3

Prism 2.1 – October 2009 – Minor update

Prism 2.2 – July 2010 – Updated for .NET 4 / Silverlight 4

Prism 4 – October 2010 – MVVM / MEF / Navigation added

9/16/2010

3©2010 Brian Noyes, IDesign Inc. All rights reserved 

Prism 4

Almost complete (feature complete end of September)

Expected release October 2010

Public Drops + Release 1 CTP already available

prism.codeplex.com

Main focus areas:

MVVM Guidance

MEF container support

Navigation guidance

Why Composites?

Good design – based on well known design patterns

Loose coupling

Separation of concerns

End goal:

Maintainability

Extensibility

Testability

etc

9/16/2010

4©2010 Brian Noyes, IDesign Inc. All rights reserved 

Prism Key Features

Modularity

Compose application of loosely coupled units of functionality

UI Composition

Compose the user interface of loosely coupled UI parts

Communications

Loosely coupled communications with events and commands

App structure

Well defined abstractions for different kinds of presentation layer code Model-View-ViewModel, Application Controller, Bootstrapper, Modules, etc

Navigation

Well defined approach for switching and navigating through different views/screens in the application

Prism Application Architecture

Prism Library

Shell Application

Bootstrapper

Shell

Shared resources

Modules

Views / ViewModels

Controllers

Services

9/16/2010

5©2010 Brian Noyes, IDesign Inc. All rights reserved 

Bootstrapper

Startup code for your application

Alternative to putting everything in the Main() method / App class

Well defined intialization point in your application

Not required, but recommended

Derive from UnityBootstrapper or MefBootstrapper

Bootstrapping Process

9/16/2010

6©2010 Brian Noyes, IDesign Inc. All rights reserved 

Shell

Main window of the application

Could be more than one

Presented at startup (typically, could be minimized to System Tray)

Root element for the whole UI Even modular views that know nothing about the shell

Typically knows nothing about the views that it is composed of Just provides a “shell” to contain them, thus the name

May define regions for dynamic plug in of views

May explicitly load views into containers in the UI

Shell Implementation

Just a normal Silverlight UserControl or WPF Window

Set as RootVisual in Silverlight, Application.MainWindow in WPF

Defines the overall layout for the application

May contribute styles and templates that flow down to child view through resources

Add regions for views to be added by modules

9/16/2010

7©2010 Brian Noyes, IDesign Inc. All rights reserved 

Views

Composite parts (Legos) of your UI

Used to decompose your window from one big monolithic blob into a bunch of semi-autonomous parts

Can be defined as: User control

Custom control

Silverlight Data Template

XAML Resource

Dynamically constructed UI Element tree

Ultimately in Silverlight and WPF: A UIElement with some backing logic

Views

9/16/2010

8©2010 Brian Noyes, IDesign Inc. All rights reserved 

Services

Common abstraction in composites

Does not (typically) mean Web or WCF services

Application (in-proc) service might be point of encapsulation for the proxy that calls an external service

Provide shared services across the application Modules and shell

Typically singleton instance model

Container provides the glue

Services

Prism Provided: Logging

Region Manager

Event Aggregator

Module Catalog

Module Manager

Navigation Service (Prism 4)

Custom Entity data services

Authentication

Caching

9/16/2010

9©2010 Brian Noyes, IDesign Inc. All rights reserved 

Modules

Unit of composition for the application As opposed to views as the unit of composition for the UI

Modules contain the artifacts for a portion of your application Self-contained

Decoupled

Reusable

Typically defined as a Visual Studio project / assembly

Can have more than one module per assembly Avoid

Modules

Module class: Initialize the objects in the module

Like a Main() method for a library

Known entry point in the module

Initializes Container types

Views

Regions

Services

Controllers

Etc

9/16/2010

10©2010 Brian Noyes, IDesign Inc. All rights reserved 

Module Loading

Prism supports: Statically from code

Dynamically Module catalog XAML

Config file or directory scan in WPF

On-demand

Modules can be dependent on other modules Need to resolve load order in that case

Modularity Implementations

Two implementations in Prism 4:

Unity container

Managed Extensibility Framework (MEF)

Either/or decision

Same implementation patterns

Difference is your use of dependency injection coding patterns in the module code

9/16/2010

11©2010 Brian Noyes, IDesign Inc. All rights reserved 

Regions

Placeholder (named location) for view containment

Place to plug in views

Views dynamically added by app/module code

Prism 2 supports two approaches:

View Discovery

View Injection

Can be defined by the shell or a composite view

Shell – almost always

Composite View – less often

Regions

9/16/2010

12©2010 Brian Noyes, IDesign Inc. All rights reserved 

Region Manager

Prism service

Registration point for named regions

Modules get a region references from the region manager

Use the region to add their views

Command Pattern

9/16/2010

13©2010 Brian Noyes, IDesign Inc. All rights reserved 

Prism Commands

Based on WPF/Silverlight ICommand interface

Gets the handling out of the visual tree

Handlers can be view models or controllers

Breaks the dependency on focus and event routing in the visual tree

Allows multiple targets

Composite Commands

Submit

OrderDetails

Submit

OrderDetails

Submit

OrderDetails

Submit All

Delegate Commands

Composite Command

Submit All

9/16/2010

14©2010 Brian Noyes, IDesign Inc. All rights reserved 

Prism Events

Address different scenarios than .NET Events or WPF/Silverlight Routed Events

Based on pub/sub and event aggregator patterns

Decouples publishers and subscribers Type

Lifetime

Prism provides IEventAggregator service

Get event from aggregator

Subscribe or publish

Handles weak references

Handles threading issues

Provides filtering capability

Allows communications between loosely coupled, non-visual parts Presenters and controllers

Event Aggregation

EventAggregatorService

OrderReceived

OrderManagerOrderManager

OrderListPresenter

Subscribes

OrderModuleOrderModule

OrderService

Publishes

Receives

9/16/2010

15©2010 Brian Noyes, IDesign Inc. All rights reserved 

IActiveAware

Infrastructure for determining “Active” view

Can mean different things based on: Container control

State of view

Region adapters can set IsActive flag on views within a Selector region

Views (or ViewModels) can use to set IsActive flag on DelegateCommand

CompositeCommand can monitor active status of child commands

MVVM Guidance

Documentation and samples on implementing the MVVM pattern

Basic QuickStart, full QuickStart, and Reference Implementation sample

Examples of:

Static hook up of view and view model from the view

Dynamic hook up of view and view model through data templates

ViewModel base class to encapsulate common concerns

“Wrapping” model properties to add property change and/or validation implementation

Exposing model properties when model supports needed interfaces

View model support for view that is not part of the model

Behaviors to communicate between view and view model

9/16/2010

16©2010 Brian Noyes, IDesign Inc. All rights reserved 

Navigation Guidance

State-based Navigation

Based on the Visual State Manager

Small scale application or portions of an application

Use state transitions to “switch” views from a user perspective Really just state transitions within a single view definition at a code

level

Navigation Guidance

Region-based Navigation

Use URIs to indicate logical views that you want to display/navigate to

Navigation service translates those URIs into view switching within a region

Journaling of where you have been with forward/back nav

Potential for deep linking in Silverlight

Integration with Silverlight Navigation framework

Allows views to decide whether to allow navigation in a standard way i.e. unsaved work

9/16/2010

17©2010 Brian Noyes, IDesign Inc. All rights reserved 

Patterns in CAL

Uni

ty /

ME

FHost Application

Shell

RegionRegion

Modules

Prism LibraryServices

Adapter

CompositeView

ServiceLocator

DependencyInjection

InversionOf Control

View

ViewModel

Controller

Module

Repository

PresentationModel

Plug In

Façade ApplicationController

Repository

UIComposition

Modularity

Events

Commands

SeparatedInterface

Registry

EventAggregator

Command