1 / 44

Silverlight using Prism v4

Introduction to Architecting Silverlight Applications with Prism v4. Silverlight using Prism v4. Alan Cobb Independent .NET Consultant Sacramento, CA Silicon Valley Code Camp 2010 Sunday, October 10, 2010, 1:15pm (c) 2010 Alan Cobb, All rights reserved. About Alan Cobb.

Samuel
Download Presentation

Silverlight using Prism v4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Architecting SilverlightApplications with Prism v4 Silverlight using Prism v4 Alan Cobb Independent .NET Consultant Sacramento, CA Silicon Valley Code Camp 2010 Sunday, October 10, 2010, 1:15pm (c) 2010 Alan Cobb, All rights reserved.

  2. About Alan Cobb • Independent consultant for over 20 years on Microsoft platforms • Silverlight, WPF, .NET, C#, C++ • One of directors of the SacDotNet User Group • http://www.alancobb.com • http://www.alancobb.com/blog

  3. Presentation Overview • Introduction: • What is common goal of Prism? • Prism features: • Dependency Injection: Unity / MEF • UI-Regions • Modularity • MVVM (Model-View-ViewModel) • Commanding, Loosely-coupled-events • Analyzing Prism code samples

  4. Anti-pattern: Big Ball of Mud Problems: Hard to see structure. Too many connections.

  5. Anti-pattern: Big Ball of Mud Problems: Hard to see structure. Too many connections

  6. Overall Prism Goals • Common goals behind Prism’s features: • Better ways to divide your app into parts • Modularity • UI-Regions • Better ways to find and connect the parts • Dependency-Injection (DI) with Unity or MEF • Better ways to communicate between parts • Commanding • Loosely coupled events

  7. Goal: Clearer structure & connections Benefits: Easier to understand, fix, extend and test.

  8. Analogy to an optical prism • Prism helps break up an app into components. A little like a prism breaking up white light.

  9. Composite application

  10. Composite application

  11. What do you get with Prism? • Documentation (“guidance”) • Libraries with source code: • Prism • Unity or MEF (both optional) • Blend’s “Interactivity” assemblies • Samples • Discussion forum

  12. Downloading Prism

  13. Prism directory structure

  14. Prism version history • CAB: Composite Application UI Block • WinForms only – 2005 • Prism v1 – 2008-06 • WPF only • Prism v2.2 – 2010-06 • Prism v4 – 2010-10 • MEF introduced. MVVM support better.

  15. Prism namespaces (old / new)

  16. Block diagram of a Prism app

  17. Steps to creating a Prism app

  18. StockTrader RI sample

  19. Topic: UI “Regions” • Sub-area of screen where views displayed • Like ASP.NET master pages • Decouples views from where they are shown or composed at a higher level

  20. StockTrader RI sample

  21. What is Dependency Injection (DI)? • Dependency Injection has similarities to a LAN

  22. Topic: Dependency Injection • Dependency injection (DI) concepts • Containers • Registering and Resolving • Constructor and property injection • Lifetime management (singleton vs. instance)

  23. Topic: Dependency Injection • Prism v4’s dependency injection choices: • Prismized-Unity • Prismized-MEF • MEF = Managed Extensibility Framework • Samples: “StockTrader RI”, “ModularityWithMef” • Native-MEF • Samples: “MVVM RI” • Other containers (StructureMap, etc.) • None

  24. Goal: find and connect to other “parts”

  25. Details of a hard reference

  26. Looser coupling with interfaces

  27. Even looser coupling Dependency Injection (DI)

  28. DI demo: HelloWorld Quickstart

  29. Topic: Modularity • What is a Prism “module”? • Normally: One Project / Assembly / DLL • Possibly in separate XAP file • Initializing WhenAvailable or OnDemand • Using Prism modules: • Implementing IModule and IModule.Initialize() • ModuleCatalog in XAML or code. • StockTrader-RI no longer uses “modules”, just MEF.

  30. ModularityWithMef sample

  31. ModularityWithMef sample details • Using Fiddler to watch XAP downloads • MEF concepts in general: • Import, Export attributes and Contracts • Catalogs and Container • Compose, Recompose • MEF usage in sample: • [Import], [Export] • [ModuleExport] • IPartImportsSatisfiedNotification . OnImportsSatisfied

  32. Topic: MVVM • Model-View-ViewModel

  33. MVVM – Prism samples • Quickstart – BasicMVVM (will cover) • Only uses DelegateCommand from Prism • Quickstart – MVVM (will cover a bit) • No MEF/Unity or regions. Uses: Behaviors. • Quickstart – RegionNavigation (won’t cover) • MVVM RI (Reference Implementation) • Uses MVVM, native-MEF • StockTrader-RI • Uses MVVM, Prismized-MEF

  34. MVVM: The main idea Benefits: Easier to understand, fix, extend and test.

  35. BasicMVVM Quickstart sample

  36. MVVM Quickstart sample

  37. MVVM Quickstart details • View’s DataContext set to ViewModel • NotificationObject (ViewModel base class) • VM can implement INotifyDataErrorInfo to support validation. • Uses “Behaviors” (behavior attached properties) • UpdateTextBindingOnPropertyChanged (Prism) • CallMethodAction (Blend. Calls method on VM.) • DataTrigger, EventTrigger and GoToStateAction (Blend. VM drives view to given VSM state.)

  38. Topic: Eventing • Loosely-coupled events • Many-to-many, publish and subscribe • Communicate “loosely” between modules, between view-models. • Can use regular .NET events inside modules. • EventAggregator class (Singleton) • Alternative: Rx – Reactive Extensions

  39. EventAggregation sample

  40. EventAggregation sample

  41. Topic: Commanding • Command types offered by Prism: • DelegateCommand • Implements ICommand. • CompositeCommand • Ways of using: • Local commands (View to ViewModel) • Global commands

  42. Commanding sample

  43. Topic: Multi-targeting • Goal: Support both WPF and Silverlight • Goal: Share as much code as possible • Source file “Linking” within VS • Partial classes • #if #else conditional-compile blocks

  44. Alternatives to Prism • MVVM-Light Toolkit • Supports MVVM, loose events and commands. • No DI (Unity, MEF) / modularity • No UI-Regions • Similar to Prism v4’s “MVVM RI” sample • Others • Caliburn, … • Roll-your-own

More Related