Xamarin Shared Projects vs Portable class libraries
Asked Answered
E

4

27

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?

Econometrics answered 2/6, 2014 at 8:32 Comment(0)
D
20

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
Designedly answered 2/6, 2014 at 12:48 Comment(1)
Partial classes! +1 , ... I can live with a method re-write between the #ifdefs# and certainly #ifdefs# on the using's ... but not in the logic , it triggers the worst of my ocd. :)Height
R
6

They both have their place. If your code is entirely portable I'd recommend using a PCL. If you need to use platform-specific APIs, then you can do it with a PCL using various techniques (generally involving creating a portable abstraction for the functionality), but if it's an isolated case sometimes it's simpler to just use an #if.

For a general list of pros and cons, see my answer to a question on PCLs versus linked files. Shared projects are similar to linked files, but don't have some of the tooling disadvantages.

Reinwald answered 3/6, 2014 at 19:8 Comment(0)
C
2

My impression is that they listed compiler directives under Benefits of Shared Libraries, simply because it was a feature that PCL does not support.

You should not treat that as an encouragement from Xamarin, as both you and Xamarin are aware of the drawbacks.

So "be smart".

Credulity answered 4/6, 2014 at 3:10 Comment(0)
U
0

I have seen a large Xamarin.iOS & Xamarin.Android app built for a major UK clothing retailer with strong MVVM using Shared Library and Partial Classes very effectively. Only a couple of very specific scenarios where we needed to use IFDEFs. From that experience I would often go this route by default.

Having said that, it's good to note that .NET standard 2.0 is coming which will probably make this debate less of a common question as it brings it's own approach (similar but really quite different to PCL)... https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/

Uterine answered 6/1, 2017 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.