Sie sind auf Seite 1von 17

9/16/2010

Prism Composite Application Guidance

Brian Noyes www.idesign.net

2010 Brian Noyes, IDesign Inc. All rights reserved

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

2010BrianNoyes,IDesignInc.Allrightsreserved

9/16/2010

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

2010BrianNoyes,IDesignInc.Allrightsreserved

9/16/2010

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

2010BrianNoyes,IDesignInc.Allrightsreserved

9/16/2010

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

2010BrianNoyes,IDesignInc.Allrightsreserved

9/16/2010

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

2010BrianNoyes,IDesignInc.Allrightsreserved

9/16/2010

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 Just provides a shell to contain them, thus the name

Typically knows nothing about the views that it is composed of

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

2010BrianNoyes,IDesignInc.Allrightsreserved

9/16/2010

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 A UIElement with some backing logic

Ultimately in Silverlight and WPF:

Views

2010BrianNoyes,IDesignInc.Allrightsreserved

9/16/2010

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) Entity data services Authentication Caching

Custom

2010BrianNoyes,IDesignInc.Allrightsreserved

9/16/2010

Modules

Unit of composition for the application

As opposed to views as the unit of composition for the UI Self-contained Decoupled Reusable

Modules contain the artifacts for a portion of your application


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

2010BrianNoyes,IDesignInc.Allrightsreserved

9/16/2010

Module Loading

Prism supports:

Statically from code Dynamically


Module catalog XAML Config file or directory scan in WPF

On-demand Need to resolve load order in that case

Modules can be dependent on other modules

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

2010BrianNoyes,IDesignInc.Allrightsreserved

10

9/16/2010

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

2010BrianNoyes,IDesignInc.Allrightsreserved

11

9/16/2010

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

2010BrianNoyes,IDesignInc.Allrightsreserved

12

9/16/2010

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
SubmitAll

Composite Command

OrderDetails

OrderDetails

OrderDetails

Submit

Submit

Submit

Delegate Commands

2010BrianNoyes,IDesignInc.Allrightsreserved

13

9/16/2010

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
EventAggregator Service OrderReceived

Receives Publishes Subscribes OrderModule OrderService OrderManager OrderListPresenter

2010BrianNoyes,IDesignInc.Allrightsreserved

14

9/16/2010

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

2010BrianNoyes,IDesignInc.Allrightsreserved

15

9/16/2010

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

2010BrianNoyes,IDesignInc.Allrightsreserved

16

9/16/2010

Patterns in CAL
Host Application Shell
Adapter

Unity / MEF

Prism Library Services


Registry UI Composition Separated Interface Modularity

Region
Composite View

Region

Dependency Injection

View Presentation Model ViewModel

Module Plug In

Inversion Of Control Service Locator Faade

Event Aggregator Events

Modules
Controller Application Controller Repository Repository

Command Commands

2010BrianNoyes,IDesignInc.Allrightsreserved

17

Das könnte Ihnen auch gefallen