Xamarin Shared Projects vs Portable class libraries

user3141326 picture user3141326 · Jun 2, 2014 · Viewed 9.7k times · Source

I am trying to figure out which is the better way to go for our project.

PCL Library or Shared Project by Xamarin.

In the Xamarin documentation link here it says that a rule of thumb is to choose shared project when we will not share the library. The shared project can be written with #if's to make sure it works with multiple platforms - this also causes some issues with refactoring #ifs not active.

Yet I have a gut feeling it is not right to put this code to a shared class. If a code that is to be available to Windows, Android and IOS mobile platforms is using a shared project instead of PCL - it means we are using #ifs inside shared project instead of writing platform specific code in a platform specific project.

This is trying to make support of non PCL items via #ifs and making the shared code more complex and harder to maintain. Shouldn't this be the work to be done by Xamarin for improving .NET PCL codebase?

And also this means we are putting platform specific features in the shared project and not the platform specific project - i.e. hiding complexity for a specific platform from the platform project itself - which feels wrong in terms of architecture.

Am I right (in that case I am conflicting with Xamarin documentation) or am I missing something?

Answer

SKall picture SKall · Jun 2, 2014

Both have their place. For example you could put an interface into a PCL, then implementation of it in shared code IF the implementation would have decent amount of shared code.

I don't like compiler flags either, I would prefer to use partial classes. This way you can avoid majority, or even all, of compiler flags. Class1.cs would go into shared project and the rest would go into their platform specific projects.

Class1.cs
Class1.ios.cs
Class1.android.cs
Class1.wp8.cs