If you usually wear two hats (architect and developer) like I do, you might find this blog post useful. My current engagement requires me to have the precision of a surgeon, because anything misplaced might break the functionality of this Xamarin Android application. My customer’s requirement was “simple” being able to extend existing functionality and stabilize the solution as it stands now. I do not know who architected or built it in the first place, but that is irrelevant at this moment in time. The solution has one Xamarin Android (Xamarin Forms) and a common library that is PCL, and even when the application has a service locator and dependency injection the way it was originally built does not allow me to pass certain types to the PCL side of things. Let’s remember, PCL does not support all the features in .NET because it is agnostic of the platform, so it can run and behave the same on iOS, Android, Windows Phone, etc. Since I had to consume some functionality in .NET project (Xamarin) in PCL it was very likely that functionality was not present. For those scenarios, I rely on the AppDomain to store code that must be available to the overall solution, in the AppDomain (in .NET) I can easily store an object or just a IntPtr and use Marshal class to consume it from somewhere else, this was not the case however. In PCL land, AppDomain is not serializable that’s why I had to come up with this approach.
PCL does not support AppDomain nor has a Marshal class, so from that standpoint I was limited. In order to workaround it this is what I had to do.
1-. In the Xamarin Android code that has access to AppDomain, we store the object that is dependent by PCL project. We implement and expose our required object as a singleton.
2-. In the PCL project we then retrieve for use
Now, the object that was required in PCL library can be retrieved from AppDomain. I would also like to mention that we should not store too much stuff in AppDomain. In this case, I had a requirement and was the only option available. If code would have been written better, none of all this would make sense.