Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Shared Projects vs Portable class libraries

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?

like image 333
user3141326 Avatar asked Jun 02 '14 08:06

user3141326


People also ask

What is the difference between the portable class library and shared projects?

The difference between a shared project and a class library is that the latter is compiled and the unit of reuse is the assembly. Whereas with the former, the unit of reuse is the source code, and the shared code is incorporated into each assembly that references the shared project.

What is PCL and shared project in Xamarin?

Shared Projects work well for application developers planning to use lots of platform-specific functionality in their cross-platform apps. While PCL projects continue to be supported in Visual Studio, . NET Standard is recommended for new projects.

What is shared project in Xamarin forms?

Shared Projects let you write common code that is referenced by a number of different application projects. The code is compiled as part of each referencing project and can include compiler directives to help incorporate platform-specific functionality into the shared code base.

What is Portable Class Library Xamarin?

What is a Portable Class Library? When you create an Application Project or a Library Project, the resulting DLL is restricted to working on the specific platform it is created for. This prevents you from writing an assembly for a Windows app, and then re-using it on Xamarin. iOS and Xamarin. Android.


1 Answers

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 
like image 101
SKall Avatar answered Sep 23 '22 01:09

SKall