Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared library for MonoDroid and MonoTouch

So my company comes from a completely C# .Net environment, so naturally we are looking at Mono for mobile development. We have a very extensive windows platform library which I was able to port most of it as an Android Class Library. The idea being that this Android Class Library will be our mobile platform library which can be reused in the future for MonoTouch (Haven't tested, but read that MonoTouch project can reference Android Class Libraries as long as there is no Android specific code). Within the library though, there are plenty of logging statements, which for the windows environment just end up being trace statements(wrapped in a custom helper class). We want to retain the logging statements and based on the current environment(iOS or Android), just translate those into native logging.

I was planning to use partial declarations for the logging in the mobile platform library and have the Android and iOS specific libraries contain the implementations for those partial logging methods. But that fell apart when I realized partials do not work between assemblies.

So now I am not sure how to go about this. The only thing I can think of if having two separate iOS and Android platform libraries and linking the same files. (Xamarin might have a logging class that does this but we still need to come up with a pattern for any other future abstractions)

Any advice is appreciated. We are still in the experimental/planning phase so there is a chance we might be overlooking something, please let us know if that is the case.

Thanks.

Edit
Lets say I have the following simple static class.

public static class TraceHelper
{
    public static void WriteLine(string message)
    {
        // Validation and preprocessing
        Trace.WriteLine(message);
    }
}

And I have hundreds of calls to this method within the mobile platform library. Now lets say I reference this library in my Android application, is there any way to override the implementation of TraceHelper.WriteLine() without modifying the library itself?

Update
We ended up creating Android and iOS libraries that implemented the missing .NET functionality. Just wrap the native equivalent functionality, in the same .NET namespace and class. EX:

using Android.Util;

namespace System.Diagnostics
{
    public static class Trace
    {
        #region Non-Public Data Members

        private const string Tag = "Trace";

        #endregion

        public static void WriteLine(string message)
        {
            Log.WriteLine(LogPriority.Verbose, Tag, message);
        }
    }
}
like image 328
Alex Avatar asked Nov 03 '22 07:11

Alex


1 Answers

In mvvmcross, I have tackled this using interfaces.

I have declared a common IMvxTrace interface, then I 've added a singleton implementation of it like:

https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Platform/Diagnostics/MvxTrace.cs

This singleton uses an underlying _realTrace implementation.

On droid, the _realTrace is:

https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Android/Platform/MvxDebugTrace.cs

And on touch, the _realTrace is:

https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Touch/Platform/MvxDebugTrace.cs

Similarly there are custom implementations on wpf, win8 and wp

I've done this both using file linking (in master), but more recently using a mixture of PCLs and platform specific libraries. My personal preference is for PCLs as I like using refactoring tools - and these work much better with PCLs than with #if code. Also, as I've grown my code from 1 to 6 platforms I found 6 sets of #if statements too complicated to maintain.

Note that the code above also uses Dependency Injection and an IOC technique. I find this really helpful for cross platform code sharing. There are several portable IOC libraries available including tinyioc, xplatutils, opennetcf, and mvvmcross's simple IOC.

like image 194
Stuart Avatar answered Nov 15 '22 06:11

Stuart