Search code examples
c#xamarin.androidmvvmcross

Using MvvmCross IoC, How can I get a list of singletons that implement a common interface?


My app has a common IRepository interface, and several interfaces which extend it like this:

public interface IRepository
{
  void Start();
  void Stop();
}


public interface IFooRepository : IRepository
{
 // Foo specific methods    
}

public interface IBarRepository : IRepository
{
 // Bar specific methods
}

The classes that implement IFooRepository and IBarRepository get registered as singletons when the app starts. Is is possible to retrieve the singletons as a List<IRepository> using MvvmCross's service locator?


Solution

  • There's no Api for this in MvxSimpleIocContainer

    However, you can easily implement this yourself reflection - simply use reflection to list all the repository-inheriting interfaces and then use Mvx.Resolve to retrieve the singletons which implement those interfaces.

    Alternatively, you could also look at other approaches such as intercepting the registrations as they happen at App startup - either in the bulk registration or in your BaseRepository constructor.