Top Banner
Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager Enterprise Developer Camp Jumpstart
40

Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Dec 16, 2015

Download

Documents

Leslie Stewart
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Robert Green | Technical EvangelistDmitry Lyalin | Product Marketing Manager

Enterprise Developer Camp Jumpstart

Page 2: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Click to edit Master subtitle style

02 | Adopt a Services Architecture

Dmitry Lyalin | Product Marketing Manager

Page 3: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

• Our scenario

• Asynchronous programming

• Portable class libraries

• Inversion of control

• Model-View-ViewModel (MVVM)

• WCF & Web API

• OData

• We’re only touching on each of these—there is a lot more out there!

Module Overview

Page 4: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Click to edit Master subtitle style

Our Scenario

Page 5: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

• Typical line-of-business application

• Create & submit reports

• View past reports

• Approve reports (if manager)

Our scenario: expense reporting

Page 6: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Corporate Network

Application topography

Expenses DB(SQL)

Expenses services(WCF on Windows Server)

UI(WPF)

Activ

e D

irecto

ry

Page 7: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

DEMOExpenses App

Page 8: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Click to edit Master subtitle style

Async, PCLs, IoC

Page 9: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Expense reporting backlog• Improve architecture, maintainability, and quality– Adopt a services architecture

• Improve accessibility, scalability, and operations–Move to the cloud

• Update the user experience– Build a more modern-looking user experience

• Expand device support– Companion apps for Windows Store and Windows Phone

Page 10: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Asynchronous programming• Critical for responsive UI, scale, & user satisfaction

• Traditionally a challenge due to manual requirements

• APIs often inconsistent– Polling– Callbacks– Events– Begin/End

• Progress and cancellation often not supported

• Error reporting unpredictable

Page 11: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Introducing async & await• async makes your method asynchronous– Expects “await” in body– Only top-level methods (main, event handlers, etc) should be “async void”

• await makes the rest of your method a callback

Page 12: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Introducing async & await• Paradigm being applied across all Microsoft platforms–Microsoft internal guidelines recommend Async for any API that could take longer than 50ms to complete

• Enables consistent progress, cancellation, and error infrastructure– Note that API support for progress and/or cancellation itself varies

Page 13: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Using async APIs with await

FileOpenPicker picker = new FileOpenPicker();

picker.FileTypeFilter.Add(".jpg");

StorageFile file =

await picker.PickSingleFileAsync();

IRandomAccessStream stream =

await file.OpenAsync(...);

BitmapDecoder decoder =

await BitmapDecoder.CreateAsync(stream);

decoder. …

Page 14: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Exposing an async API• Return a Task or Task<T>

• “async” is not necessary, unless you are using “await” within the method

Page 15: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Building an async API examples

public Task<string> RequestDataAsync(Uri uri){ return Task.Run<string>( () => { WebClient webClient = new WebClient(); return webClient.DownloadString(uri); });} // Create Tasks when you have no other choice.

public Task<string> RequestDataAsync(Uri uri){ var tcs = new TaskCompletionSource<string>();

WebClient webClient = new WebClient(); webClient.DownloadStringCompleted += (_, args) => { tcs.SetResult(args.Result); }; webClient.DownloadStringAsync(uri);

return tcs.Task;} // Example of wrapping Async/Completed.

public async void Method(string[] args){ MyClass instance = new MyClass(); string result = await instance.RequestDataAsync(new Uri("..."));}

public Task<string> RequestDataAsync(Uri uri){ WebClient webClient = new WebClient(); return webClient.DownloadStringTaskAsync(uri);} // Rely on underlying Tasks whenever possible.

Page 16: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Additional async resources• Async'ing Your Way to a Successful App with .NET– http://channel9.msdn.com/Events/Build/2013/3-301

• Creating Async Libraries That Are Modular, Reusable and Fast, in Microsoft Visual C# and Visual Basic– http://channel9.msdn.com/Events/TechEd/Europe/2013/DEV-B318

Page 17: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Portable class libraries (PCLs)• One source

• One project

• One binary

• Multiple platforms!

Page 18: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

What can I use and where?

Feature .NET Framework

Windows Store

Silverlight

Windows Phone

Core √ √ √ √

LINQ √ √ √ √

IQueryable √ √ √ 7.5 and higher

Dynamic keyword 4.5 and higher

√ √

Managed Extensibility Framework (MEF)

√ √ √

Network Class Library (NCL) √ √ √ √

Serialization √ √ √ √

Windows Communication Foundation (WCF)

√ √ √ √

Model-View-View Model (MVVM) 4.5 and higher

√ √ √

Data annotations 4.0.3 and 4.5+

√ √

XLINQ 4.0.3 and 4.5+

√ √ √

System.Numerics √ √ √

