4
votes

I'm giving a MVVMCross a spin, to see if it will be of use in some bigger projects coming up, and it's great. I like the navigation, viewModel location and general cross-platform approach, which is just what I need. However, I'm a bit stuck on splitting out some of the dependency injection depending on the platform.

So, we have the basic application, with a shared portable library, that initialises the service references when starting up:

    public TwitterSearchApp()
    {
        InitaliseServices();
    }

    private void InitaliseServices()
    {
        this.RegisterServiceInstance<ITwitterSearchProvider>(new TwitterSearchProvider());
    }

Fine. That defines the service implementations that will be used across all the platforms. But what about the situation where I will need different implementations on different platforms - for instance perhaps storage/caching, where the core requirement is the same, but needs to be handled differently on a phone than on a tablet.

I thought it might go in Setup somewhere:

public class Setup : MvxBaseWinRTSetup
    {
        public Setup(Frame rootFrame): base(rootFrame)
        {
        }

        protected override MvxApplication CreateApp()
        {
            var app = new TwitterSearchApp();//set platorm specific IoC here maybe?
            return app;
        }

        protected override void AddPluginsLoaders(Cirrious.MvvmCross.Platform.MvxLoaderPluginRegistry loaders)
        { // or perhaps here?
            loaders.AddConventionalPlugin<Cirrious.MvvmCross.Plugins.Visibility.WinRT.Plugin>();
            base.AddPluginsLoaders(loaders);
        }
    }

but I'm not sure. I've seen the references to replacing the ViewModel locator, but is there are similar way of replacing the other IoC services?

thanks, great job on the framework in general, I really like how it works (apart from this bit, which I don't understand properly yet)

Toby

1
hmm, maybe a PlugIn then: stackoverflow.com/questions/12564272/…TobyEvans

1 Answers

9
votes

There are three basic options:

1. Add the platform specific services in your UI project and then register them in an override during setup - which override you use depends on when your services are needed, but for most cases you can just use the InitializeLastChance override which gets called at the end of initialization:

protected override void InitializeLastChance()
{
    this.RegisterServiceInstance<IMyService>(new SingletonMyService());
    this.RegisterServiceType<IMyService2, PerCallService2>();
    base.InitialiseLastChance();
}

If 'last chance' is too late for your service - if you need the service during the core app startup - then you can override any initialisation step after InitializeIoC - e.g. InitializeFirstChance. For the list and order of initialisation steps, see InitializePrimary and InitializeSecondary in MvxBaseSetup.cs

2. Add the platform specific registration in some other bit of the UI code - e.g. in the constructor for a specific View (this option isn't used much... but you could use it in some odd cases, if you wanted to...)

3. Use a plugin - all plugins are is a wrapper around IoC. Plugins have the disadvantage that they add some development overhead (you have to add the separate projects and the plugin boilerplate files), but they have the advantages that they can be reused across apps and it's easier to write test apps and test harnesses for them. For more info on plugins, see Making mono cross platform support for Task/Intent and see http://slodge.blogspot.co.uk/2012/10/build-new-plugin-for-mvvmcrosss.html


My general advice - start with the first option and migrate out to a plugin if you want to reuse the code in later projects...