Question

I currently have a native Android AND iOS library that contains different "modules" composed of models (the smallest part), views and controllers (the biggest part).

I would like to start writing apps using xamarin without losing all the work I did on this library. But it seems like if the native library binding is more suited for utilities libraries, or libraries that only contains 1 part of the MVC components, not the 3 which are depending from each other.

So after reading the Xamarin documentation about cross platform development and native library binding, I think that I should rewrite all the model part of my library using a Xamarin C# library, I would drop Core Data on iOS, and ORM Lite on Android, and instead I would use the SQLite library provided. This job does not sounds to big, so it is not a problem (and on long term it will reduce the maintenance effort). But now comes the problem/question, when my models will be C# classes, how will I be able to use these models in my controllers which are still written using Obj-C and Java ? Can I obtain header files from the C# models classes that I can import in the iOS / Android code ? Or do I also need to rewrite the controllers using C# (that would be a no-go to using Xamarin for me) ?

I am open to any migration suggestion/strategy that would avoid losing multiple months of work, so feel free to answer if you have one :)

Was it helpful?

Solution

I currently have a native Android AND iOS library that contains different "modules" composed of models (the smallest part), views and controllers (the biggest part).

That probably means you have currently two libraries, one for iOS and one for Android. One written in Objective-C and one in Java.

I would like to start writing apps using xamarin without losing all the work I did on this library. But it seems like if the native library binding is more suited for utilities libraries, or libraries that only contains 1 part of the MVC components, not the 3 which are depending from each other.

What makes you think this? You can bind whatever you want to bind. And you don't need to bind everything to start with. If for now you only want to bind some parts from your native library that deals with UI, then skip the rest. You can add bindings later.

I think that I should rewrite all the model part of my library using a Xamarin C# library, I would drop Core Data on iOS, and ORM Lite on Android, and instead I would use the SQLite library provided.

That is a valid approach. However then you will not be able to continue using your native code. There is no way to talk from ObjC to C#. Only vice versa. Even if it was possible: you would have to change your controllers to talk to SQLite.Net instead CoreData, or add an abstraction layer, which in turns you will have to bind and implement on the C# side of things. All of this is probably more work than sticking with the native versions or rewriting everything in C#.

Can I obtain header files from the C# models classes that I can import in the iOS / Android code ? No, you cannot.

Or do I also need to rewrite the controllers using C# (that would be a no-go to using Xamarin for me) ? You have the choice:

  • stick with what you have and live with the limitations of maintaining two libraries (and two application code bases)
  • Bind the libraries and create a managed abstraction around them. Then use this abstraction from a PCL and reference that PCL from your Xamarin C# and Android application. Live with this solution, until you're unhappy enough from maintaining two code bases, and then:
  • Rewrite your native code and switch to Xamarin and have one code base.

Maybe one more note on bindings: bindings are not meant to exchange part of the native functionality. They transport native functionality into the managed world. They do not enable you to pick the best of both worlds and then create one jack of all trades solution. You end up with the same result as you would if you stayed in the native world: think what you would have to do if you wanted to change from CoreData on iOS to a Json based storage for instance? You will have to touch all of your controllers unless you prepared everything to support abstraction of the data layer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top