Monday, June 18, 2012

[.NET 4.5] Portable Class Library (PCL)
How to write code that runs on all .NET platforms

The Portable Class Library project in .NET 4.5 allows you to share source code easily between different technologies, which are based on the .NET framework (Windows, Windows Phone, Silverlight, XBOX 360, Metro).

Very helpful if you need to share common algorithms (for validation purposes for example), common interfaces and common data objects between applications based on those different technologies. Using the PCL in this case provides consistency and encapsulation of your code independently from the place where it will be used. Thus resulting in less maintenance costs and higher productivity for your teams.

You have to note however that the PCL only allows using a common sub-set of all features provided by those different technologies. This makes perfectly sense since not all features are supported by each technology.

Now lets see how to use the Portable Class Library from within Visual Studio 2012. First of all you have to create a new project of type Portable Class Library.

PCL_Project

You may then select the target frameworks, where the code needs to run on and the solution gets created by Visual Studio 2012 in the next step.

If you build your solution the resulting DLL is useable by the target frameworks without any additional configuration or modifications, you just add a reference to it and you can use the common code.

Platforms2

You may modify the frameworks you want to target in your existing PCL projects after project generation anytime. For that you have to open the project setting and you may add or remove target frameworks from within the Library section.

ChangePlatforms

Other targeting packs are available for Visual Studio 2012. They may include Visual Studio updates and must be installed with the corresponding runtime versions most of the time.

Those are the additional targeting packs currently available:

  • .NET Framework 4.0.3
  • .NET Framework 4.0.2
  • .NET Framework 4.0.1
  • .NET Framework 4
  • .NET Framework 2.0/3.0/3.5 SP1
  • Windows Azure (via SDK)
  • XNA Game Studio 4.0

You can download them from here:
http://msdn.microsoft.com/en-us/hh487283.aspx

Examples using VS 2012 and VS 2010:

In the following very basic example I am going to show you a real example of how to use the PCL. Lets say that you have some business rules that need to be validated in your applications. Those business rules won’t change if you use a Windows Application, a Windows Phone application or a Metro Application. So it makes perfectly sense to have them developed in a common place.

The class diagram shows the structure of a Validator class, that validates Rules which are based on expressions. This is a quite straight-forward approach on how to implement business rules validation.

As explained before we create a PCL project and implement all those classes, interfaces and validation code in this project.

We then create a common DLL that is used in the different application project, which are based on completely different technologies.

ClassDiagram

Lets start with a Silverlight application created within Visual Studio 2012. Create a new project of type Silverlight Application.

SilverlightProject1

Select Silverlight 5 for this example but note that you could also create a Silverlight 4 project and use the PCL if you wanted to.

SilverlightProject2.

Here is a very basic example of the usage of the common classes, that are defined in the common code assembly, in the newly created Silverlight project. The project builds successfully and you can reuse all interfaces, classes and algorithms, that you have defined in the common code assembly.

CommonCodeInSilverlight

The following example shows how to use the common code library in a Windows Phone 7 application, being developed using Visual Studio 2010. After installing the Windows Phone 7 SDK, add a new project of type Windows Phone Application and add a reference to the common code library.

PhoneProject1

Select Windows Phone OS 7.1 for this example but note that you could also create a Windows Phone 7.0 project and use the PCL if you wanted to.

PhoneProject2

Here is a very basic example of the usage of the common classes, that are defined in the common code assembly, in the newly created Windows Phone project. The project builds successfully and you can reuse all interfaces, classes and algorithms, that you have defined in the common code assembly.

CommonCodeInPhone71


Share/Save/Bookmark

No comments: