silverlight 2 for developers - teched new zealand 2008

51

Upload: jonas-folleso

Post on 07-Dec-2014

4.768 views

Category:

Technology


0 download

DESCRIPTION

The slides from the WEB308 Silverlight 2 for Developers talk at TechEd New Zealand 2008

TRANSCRIPT

Page 1: Silverlight 2 for Developers - TechEd New Zealand 2008
Page 2: Silverlight 2 for Developers - TechEd New Zealand 2008

2

Silverlight 2 for Developers

Jonas FollesøSenior ConsultantCapgemini

Session Code: WEB304

Page 3: Silverlight 2 for Developers - TechEd New Zealand 2008

3

Page 4: Silverlight 2 for Developers - TechEd New Zealand 2008

4

Agenda

Blend 2.5CRUDDesignersPatternsTestingQ&A

Page 5: Silverlight 2 for Developers - TechEd New Zealand 2008

5

Dive Log App

Page 6: Silverlight 2 for Developers - TechEd New Zealand 2008

6

Outside data

PublicPublicInternetInternetmashup APIsmashup APIs

ExistingExistingIntranetIntranetservicesservices

New servicesNew servicesyou buildyou build

RSS/AtomRSS/AtomFeedsFeeds

ImagesImagesSoundsSoundsVideosVideos

Page 7: Silverlight 2 for Developers - TechEd New Zealand 2008

7

A bit of history: Silverlight 1

PublicPublicInternetInternetmashup APIsmashup APIs

ExistingExistingIntranetIntranetservicesservices

New servicesNew servicesyou buildyou build

RSS/AtomRSS/AtomFeedsFeeds

ImagesImagesSoundsSoundsVideosVideos

JavaScript

HTML

?AJAX (XmlHttpRequest)

1.0<XAML/>

Page 8: Silverlight 2 for Developers - TechEd New Zealand 2008

8

Today: Silverlight 2

2

