IOC platform in MVVMCross

I give MVVMCross a back to see if it will be useful in some large projects, and thatโ€™s great. I like the navigation, viewModel layout and the general cross-platform approach I need. However, I am a bit stuck in sharing some injection dependencies by platform.

So, we have a main application with a common portable library that initializes service links at startup:

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

Fine This defines the service implementations that will be used across all platforms. But what about the situation when I need different implementations on different platforms - for example, memory / caching, where the main requirement is the same, but on the phone it needs to be processed differently than on the tablet.

I thought he could go to 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 saw links to replace the ViewModel locator, but is there a similar way to replace other IoC services?

Thank you, excellent work on the framework as a whole, I really like how it works (except for this bit, which I do not understand yet)

Toby

+4
source share
1 answer

There are three main options:

1 . Add platform-specific services to your user interface project and then register them with an override during installation - this overridden use depends on when your services are needed, but in most cases you can simply use the InitializeLastChance override that is called at the end of initialization:

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

If the "last chance" is too late for your service - if you need a service during the launch of the main application, then you can override any initialization step after InitializeIoC - for example. InitializeFirstChance . For a list and order of initialization steps, see InitializePrimary and InitializeSecondary in MvxBaseSetup.cs

2 . Add the registration of a specific platform to another bit of the UI code - for example, in the constructor for a specific view (this parameter is not used much ... but you can use it in some odd cases if you want ...)

3 . Use the plugin - all plugins are the wrapper around the IoC. Plugins have the disadvantage that they add some development overhead (you need to add separate projects and plugin template files), but they have advantages that they can reuse in applications, and itโ€™s easier to write test applications and test harnesses for them. For more information about plugins, see Implementing exclusive support for a platform / task and see http://slodge.blogspot.co.uk/2012/10/build-new-plugin-for-mvvmcrosss.html


My general advice is to start with the first option and go to the plugin if you want to reuse the code in later projects ...

+7
source

All Articles