May 272014
 
 May 27, 2014  Posted by at 7:03 am C# & F#, Universal Apps, WinRT, WP, WPF, XAML  Add comments

I had a discussion with a developer who was getting into .Net development and had questions in regards to the many ways we can reuse code across platforms. We have several ways of reusing code, all with pro’s and con’s. I tried to just quickly summarize what we can and cannot do, and what the rough recommendations are. Feel free to correct me if I’m wrong, or if something is missing in the matrix.

I’ve left out dealing with platform differences as preferred way (be that abstraction of conditional compilation) is a matter of taste.

One could question if it makes sense to compare linked files and shared project, but I’ve left them as two separate options as they have two distinct different ways of setting up the sharing. Shared project works by linking files, but does the ‘heavy lifting’ for us- which is quite valuable and allows for future improvements such as extended tooling and authoring support beyond what we got in the latest Visual Studio release.

Demo code

Coming later this week

Comparison

Universal Apps or portable class library

* You could share with other XAML tech based applications but with such restrictions it would most likely not be worth it, in my opinion.

** API access will be limited to the most limited platform targeted

*** Brokered components can call native APIS, but this is for Windows Store Apps only, and only if they are side loaded!

­

Rough recommendations (as reflected by documentation and developers using these technologies):

Linked files:

If only very few files are shared and for some reason you don’t want to create a Shared project to hold those files

PCL:

When targeting several platforms (such as iOS and Android) for scoping reusable data objects, services and other API independent logic. Or when you need just one binary. Can be used with linked files / Shared project. API access will be limited to the most limited platform targeted.

WinRT Components

When cross language is needed, or you want to call native APIs (for side loaded Windows Store Apps only).

Shared project (Universal Apps)

Best option for Windows Store and Windows Phone (and xBox later) targeting. Can be used for other project types and platforms and in combination with the other sharing options.

———————

Sharing views using PCL:

  2 Responses to “Code reuse: Shared project, PCL, WinRT component or linked files?”

  1. Similar to shared projects, another approach would be to setup a local NuGet repository.

    There are a few different dev teams at my employer, and each team is doing some unique projects, but there have been some opportunities for using shared libraries. Rather than circulate DLLs manually and email notifications of new releases, we created an internal NuGet package repository and publish the shared “features” there. This allows shared codebases and libs on top of the goodness of NuGet’s package manager and updater.

  2. What do you mean “portable”?

    There is a difference between linked files and shared project. That is, with linked files you can choose which files to add to which project, but in shared project you cannot (I guess). I found this feature particularly useful when I need to develop both desktop and mobile apps. In desktop apps, I deal with more complicated business logics which are not involved in mobile apps. Maybe I can use multiple shared projects to handle this situation, but I don’t feel I will like this idea.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

What is 11 + 13 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)