.NET Zone is brought to you in partnership with:

I’ve been a Windows developer since 3.0 and caught the Visual Basic wave early with v1. I’ve released a “production” application in every version of VB since then (except VB for DOS). Focusing on enterprise, line-of-business development I’ve built Call Center Applications, Mortgage finance systems, Customer Relationship Management tools and more recently I’ve been in the Litigation Support/Electronic Data Discovery/Electronically Stored Information space. Greg is a DZone MVB and is not an employee of DZone and has posted 436 posts at DZone. You can read more from them at their website. View Full User Profile

Enlightenment to Adaptation for the Portable Class Library

11.19.2012
| 1856 views |
  • submit to reddit

 

Nito Programming - Portable Class Library Enlightenment / Adaptation

I have a long-standing interest in portable class libraries (PCL), because most of my open-source contributions are widely-applicable libraries (including Comparers, ArraySegments, and of course AsyncEx). This post is an explanation of a technique that I learned from Rx; it's useful for any PCL that is actually a library (i.e., not a portable application).

Portable class libraries are awesome. Now that NuGet supports them, every library writer should join in!

The Problem

Portable Class Libraries enable you to create a single binary that runs on several (.NET) platforms. Unfortunately, it uses the "least common denominator" approach, which means your PCL is greatly constrained in what it can do. In order to "step outside" these restrictions, you need some way for a platform-specific assembly to provide functionality to your PCL core. The obvious answer is to use inversion of control, but how are the (platform-specific) implementations created and passed to your portable code?

Possible Solutions

There are several ways to do this. Daniel Plaisted has a great blog post that gives an overview of different solutions:

  1. Manual dependency injection (passing interface implementations into constructors). Daniel's classic "Disentaglement" demo uses this approach as he describes in his //build/ talk. This is OK if your PCL just has a few large classes (e.g., ViewModels) which are always the "entry point" to your PCL. It's not so good if your PCL is more of a generic library.
  2. Real dependency injection. The disadvantage to this approach is that it restricts all users of a PCL to a specific DI provider.
  3. Service locator (static variables holding the interface implementations). This approach is described in the official MSDN documentation (section "Platform Abstraction"). This requires all code using the PCL to "wire up" its own implementations.
  4. Platform enlightenment / adaptation libraries (extra assemblies loaded via reflection). This is the approach described in this blog post.

The first three approaches depend on the consumer of the library implementing the platform services (or at least instantiating them) and providing them to the portable library:

...

This is fine if your PCL is just the core of a portable application, like Daniel's "Disentanglement" application, where the PCL contains the logic but its "entry points" are just a handful of ViewModels.

But I'm not a fan of this. When I distribute a library, I want users to just add it via NuGet and start using it; requiring "startup" code is a big barrier to adoption.

AFAIK, the Rx team was the first to solve this problem. They describe their "Platform Enlightenment" approach well on their blog (section "Intermezzo - The refactored API surface"). Members of the PCL team have referred to this technique as "Platform Adaptation".

image...

..."

I was just chatting with someone today at work about PCL and how you can use them to re-use binaries...

Related Past Post XRef:
Need a refresher on what Portable Class Library Projects are? (For your co-workers of course, since YOU know about them already... right?)
GoTo Portable Class Libraries - Now's the time to start using them as your default Class Project template?
It's RX v2 Baby! Reactive Extensions v2.0 RTW, with details, download and the SDK too!
Update 4.0.3 for Microsoft .NET Framework 4 Roundup
Visual Studio 2010 is now ever more portable (No, not that kind of portable)... As in the release of the Portable Library Tools 2 Beta for Visual Studio 2010
Using the Portable Library Tools, a tutorial
Portable Library Tools RTW (think "One library binary, many platforms")
Write (.Net library) once, use everywhere (in .Net world)? The Portable Library Tools CTP Released. (Think “Reusing/Sharing the same Project between SilverLight, XNA, Windows Phone, etc” or “DRY .Net Project Style” )

Published at DZone with permission of Greg Duncan, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)