Page 19: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

PCL tips• Consider the platform tradeoffs from pulling in new libraries– Also check out NuGet for similar/updated packages supporting more platforms

• The more you do in PCLs, the broader that functionality can be leveraged

Page 20: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

PCL tips• Be careful about building PCLs too directly for a single consumer if others are planned, such as:– Assuming one auth model (like Basic) when other scenarios (AD, Oauth, etc) could be well supported with a little more planning– User selection of a file on desktops is significantly different from devices

• Abstract platform services as interfaces, and require those dependencies to be provided by the consumer– File access, UI, system services, etc.

Page 21: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Inversion of control (IoC)• The Hollywood Principle– “Don’t call us, we’ll call you”

• Objects rely on their dependencies from the outside– Data connections– Algorithms– Platform-specific services

• Dependencies are usually provided as abstractions– IExpenseRepository– INavigationService– IPlatformService

• Enables thorough automated testing

Page 22: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

IoC patterns• Factory

• Dependency injection– Constructor– Parameter– Setter

• Service locator is considered an anti-pattern in many circles, but is sometimes the most cost-effective options– Framework requires parameterless constructors– No support from MVVM libraries

Page 23: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

DEMOIoC and Testing

Page 24: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Click to edit Master subtitle style

Model-View-ViewModel (MVVM)

Page 25: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

MVVM refresher• Design/development separation

• Code reuse

• Multiple views on the same logic

• View-logic testability

Model

View (XAML)

View Model

Page 26: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

MVVM overview• View– User interface– Navigate to views– Interaction layer

• ViewModel– Application logic– Service calls– Data management

• Model– Simple representation of data– No logic or functionality

Model

View (XAML)

ViewModel

Data Bindings Command

s

Page 27: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Data binding• View - XAML– Text=“{Binding MyProperty}”

• ViewModel - C#– INotifyPropertyChanged View (XAML)

ViewModel

Data Bindings

Page 28: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Commands• View XAML– Command=“{Binding MyCommand}”

• ViewModel - C#– ICommand– DelegateCommand– RelayCommand

View (XAML)

ViewModel

Commands

Page 29: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Portable MVVM structure

Portable Class Libraries

Application

Services

ViewModels (limited or abstract)

Models

Views (XAML) App Lifecycle

Navigation

ViewModels (Storage, Alerts, Timers)

Page 30: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

DEMOMVVM

Page 31: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

• No philosophy is perfect, especially not MVVM

• Understand the benefits of MVVM frameworks– PRISM–MVVM Light–Many others…

• Rely on tested patterns– Commanding– Dependency Injection– Inversion of Control– Observer– Repository– Service Locator–Many others…

MVVM tips

Page 32: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Click to edit Master subtitle style

Data Services

Page 33: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

External services• Always a good idea to abstract

• Protect DB calls– Even a simple service layer is worth it

• When in doubt, use a standard– SOAP, REST over HTTP, etc

• Use a service bus to traverse network boundaries– Internet client relying on an intranet service, for example

Page 34: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Windows Communication Foundation vs. Web API• WCF–Multiple transport protocols–Multiple encodings

WS-* standardsSupports Request-Reply, One Way, and Duplex message exchange patterns

• Web API (preferred for new projects)–HTTP only– Supports a wide variety of

media types (XML, JSON, etc)–Uses basic protocol and

formats including HTTP, WebSockets, SSL– Is request/response due to

nature of HTTP, but more patterns available via SignalR and WebSockets integration

Page 35: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

OData, the Open Data Protocol• A standardized protocol built on HTTP

• Uses the REST methodology

• Designed for formats like XML, ATOM, and JSON

• Provides a uniform way to represent metadata

• Client data context-friendly

• Great ecosystem of tools and developers

• Available via WCF Data Services and Web API

Page 36: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

Click to edit Master subtitle style

Summary

Page 37: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

• Asynchronous programming

• Portable class libraries

• Inversion of control

• Model-View-ViewModel (MVVM)

• WCF & Web API

• OData

Summary

Page 38: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

• Asynchronous Programming in the Microsoft .NET Framework 4.5– http://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/DEV-H302#fbid=kt1W5OJuY58

• Modernizing WPF Line-of-Business Applications– http://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/DEV-B325#fbid=kt1W5OJuY58

• Understanding Dependency Injection and Those Pesky Containers– http://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/DEV-B207#fbid=kt1W5OJuY58

Resources

Page 39: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

• Using Portable Class Libraries– http://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/DEV-H323#fbid=kt1W5OJuY58

• Getting Started with MVVM– http://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Getting-Started-with-MVVM

Resources

Page 40: Robert Green | Technical Evangelist Dmitry Lyalin | Product Marketing Manager.

©2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics 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.