Search code examples
wpfsqlitemvvmcross

How do I override a plugin registration in MVVMCross


I'm using the WPF Sqlite plugin in MVVMCross, but I want to override the directory that the database is written to. I've written a custom ISQLiteConnectionFactory which currently resides in my WPF bootstrapper project:

internal class CustomMvxWpfSqLiteConnectionFactory : ISQLiteConnectionFactory
{
    const string DirectoryName = "ProductName";

    public ISQLiteConnection Create(string address)
    {
        var appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        var dir = Path.Combine(appData, DirectoryName);
        Directory.CreateDirectory(dir);

        var path = Path.Combine(dir, address);
        return new SQLiteConnection(path, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, false);
    }
}

What I can't figure out is how to override the Mvx.RegisterSingleton<ISQLiteConnectionFactory>(new MvxWpfSqLiteConnectionFactory()); that Cirrious.MvvmCross.Plugins.Sqlite.Wpf.Plugin does.

My PCL project's App registers a singleton service that depends on ISQLiteConnectionFactory during Initialize, so I obviously want to override the IOC registration before then. But no matter what I do, the plugin's registration of MvxWpfSqLiteConnectionFactory rather than my own registration of CustomMvxWpfSqLiteConnectionFactory seems to take precedence.

I've tried putting my register call in all sorts of overrides in my WPF Setup.cs, but nothing has worked so far.


Solution

  • An article on how plugins are loaded is included in https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#how-plugins-are-loaded

    The Sqlite plugin by default is initialised during PerformBootstrapActions in Setup - see https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#setupcs for where this occurs in the start sequence.

    From your question, it's not clear which overrides in Setup you've tried - I'm not sure which positions "all sorts" includes. However, it sounds like want to register your ISQLiteConnectionFactory at any point after PerformBootstrapActions and before InitializeApp - so one way to do this would be to override InitializeApp:

    protected virtual void InitializeApp(IMvxPluginManager pluginManager)
    {
        // your code here
         base.InitializeApp(pluginManager);
    }
    

    Some possible other ideas to consider: