shared project library - sddconf.comsddconf.com/brands/sdd/library/cross-platform-dev.pdfshared...

38

Upload: others

Post on 01-Jul-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 2: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

§

§

§

Page 3: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

Shared Project Portable Class Library

Page 4: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 5: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

Shared Projects source + assets

§§§

Shared Project with C# source files, and images

Page 6: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

Shared.shprojNoteManager.cs

NoteItem.cs

Paperclip.png

Project defined by.shproj type

This project defines the included files as well as the build type (Compile, None, etc.), but does not actually generate any outputno assembly is produced

Page 7: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

compiled for the target

AnyNote.Droid.csprojNoteFragment.cs

FreehandFragment.cs

AnyNote.iOS.csproj

NoteViewController.cs

FreehandViewController.cs

AnyNote.WP8.csproj

NoteView.xaml.cs

DrawInkView.xaml.cs

Page 8: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 9: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

Conditional Compilation

Class Mirroring

Partial Classes + Methods

Page 10: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 11: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 12: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

specific platform

Page 13: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

Portable class libraries are made up of two concepts: an API contract (AKA: reference assembly) and an implementation. Most of the time, those concepts exist together in a single assembly. PCLs, however, have the flexibility to have multiple implementations for a single API contact. This 2nd case is useful, when implementations need to be platform-specific in some way (ex: x86 vs. ARM or Android vs. iOS). In this case, a PCL would have a single reference assembly that consumers compile with, and then implementations available to be deployed for each of the supported platforms. In all cases, PCLs expose a surface area that is compatible with multiple .NET platforms.

„− definition from Microsoft

Page 14: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 15: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 16: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

Feature .NET Framework

Windows Store Silverlight Windows

Phone (SL)Windows

Phone (Store) Xamarin

Core Libraries ✔ ✔ ✔ ✔ ✔ ✔

LINQ ✔ ✔ ✔ ✔ ✔ ✔

IQueryable ✔ ✔ ✔ 7.5+ ✔ ✔

Compression 4.5+ ✔ ✖ ✖ ✔ ✔

Data Annotations 4.0.3+ ✔ ✔ ✖ ✖ ✔

System.IO.File ✖ ✖ ✖ ✖ ✖ ✖

Page 17: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 18: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

Pick only the framework targets you need right nowto give you as broad API reach as possible

Page 19: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 20: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 21: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

Selected profile has no constructor on StreamReaderwhich takes a string

void LoadNotes(string filename) {var reader = new StreamReader(filename);...

}

Page 22: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

Callbacks Factories

DependencyInjection

Page 23: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 24: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

Shared Projects Portable Class Libraries

PROS CONSAll APIs available Can lead to

spaghetti code

Platform-specificlogic can be added directly

Cannot be unit tested on it's own

All file types can be shared

Must be shipped in source form

PROS CONSEnforcesarchitectural design

Limited APIs available

Can be unit tested separately

Can be difficult to share non-code files

Can be shipped in binary form (Nuget)

More work to integrate platform-specific code

Page 25: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 26: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

§

Page 27: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

§

§

Page 28: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

§

> Loaded by assembly name at runtime> Nuget always prefers platform-specific library over PCL> Method dispatch is done at runtime

Page 29: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

Portable class libraries are made up of two concepts: an API contract (AKA: reference assembly) and an implementation. Most of the time, those concepts exist together in a single assembly. PCLs, however, have the flexibility to have multiple implementations for a single API contact. This 2nd case is useful, when implementations need to be platform-specific in some way (ex: x86 vs. ARM or Android vs. iOS). In this case, a PCL would have a single reference assembly that consumers compile with, and then implementations available to be deployed for each of the supported platforms. In all cases, PCLs expose a surface area that is compatible with multiple .NET platforms.

Page 30: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

1.2.

3.

Page 31: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

§

PCL

public  class AlertService{

bool Speak(string text){

return  false;}

}

public  class TTSService{

bool Speak(string text){

return  false;}

}

Page 32: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

§

public  class AlertService{

bool Speak(string text){

return  false;}

}

public  class TTSService{

bool Speak(string text){

...}

}

Page 33: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

§ lib/ Portable-Net45+NetCore45+MonoTouch+MonoAndroid/

MyComponent.dll // PCL VersionNet45/

MyComponent.dll // Desktop VersionXamarin.iOS10/

MyComponent.dll // iOS VersionMonoAndroid/

MyComponent.dll // Android VersionNetCore45/

MyComponent.dll // WinRT Versionwp8/

MyComponent.dll // WP 8 Versionwpa8/

MyComponent.dll // WP 8.1 Version

Page 34: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

§

Page 35: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

§assembly type method

call  instance   bool[MyComponent]Services.TTSService::Speak(String)

That means your app actually executes using the platform-specific assembly, even if it was compiled with the PCL definitions – this is exactly how .NET itself works; the compiler uses reference assemblies but the runtime uses a different set of DLLs

Page 36: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows
Page 37: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

§

§

§

Page 38: Shared Project Library - sddconf.comsddconf.com/brands/sdd/library/Cross-Platform-Dev.pdfShared Project Portable Class Library. Shared Projects source + assets!!! ... Silverlight Windows

§

§

§

§