37
Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight John Papa Senior Technical Evangelist Microsoft Corporation CL22

Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

  • Upload
    morwen

  • View
    45

  • Download
    0

Embed Size (px)

DESCRIPTION

CL22. Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight. John Papa Senior Technical Evangelist Microsoft Corporation. Outline. MVVM Model – View – ViewModel What it does Where it works well Variations Prism Enabling MVVM Filling the gaps Options. Goals. - PowerPoint PPT Presentation

Citation preview

Page 1: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Advanced Topics for Building Large-Scale Applications with Microsoft SilverlightJohn PapaSenior Technical EvangelistMicrosoft Corporation

CL22

Page 2: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Outline> MVVM

> Model – View – ViewModel> What it does> Where it works well> Variations

> Prism> Enabling MVVM> Filling the gaps> Options

Page 3: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Goals> Understand MVVM’s Role in Silverlight

> Many ways to accomplish your goal> See it in Action> Learn about MVVM Enablers

> Prism> Roll Your Own

Page 4: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Another MC_ Acronym?> MVVM> MVC> MVP> MV????

> Commonly accepted patterns for maintainable, scalable applications

Page 5: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

MVVMThe What and Why> Separation of concerns> View = handles UI> Model = contains the pure data > ViewModel = communicates between

View and Model through bindings> Works GREAT for Silverlight and WPF

> XAML based data bindings> Testable

Page 6: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

The MVVM TriadPutting it Together

ViewModel

View

Model

> Display data in Controls

> UI Friendly Entities, UI State, Actions

> Entities representing data

Page 7: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

ModelMVVM> Represents the data> The entity> Not required to know where it gets its

data from> From a WCF service. WCF RIA Services,

etc> May contain validation

Page 8: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

ViewMVVM> The screen, the UI, the UserControl in

Silverlight> Handles UI look and feel> Presentation of information> Communicates with ViewModel

through bindings

Page 9: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

ViewModelMVVM> Main source of logic for the MVVM triad> Connects the Model to the View> Abstracts the View> Public properties that are bound to a View> INotifyPropertyChanged and

INotifyCollectionChanged talk to the View through bindings

> Listens for changes from the View through bindings

> Invokes services to communicate outside the MVVM triad

Page 10: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Which Way Do I Go?MVVM Variations> View must be paired with a ViewModel

somehow> Several workable options> Maintain the spirit of each of the

components of the MVVM triad

Page 11: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

View First MVVM Variations> ViewModel is declared as a Static

Resource in the View’s XAML> Works well in Expression Blend

> Another way is to create ViewModel in the View’s code-behind

Page 12: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

View First MVVM

demo

Page 13: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

ViewModel FirstMVVM Variations> View is injected into the ViewModel’s

constructor > Example:

> ViewModel is created then the View is created using Dependency Injection

Page 14: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

View and ViewModel MarriageMVVM Variations> View must be paired with a ViewModel

somehow> ViewModel and View are created

through an intermediary, then paired together

Page 15: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Marrying the View to the ViewModelvm = new MyVM();view = new MyView();view.DataContext = vm;

// With Unityvm = container.Resolve<IMyVM>();view = container.Resolve<MyView>();

view.DataContext = vm;

Page 16: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Prism 2Be Picky> Prism is a set of options> Use what you want and ignore the rest

> Example: > Choose Modules and Commanding, but ignore

Event Aggregation and Regions

Page 17: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Prism Technical Concepts

Container

Commands

Bootstrapper

Regions

Modules

Event Aggregation

Unity and DI

Shell

Page 18: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

BootstrapperLet’s Get this Party Started!> Kicks off the application> Starts the main UI container (the

Shell)> Registers Modules and loads, if

needed> Registers any global singletons

(optional)> Usually contained in the “Bootstrapper

project”

Page 19: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

ShellThe Main View> The main UI container> Houses all of the Views that will be

loaded> Can be split into Regions> Knows nothing of what will be loaded

into it

Page 20: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

RegionsContent Areas > Area(s) in the Shell where Views can

be placed> Given a name> Can contain context, if needed> RegionManager exists to help

maintain Regions

Page 21: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

ModularitySelf Contained Modules> To the user this is seamless> Developed separately> Do not reference other modules> Solution is split into Modules > Example:

> City government application> Module 1: Managing land parcels> Module 2: Traffic light administration> Module 4: City Parks

> Modules share infrastructure and Models

Page 22: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Dependency InjectionUsing Unity> Unity or other DI Tools (Ninject)> Testability and mocking> Abstraction

> Container object allows classes to be registered against their interfaces

> When an interface is requested, the container creates the class registered with the interface

> Supports singletons, too

Page 23: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Register the Interface to the Class

container.RegisterType<IMyViewModel,

MyViewModel>();

Page 24: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Create the Concrete Class

container.Resolve<IMyViewModel>();

Page 25: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

InfrastructureCommon Tools> A Silverlight class library project> Contains shareable items for the

modules> Classes> Assets> Resources

> Makes no references> A pure library

Page 26: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

CommandingActions and Reactions> Allows events between a View and a

ViewModel through Data Binding> ViewModel declares the Command

receiver> Command is declarative in XAML

> Button - Click> ListBox (Selector) – Selected

> Command is data bound to the ViewModel’s command receiver

> Can be disabled/enabled based on rules

Page 27: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Event AggregatorPublisher and Subscriber > Allows events of any kind to be

published and subscribed to> Can be cross module> Can be filtered by subscribers> For example:

1. Click on a menu item in the Shell2. Event is invoked by the publisher3. Event is received by the subscriber4. The subscriber then loads a View in a

Region in the Shell

Page 28: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

InfrastructureCommon Tools> Infrastructure is a Silverlight class

library project> Contains classes, assets, resources

that are shared amongst the Modules> Does not reference any modules> Does not make calls to web services> It’s a pure library

Page 29: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Modular MVVM and Prism

demo

Page 30: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Screen Presentation EnablerEnabling Screens> Must be able to navigate between

screens> Views do not know about each other> Roll Your Own Screen Conductor

> Loading/unloading> Displaying/hiding> Can I Leave?> Clean yourself up!

Page 31: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Screen Presentation FrameworkKey Players> Screen

> Manages the MVVM triad> ScreenFactory

> Creates a Screen class> ScreenFactoryRegistry

> A ScreenFactory directory> ScreenConductor

> Listens for & acts upon screen activation events

> ScreenCollection> Collects screens

Page 32: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Screen Conductor

demo

Page 33: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Resources> My blog: http://www.johnpapa.net > Twitter: @john_papa

> Prism from Patterns and Practices > http://www.codeplex.com/prism

> Article on the Screen Presentation Framework, MVVM and Prism > Coming soon to MSDN Online

Page 34: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

YOUR FEEDBACK IS IMPORTANT TO US! Please fill out session evaluation

forms online atMicrosoftPDC.com

Page 35: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

Learn More On Channel 9> Expand your PDC experience through

Channel 9

> Explore videos, hands-on labs, sample code and demos through the new Channel 9 training courses

channel9.msdn.com/learnBuilt by Developers for Developers….

Page 36: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Page 37: Advanced Topics for Building Large-Scale Applications with Microsoft Silverlight