Multiplatform usage

To use Iridium in a multi-platform environment with a shared PCL or .NETStandard library for common application code, the database provider should be created in the platform-specific project.

For example, if you're creating a Xamarin app for iOS and Android, with a common shared project (PCL/.NETStandard), you can create the database context and all other database access code in the shared library. The iOS projects and Android projects should both create the Sqlite data provider and register them as a dependency service.

In the example below, the Iridium dependency injection framework is used. Add a reference to the iridium.depend Nuget package to use it.

iOS project

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        // other code

        // Register data provider
        ServiceLocator.Register(new SqliteDataProvider(dbName));  

        return base.FinishedLaunching(app, options);
    }
}

Android project

// Xamarin Forms example

[Activity(Label = "MyAppName", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        // Register data provider
        ServiceLocator.Register(new SqliteDataProvider(dbName));  

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }
}

Common project

Context class:

public class MyContext : StorageContext
{
    public MyContext(IDataProvider provider) : base(provider)
    {
    }

    public IDataSet<Customer> Customers;
    // ...
}

Instantiate context:

// the data provider registered in the iOS/Android project
// will be injected in the constructor call

var context = ServiceLocator.Create<MyContext>();

// use data:
var customer = context.Customers.Read(1); // read customer ID 1