I am trying to figure out the whole Mvvm way of declaring singletons and especially ones that have IoC dependencies tied to them. In my particular example I have a UserService that requires an IDatabase and an IMvxMessenger as an IoC dependency.
The IDatabase is platform specific so In my Droid.UI project I have resolved this using the following code:
var database = new Database(new SQLitePlatformAndroid(), Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "database.db"));
database.Initialize();
Mvx.RegisterSingleton<IDatabase>(database);
In my .Core project I have the following code:
var database = Mvx.Resolve<IDatabase>();
var messenger = Mvx.Resolve<IMvxMessenger>();
Mvx.RegisterSingleton<IUserService>(new UserService(database, messenger));
I am trying to wrap my head around this. What really is the Mvx.Resolve's job and how it works along with the resolving other dependencies. I have already tested the code above and it doesn't seem to be working so I know I am missing something important here.
Any help on this will be much appreciated, thank you in advance!
Follow this link to the Service Location and Inversion of Control page for MvvmCross and look at Constructor Injection and Lazy Singleton.
Instead of resolving the dependency when you register the next, have it be resolved when the UserService instance gets initialized.