wintellect - devscovery - portable class library

20
wintellect.com consulting training design debugging Portable Class Library: Tomorrow's Apps Today Jeremy Likness (@JeremyLikness) Principal Consultant [email protected] Copyright © 2012

Upload: jeremy-likness

Post on 18-Dec-2014

523 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

Portable Class Library: Tomorrow's Apps Today

Jeremy Likness (@JeremyLikness)Principal [email protected] Copyright © 2012

Page 2: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

Founded by top experts on Microsoft – Jeffrey Richter, Jeff Prosise, and John Robbins – we pull out all the stops to help our customers achieve their goals through advanced software-based consulting and training solutions.

Consulting & Debugging• Architecture, analysis, and design services• Full lifecycle custom software development• Content creation• Project management• Debugging & performance tuning

Training• On-site instructor-led training• Virtual instructor-led training• Devscovery conferences

Design• User Experience Design• Visual & Content Design• Video & Animation Production

what we do

who we are

how we do it

consulting training debuggingdesign

Page 3: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

• http://bit.ly/win8design • Free source at:

http://windows8applications.codeplex.com/

• Everything you need to build and deploy Windows Store apps using C# and XAML

• Includes the portable project you will see today

Building Windows 8 Apps with C#

Page 4: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

• Introduction to the Portable Class Library• Understanding Extension SDKs• Portable profiles • Chasing ICommand: PCL Under the Hood• Porting existing code • Strategies for building new applications• Comprehensive Example: Wintellog • PCL Futures

Agenda

Page 5: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

Introducing PCL

• Write and build managed assemblies that work (without recompiling) on more than one .NET Framework platform

• Target frameworks include:– .NET Framework 4 and later (including WPF)– .NET for Windows Store apps (Windows 8)– Silverlight 4 and later– Windows Phone 7 and later– Xbox 360

• Eliminate sharing of source

Page 6: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

Supported Features

Feature .NET Framework

Windows Store

Silverlight

Windows Phone

Xbox 360

Core Yes Yes Yes Yes Yes

XLINQ 4.0.3+ Yes Yes Yes Yes

LINQ Yes Yes Yes Yes

Network Yes Yes Yes Yes

Serialization Yes Yes Yes Yes

WCF Yes Yes Yes Yes

IQueryable Yes Yes Yes 7.5+

MVVM 4.5 Yes Yes Yes

MEF Yes Yes Yes

Numerics Yes Yes Yes

Dynamic 4.5 Yes Yes

Annotations 4.0.3+ Yes Yes

Page 7: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

PCL in MSDN Documentation

Page 8: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

PCL in MSDN Documentation

Page 9: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

PCL and Model-View-ViewModel• .NET Framework 4.5, .NET for Windows Store apps,

Silverlight, and Windows Phone• Observable collections (normal and read-only)• Notify property changed • Notify collection changed • Notify data error info • ICommand

Page 10: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

PCL + VS 2012 = Extension SDK• Software Development Kit (SDK) is a collection of files that

Visual Studio will treat as a single item– Platform SDK – mandatory for a target platform, i.e. "Windows 8

SDK" – Extension SDK – optional components to extend a platform

• Target Platform Moniker (TPM) = Identifier (TPI) and Version (TPV) ex: "Windows, version=8.0"

• References (binaries, either .WinMD or assemblies), redistributables, and design-time components

• Can drop any components to a path with a TPM and Visual Studio will substitute that path

• Easy to see what the target profile is• Each combination of target platforms creates a unique profile

Page 11: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

PCL Under the Covers

• Use ILDASM to inspect portable assemblies

• ICommand lives in System.Windows.dll for Silverlight, but System.dll in .NET 4.5 and Windows.UI.Xaml.Input.dll in Windows Store apps ("retargetable")

Page 12: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

PCL Under the Covers (cont.)

• Classic type forwarders route to the appropriate assembly

Page 13: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

PCL Under the Covers (cont.)

• Windows Store apps forward to System.ObjectModel and language projections maps them to the WinRT framework

Page 14: Wintellect - Devscovery - Portable Class Library

wintellect.com

demo

consulting training design debugging

Basic MVVM with PCL

Page 15: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

• Always easier when implementing MVVM, but are your ViewModels tied to views (i.e. do you include XAML namespaces?)

• Decoupled code is key• Identify key APIs that are platform-specific and

implement façade • Common areas to refactor out include UI,

networking, and storage• Convert an existing class library to a PCL assembly

Porting Existing Code

Page 16: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

• You can convert an existing class library to a PCL• First, unload the project• Second, change the settings:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> to<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />

• Next, add the following project GUIDs to the end of the first property group – this adds the library tab and dialog to change the targets:<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

• Finally, reload the project and select the target frameworks

Porting Existing Code

Page 17: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

• Follow S.O.L.I.D. Principles – they do make it easier– Single responsibility – a class should only do one thing and change for one reason– Open/closed principle – open for extension, closed for modification– Liskov substitution principle – no side effects/unexpected behaviors when cast to a

base class– Interface segregation – interfaces should be focused on specific, targeted

functionality– Dependency Injection – keep dependencies external from your class and inject

them

• Use MVVM and keep view-specific code in the view, not the view model

• Target the smallest number of frameworks possible in your portable assemblies to ensure the widest surface area of APIs to choose from, i.e. don't include Xbox if you're not going to write for it

• Pick a target platform to run your unit tests and only write tests for PCL assemblies in that target

• Don't forget to write unit tests for the classes that are specific to target platforms

Strategies for Building New Apps

Page 18: Wintellect - Devscovery - Portable Class Library

wintellect.com

demo

consulting training design debugging

Wintellog: Windows Store and WPF

Page 19: Wintellect - Devscovery - Portable Class Library

wintellect.comconsulting training design debugging

• PCL is native to VS 2012 and uses Extension SDKs• PCL maps target frameworks to profiles that

represent a "lowest common denominator" of API surface area

• Under the hood, PCL takes advantage of type forwarders and Windows 8 advances to enable the portable assembly

• Existing code can be ported to use the PCL• New applications should take advantage of the PCL

for various core assemblies, with storage and UI abstracted away

• PCL should be a solid investment as the team is actively working on updates to keep pace with the latest

Recap

Page 20: Wintellect - Devscovery - Portable Class Library

wintellect.com

Questions?

consulting training design debugging

Jeremy Likness (@JeremyLikness)Principal [email protected]