Question on architecture of XF Plugins

CalvinMorooney.8162CalvinMorooney.8162 USUniversity ✭✭
edited February 2017 in Xamarin.Forms

I was looking through some of James Montemagno's super helpful plugin libraries this morning and noticed something that I thought was strange. I'll use the CrossConnectivity plugin as an example, but I've seen this pattern in other libraries.

In CrossConnectivity there is a linked file that lives in the Connectivity.Plugin and is linked to both the Android and iOS implementations of the library. This function in that file grabbed my eye:

static IConnectivity CreateConnectivity()
{
#if PORTABLE
            return null;
#else
        return new ConnectivityImplementation();
#endif
}

I get how it works, it should force the proper implementation of IConnectivity to be resolved based on the platform the linked file is running in, and null if it's run inside it's actual project but it depends on having the proper defined symbols in your compile configurations.

I suppose I just thought this was a strange way to do this rather than using an IoC or DI and registering or injecting the proper IConnectivity implementation in the platform projects (instead of relying on compiler symbols). I get it if it's just the developer's preference but was wondering if there was a technical reason people are using this pattern.

Best Answer

Answers

  • CalvinMorooney.8162CalvinMorooney.8162 USUniversity ✭✭

    Thanks @AlexDunn! I can understand that reasoning behind that...it was just strange to read at first; it didn't seem like the cleanest way to do it but I guess I was looking at it from a totally different perspective

  • AlexDunnAlexDunn USMember ✭✭✭

    Oh it's nothing pretty. Compiler symbols are gross, and even Singletons are gross, but it does cover everyone's needs.
    I personally still inject the actual implementation most of the time. It makes it easier to expand on the plugin or implement it on platforms that aren't initially supported while still being consistent between platforms.

    For example, UserDialogs https://github.com/aritchie/userdialogs ( Shout out @AllanRitchie ) covers a lot, but I have had times where I wanted to customize something or do it differently, so I create my own implementation that inherits the pre-built one, override the method I want, then inject THAT implementation rather than the provided one.

Sign In or Register to comment.