shared project library - sddconf.comsddconf.com/brands/sdd/library/cross-platform-dev.pdfshared...
TRANSCRIPT
§
§
§
Shared Project Portable Class Library
Shared Projects source + assets
§§§
Shared Project with C# source files, and images
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
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
Conditional Compilation
Class Mirroring
Partial Classes + Methods
specific platform
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
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 ✖ ✖ ✖ ✖ ✖ ✖
Pick only the framework targets you need right nowto give you as broad API reach as possible
Selected profile has no constructor on StreamReaderwhich takes a string
void LoadNotes(string filename) {var reader = new StreamReader(filename);...
}
Callbacks Factories
DependencyInjection
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
§
§
§
§
> Loaded by assembly name at runtime> Nuget always prefers platform-specific library over PCL> Method dispatch is done at runtime
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.
‟
„
1.2.
3.
§
PCL
public class AlertService{
bool Speak(string text){
return false;}
}
public class TTSService{
bool Speak(string text){
return false;}
}
§
public class AlertService{
bool Speak(string text){
return false;}
}
public class TTSService{
bool Speak(string text){
...}
}
§ 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
§
§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
§
§
§
§
§
§
§