Transcript Slide 1
This deck (and sample code) was used for several presentations of this talk: • NH Code Camp (28-Feb-2009) • Beantown .NET User Group (05-Mar-2009) • Boston Code Camp 11 (28-Mar-2009) Prism is 100% Resume Compliant! • Fancy design patterns like Model-View-ViewModel • Test-Driven Development (TDD) • Loosely Coupled • Separation of Concerns (SoC) • Interface-based Programming • Dependency Injection (DI) • Inversion of Control (IoC) Containers • Code-Behind minimalist • Modular, Composite Application • Distributed Agile Team • Reverses baldness • Fluent Interfaces • Multi-Targeting Silverlight and WPF • Routed Commands & Routed Events • High Performance Dev • Increases your IQ 42 points • Data Binding • Architect (not Astronaut) Introduction to Prism Building Silverlight and WPF apps with Composite Applications Guidance and Composite Application Library Bill Wilder http://blog.codingoutloud.com What is this talk about? • Targeting Silverlight and/or WPF – Managing complexity well – Best practices for Testability and design patterns • Focus (mostly) around UI concerns – XAML, Code-behinds, Data Binding • Doing all of the above Using Prism – Runtime Library + Guidance + Project Linker Assumptions: You have heard of… • Windows Presentation Foundation (WPF), Silverlight, XAML • Unit Testing (nUnit, …), TDD • Dependency Injection (DI), Inversion of Control (IoC), Loose coupling • UI Design Patterns (MVC, MVP, …) Where are YOU coming from? Goals of this Talk • Appreciate value of Prism for SL and WPF • Demonstrate a few key Prism concepts Side effects: • A little WPF, Silverlight; discuss a couple of Design Patterns / Anti-Patterns Rules of engagement: • Ask questions any time Terminology • Prism code name (which I will call it) • Prism v2 talking about 2nd release • Composite Application Guidance for WPF and Silverlight official name of whole package • Composite Application Library for WPF and Silverlight runtime support included – sometimes called “CAL” What’s the problem? • US businesses annually spend ~ $250 billion on software dev across approx 175,000 projects • Only 16 percent of these projects finish on schedule and within budget • Another 31 percent are cancelled, mainly due to quality problems • Another 53 percent exceed their budgets by an average of 189 percent • Projects reaching completion deliver an average of only 42 percent of the originally planned features [Credit: Jack Greenfield, Microsoft] Industry Response Where did Prism come from? P&P = Patterns & Practices Group microsoft.com/patterns Product Groups • • • • • Long cycle times Strategic Very large teams Cost money Fully Supported Windows, Office, Exchange, Visual Studio, .NET, Sharepoint, Zune, ... P&P Group • Rapid cycle times • Small, focused agile teams – Partner with community • Ship tactical solutions now • EntLib, Unity, Arch Guide • CAB, Prism v1, Prism v2 – Code and Guidance • Free, Open Source • More latitude, less legacy Demo Stock Trader RI (RI = Reference Implementation) Prism Key Concepts Eye Chart [Source CAL docs] Shell, Regions, Views • • • • Shell is main window for app Shell defines (visual) Regions Regions control where UI will appear – flexible Views are displayed within Regions • WPF/Silverlight design, code, tooling still applies – Prism just makes it better Views and Modules • Modules are DLLs (Projects) – Nice unit of work • You can decide when/whether to load • Modules can be configured to load code, XML, or XAML • Modules can be downloaded (over http) – Can write your own loader rules • Modules have Views (usually) Events • .NET events – Tight coupling (references in both directions) • WPF events – Looser coupling, (generally) limited to same visual tree • Composite events – Looserer coupling; not limited to same visual tree • More: Force to UI thread; event filtering • [Related to Commanding] Prism “Conventions+” • • • • Bootstrapper Shell One module per DLL M-V-VM pattern • Don’t need to follow the conventions… • But you’ll be glad you did Prism is (relatively) Small Silverlight WPF Prism First Cut At “SaveAsPodcast” App Look at some code… Why Separate Concerns in UI? • Optimizes Developer / Designer interaction – Dev in Visual Studio, Designer in Expression Blend • Easier to Test – Now possible to Unit Test (vs. Integration Test) – Simpler problem isolation • Reuse opportunity goes way up – Same ViewModel can be reused with UI variations • Better organized code is better code – Easier to understand, maintain – SE Principles: SoC, coupling, cohesion, SRP (rendering, mouse & keyboard, disabled, hover) How to Separate Concerns in UI? • • • • • Use Code-Behinds reluctantly + M-V-VM M-V-VM Model-View-ViewModel Pattern Abbreviated as M-V-VM, MVVM Often referred to simply as ViewModel Specialization of Fowler’s Presentation Model pattern where View knows about ViewModel – http://martinfowler.com/eaaDev/PresentationModel.html • Prism docs refer to as Presentation Model • I will refer to as ViewModel pattern ViewModel Pattern Load external data from web services, etc. View Data Binding ViewModel Your Glue Model Supported by WPF / SL Powerful Data Binding glue provided free with both Silverlight and WPF Custom code (boring); Automapper is promising Code-Behind Pattern (Old Way) Load external data from web services, etc. View Your Other Glue Model Code Behind for each Control Supported by WPF / SL UI “is” the data store for much of the data (e.g., ListBox); logic intertwined Custom code to populate UI Port “SaveAsPodcast” to Prism M-V-VM Modules Eventing Event Filtering Prism Rough Edges • You build it; P&P does not (yet) ship a binary • No Prism-specific templates for Visual Studio • Cannot unload a Module (MEF?) When to go Composite? (1/2) • You are designing “complex” WPF or Silverlight applications, or… • You are building an application that presents information from multiple sources through an integrated user interface, or… • You are developing, testing, and deploying modules independently of other modules, or… [Source CAL docs] When to go Composite? (2/2) • Your app will add more views and more functionality over the coming years, or… • You must be able to change the app quickly and safely to meet emergent business requirements, or… • Your app is being developed by multiple collaborating teams; or… • Your app targets both WPF and Silverlight, and you want to maximize code sharing between platforms. [Source CAL docs] When to NOT go Composite? • Your apps do not require any of the above scenarios, or… • Your application consists of a few simple screens, or… • You are building a prototype or demo, or… • Your developers are not familiar with the ideas and practices and do not have the time to learn them. * COMPLEMENTARY * (remember the opening slide) [Source CAL docs] Why Prism • SoC + SRP + reducing plumbing code + Unit Tests best way to handle complexity and enable ability to respond rapidly to requirement changes • M-V-VM best way to support the previous item (in SL/WPF) • Prism best way to support the previous item Official Site http://microsoft.com/compositewpf -- or -- http://codeplex.com/compositewpf Watch these spaces • “Contrib” site @ http://compositewpfcontrib.codeplex.com/ – Assorted contributions for Prism (currently v1) • Channel 9 for tutorial videos – http://channel9.msdn.com/tags/Prism/ • Infragistics control adapters for Prism @ http://ncal.codeplex.com/ – Region Adaptors for Prism (currently v1) Prism v2 References • http://www.microsoft.com/downloads/details.aspx?FamilyI D=fa07e1ce-ca3f-4b9b-a21be3fa10d013dd&DisplayLang=en • http://blogs.msdn.com/jmeier/archive/2009/02/18/newrelease-composite-application-guidance-for-wpf-andsilverlight-v2-0-prism.aspx • http://timheuer.com/blog/archive/2009/02/18/prism-2released-composite-guidance-for-silverlight-lob.aspx • http://msdnrss.thecoderblogs.com/2009/02/18/compositeapplication-guidance-for-wpf-and-silverlight-v20-prism/ • http://tinyurl.com/d4s22b Other Resources • Josh Smith on MVVM with WPF: http://msdn.microsoft.com/enus/magazine/dd419663.aspx • Brian Noyes on Understanding Routed Events and Routed Commands in WPF: http://msdn.microsoft.com/enus/magazine/cc785480.aspx • WPF Commanding Overview: http://msdn.microsoft.com/en-us/library/ms752308.aspx • Martin Fowler’s description of Presentation Model pattern: http://martinfowler.com/eaaDev/PresentationModel.html • XAML Guidelines for Creating a Composite UI: http://msdn.microsoft.com/en-us/library/dd458877.aspx Did I get to this? • What would happen if I ran the Synchronous retrieval of the Podcast feed from Silverlight? • Show the Prism docs Silverlight for .NET • Challenge #1: not binary compatible with runtimes that run on Windows desktop – But it is source compatible • Challenge #2: only a subset of runtime is implemented on Silverlight – But most of the stuff that “makes sense” is there (subset, security, useful, choose 1, async, cross-domain) • Challenge #3: XAML not same as WPF – But is converging on mostly a subset • Other miscellaneous differences… • Suggestion: Write 1st in Silverlight, then port to WPF Questions?