440 likes | 911 Views
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.
E N D
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.
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
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
Anti-pattern: Big Ball of Mud Problems: Hard to see structure. Too many connections.
Anti-pattern: Big Ball of Mud Problems: Hard to see structure. Too many connections
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
Goal: Clearer structure & connections Benefits: Easier to understand, fix, extend and test.
Analogy to an optical prism • Prism helps break up an app into components. A little like a prism breaking up white light.
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
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.
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
What is Dependency Injection (DI)? • Dependency Injection has similarities to a LAN
Topic: Dependency Injection • Dependency injection (DI) concepts • Containers • Registering and Resolving • Constructor and property injection • Lifetime management (singleton vs. instance)
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
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.
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
Topic: MVVM • Model-View-ViewModel
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
MVVM: The main idea Benefits: Easier to understand, fix, extend and test.
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.)
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
Topic: Commanding • Command types offered by Prism: • DelegateCommand • Implements ICommand. • CompositeCommand • Ways of using: • Local commands (View to ViewModel) • Global commands
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
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