MvvmCross and IoC

MvvmCross has IoC built-in, in this post I will show how to use it. The Mvx class has a few static metods that you can use for configure the IoC container.

Use the register type method to register new types to the IoC container.

//Register a type
//Register a type as a singleton
Mvx.RegisterSingleton<IMySingleton>(new MySingleton());

Use the resolve method to create instances from interfaces.

var myInstance = Mvx.Resolve<IMyType>();

But I recommend to use constructor injection so much as possible. This because you can use it to resolve interfaces in a class library without referencing the assembly that contains the IoC container.

public class MyClass
     private IMyType _myType;
     public MyClass(IMytype myType)
          _myType = myType;

I use to register types in the create app method in the Setup class on each client.



2 thoughts on “MvvmCross and IoC

  1. Ayco Mentor says:

    I’m new to mvvmcross. Once the instance is declared can I set the object’s properties and they be retained the nest time I resolve the Singleton? I imagine yes, but would like to verify.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.