Managed Code (C#/VB)

PublicPublicIntInteernetrnetmashup APIsmashup APIs

ExistingExistingIntrIntraanetnetservicesservices

New servicesNew servicesyou buildyou build

RSS/AtomRSS/AtomFeedsFeeds

ImagesImagesSoundsSoundsVideosVideos

HTML

Page 9: Silverlight 2 for Developers - TechEd New Zealand 2008

9

Services that describe themselves

SOAPSOAPservices in the services in the enterpriseenterprise

Services for Services for your Silverlightyour Silverlightprojectproject

ADO.NET ADO.NET Data ServicesData Services

SOAP SOAP servicesserviceson the Interneton the Internet

Computer-ReadableMetadata

(e.g. WSDL)

AutomaticProxy

Generation

WCF

Page 10: Silverlight 2 for Developers - TechEd New Zealand 2008

10

Securing Silverlight ServicesSilverlight will use auth. info in the browser

HTML

E.g.: ASP.NET Login

User:Password:

DiveLog.comDiveLog.com…/Login.aspx…/Login.aspx../MyApp.aspx../MyApp.aspx../MyService.svc../MyService.svc

Credentials

Auth info (e.g. cookie)

Service calls + Auth info

Silverlight code does not normallydeal with credentials…

Page 11: Silverlight 2 for Developers - TechEd New Zealand 2008

11

HTTP Requests in Silverlight

HttpWebRequestHttpWebRequest

High-level components and User CodeHigh-level components and User Code

Browser Plugin APIsBrowser Plugin APIs

Web Browser- Cookies- Authenticated sessions- Caching- Proxy server to use

Web Browser- Cookies- Authenticated sessions- Caching- Proxy server to use

Windows/MacNetworking LayerWindows/MacNetworking Layer

Restrictions

Restrictions

Page 12: Silverlight 2 for Developers - TechEd New Zealand 2008

12

Blend – The dark is not that scary…

Page 13: Silverlight 2 for Developers - TechEd New Zealand 2008

13

Dive Log UI and Data

Page 14: Silverlight 2 for Developers - TechEd New Zealand 2008

14

Everything in code behind…is probably not a good idea

Page 15: Silverlight 2 for Developers - TechEd New Zealand 2008

15

A team of sad coders and designers

Page 16: Silverlight 2 for Developers - TechEd New Zealand 2008
Page 17: Silverlight 2 for Developers - TechEd New Zealand 2008

17

Different people reading about MVC in different places take different ideas from it and describe these as “MVC”.

Martin Fowler, GUI Architectures Essay

Page 18: Silverlight 2 for Developers - TechEd New Zealand 2008

18Separated Presentation Patterns

Page 19: Silverlight 2 for Developers - TechEd New Zealand 2008

19

Data & Domain Data & Domain LogicLogic(Model)(Model)

UIUI(View)(View)

Interaction Interaction (Controller/Presenter)(Controller/Presenter)

Page 20: Silverlight 2 for Developers - TechEd New Zealand 2008

20

Represent the state and behavior of the presentation independently of the GUI controls used in the interface.

Martin Fowler, Presentation Model Essay

Page 21: Silverlight 2 for Developers - TechEd New Zealand 2008

21

Makes your app easier to design…makes the designers like you

Page 22: Silverlight 2 for Developers - TechEd New Zealand 2008

22

Everything in code-behind

Data ModelData Model

ViewView

XAMLXAML

Code-BehindCode-BehindEvent HandlersEvent Handlers

Page 23: Silverlight 2 for Developers - TechEd New Zealand 2008

23

Presentation Model

Data ModelData Model

ViewView

XAMLXAML

Code-BehindCode-Behind

Presentation ModelPresentation Model

State + State + OperationsOperations

Change notification

Data-binding and commands

Page 24: Silverlight 2 for Developers - TechEd New Zealand 2008

24

Dive Log Presentation Model

Dive Log ServiceDive Log Service

Dive LogDive Log

XAMLXAML

Code-BehindCode-Behind

Dive Log View Dive Log View ModelModelState + State +

OperationsOperations

Change notification

Data-binding and commands

Page 25: Silverlight 2 for Developers - TechEd New Zealand 2008

25

Data BindingImplement INotifyPropertyChanged and use ObservableCollection<T> for collections

ViewView <ListBox ItemsSource="{Binding Path=Dives}" SelectedItem="{Binding Path=SelectedDive, Mode=TwoWay}" />

Presentation ModelPresentation Model

State + OperationsState + Operations

Page 26: Silverlight 2 for Developers - TechEd New Zealand 2008

26

Data BindingImplement INotifyPropertyChanged and use ObservableCollection<T> for collections

ViewView

XAML

Code-BehindCode-Behind

Presentation ModelPresentation Modelpublic class PageViewModel : INotifyPropertyChanged{ public event PropertyChangedEventHandler PropertyChanged; public ObservableCollection<Dive> Dives { ... } public Dive SelectedDive { ... }}

Page 27: Silverlight 2 for Developers - TechEd New Zealand 2008

27

IValueConverter

Converts data bound valuesE.g. Bind temperature to color

public object Convert(object, value, Type targetType, object parameter, CultureInfo culture){ int temp = System.Convert.ToInt32(value); Color color = Colors.Black; if(temp < 5) color = Colors.Blue; else if(temp < 20) color = Colors.Yellow; else color = Colors.Red;

return new SolidColorBrush(color);}

ViewView<UserControl.Resources> <demo:ColorConverter x:Key="colorConverter" /></UserControl.Resources>

<TextBox Text="{Binding Temperature, Mode=TwoWay}" Background="{Binding Temperature, Converter={StaticResource colorConverter}}"/>

Page 28: Silverlight 2 for Developers - TechEd New Zealand 2008

28

Building a Presentation Model

Page 29: Silverlight 2 for Developers - TechEd New Zealand 2008

29

User Interaction

But what about Word?

ViewViewprivate void btnSave_Clicked(object sender, ExecutedEventArgs e){ ((PageViewModel)DataContext).Save();}

Page 30: Silverlight 2 for Developers - TechEd New Zealand 2008

30

Objects are used to represent actions. A command object encapsulates an action and its parameters.

This allows a decoupling of the invoker of the command and the handlers of the command.

Page 31: Silverlight 2 for Developers - TechEd New Zealand 2008

31

Commands in SilverlightViewView

<Button Content="Save Dives" input:CommandService.Command="SaveDives" />

Presentation ModelPresentation Modelpublic PageViewModel(){ Commands.SaveDives.Executed += new EventHandler<ExecutedEventArgs>(SaveDives);}

private void SaveDives(object sender, ExecutedEventArgs e){ // code to save dives..}

Page 32: Silverlight 2 for Developers - TechEd New Zealand 2008

32

The strategy pattern is a software design pattern, whereby algorithms can be selected at runtime.

Presentation ModelPresentation Modelpublic PageViewModel(){ if(HtmlPage.IsEnabled) { client = new DiveLogServiceClient(); } else { client = new ServiceStub(); }}

Page 33: Silverlight 2 for Developers - TechEd New Zealand 2008

33

Page 34: Silverlight 2 for Developers - TechEd New Zealand 2008

34

Page 35: Silverlight 2 for Developers - TechEd New Zealand 2008

35

Dealing with dependencies

The Presentation Model is coupled with both the stub service and the real service.

Makes code less flexible for changeMakes code harder to test

Object should not be responsible for creating their own dependencies – Inversion of Control

Page 36: Silverlight 2 for Developers - TechEd New Zealand 2008

36

Dependency Injection (DI)

One form of Inversion of Control (IoC) Create dependencies outside the object and, inject them into it

But who creates the dependency?

Presentation ModelPresentation Modelpublic PageViewModel(IDiveLogServiceClient proxy){ this.proxy = proxy}

Page 37: Silverlight 2 for Developers - TechEd New Zealand 2008

37

Dependency Injection by hand

Should the page be responsible for creating dependencies?

ViewViewpublic Page(){ InitializeComponent(); if (HtmlPage.IsEnabled) this.DataContext = new PageViewModel(new DiveLogServiceClient()); else this.DataContext = new PageViewModel(new ServiceStub());}

Page 38: Silverlight 2 for Developers - TechEd New Zealand 2008

38

IoC Containers

Presentation ModelPresentation Model[Inject]public PageViewModel(IDiveLogServiceClient proxy){ this.proxy = proxy}

ViewViewpublic Page(){ InitializeComponent(); IKernel iocContainer = new StandardKernel(); this.DataContext = iocContainer.Get<PageViewModel>();}

Page 39: Silverlight 2 for Developers - TechEd New Zealand 2008

39

IoC ContainersWhat about design time support in Blend?

ViewView<UserControl.DataContext> <dive:PageViewModel /></UserControl.DataContext>

Need to find a way to use IoC and set DataContext declaratively…

Page 40: Silverlight 2 for Developers - TechEd New Zealand 2008

40

Using DI on the ViewModel

Page 41: Silverlight 2 for Developers - TechEd New Zealand 2008

41

Testing is important!

Page 42: Silverlight 2 for Developers - TechEd New Zealand 2008

42

Testing Silverlight Code

The standard solutions do not applyThe Silverlight CLR is a subsetAll networking is asynchronous

But it’s still familiar…[TestClass], [TestMethod], [Asynchronous]

Page 43: Silverlight 2 for Developers - TechEd New Zealand 2008

43

Asynchronous TestsHow do we “wait” before doing assertions?

Page 44: Silverlight 2 for Developers - TechEd New Zealand 2008

44

Unit Testing in Silverlight

Page 45: Silverlight 2 for Developers - TechEd New Zealand 2008

45

Page 46: Silverlight 2 for Developers - TechEd New Zealand 2008

46

Resources

www.microsoft.com/teched Tech·Talks Tech·Ed BloggersLive Simulcasts Virtual Labs

http://microsoft.com/technet

Evaluation licenses, pre-released products, and MORE!

http://microsoft.com/msdn

Developer’s Kit, Licenses, and MORE!

Page 47: Silverlight 2 for Developers - TechEd New Zealand 2008

Related Content

Code and lots of good resources on http://jonas.follesoe.noCode and lots of good resources on http://jonas.follesoe.no

WEB311 Designing Compelling Silverlight User Experiences with Expression Studio 2WEB311 Designing Compelling Silverlight User Experiences with Expression Studio 2Wednesday, 3-9-2008 14:20 - 15:35, NZ Room 4, SkyCityWednesday, 3-9-2008 14:20 - 15:35, NZ Room 4, SkyCity

Hands-on Labs (session codes and titles)Hands-on Labs (session codes and titles)

Page 48: Silverlight 2 for Developers - TechEd New Zealand 2008

Track ResourcesWEB311 Designing Compelling Silverlight User Experiences with Expression Studio 2WEB311 Designing Compelling Silverlight User Experiences with Expression Studio 2Wednesday, 3-9-2008 14:20 - 15:35, NZ Room 4, SkyCityWednesday, 3-9-2008 14:20 - 15:35, NZ Room 4, SkyCity

claireh
Place Holder
Page 49: Silverlight 2 for Developers - TechEd New Zealand 2008

49

Please Please complete ancomplete anevaluationevaluation

Page 50: Silverlight 2 for Developers - TechEd New Zealand 2008

50

© 2008 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 51: Silverlight 2 for Developers - TechEd New Zealand 2008

51

Photo Copyright Notices

All diving photos taken by Hege Røkenes and licensed under the creative commons license. http://flickr.com/photos/hegerokenes/

Photos of Martin Fowler taken by Dave Thomas and licensed under the creative commons license.http://flickr.com/photos/pragdave/