0

I have an MvvmCross project in which I have defined a Database class (implementing a IDatabase "service"). This class needs a parameter (the connection string) in the constructor.

Of course, the parameter is known at the level of the application project (WPF, in my case), and not at the level of the library implementing the Database class.

My problem is: how do I pass the parameter when the Database object is created via the IoC container?

I think I should do something similar to

Mvx.RegisterType<IDatabase>(() => new Database("my connection string"));

but I can't find out the right place to write this call. The App class in the top-level WPF project is in no way related to the App class of the "Core" project, so I can't leverage abstract inheritance either.

Simone
  • 1,260
  • 1
  • 16
  • 27

2 Answers2

2

This is possible in 6.1. Also see: https://github.com/MvvmCross/MvvmCross/pull/2814

var title = "The title";
var subtitle = "The subtitle";
var description = "The description";

var arguments = new { title, subtitle, description };
var d = Mvx.IoCConstruct<Database>(arguments);
Martijn00
  • 3,569
  • 3
  • 22
  • 39
  • Was this introduced in 6.1? There is no way to do it in 6.0.1?! – Simone Jun 14 '18 at 07:29
  • I upgraded to 6.1.1, and I see the overload you point out. However, I don't think that's what I need yet. The Database is injected into a ModelView by MvvmCross's reflection. I would need to pass that parameter when that happens - not because I create it explicitly. I need something more like "when you construct an IDatabase, do it using this as the parameter". – Simone Jun 14 '18 at 08:44
0

I finally found it out. The line I wrote in the question was in fact right:

Mvx.RegisterType<IDatabase>(() => new Database("my connection string"));

What I was missing was where to put that line. The correct place is in a MySetup class, at the application level, that extends MvxWpfSetup<MyApp> - precisely, in the InitializeLastChance() method, that gets called after the Initialize() method of your App class (MyApp, as I called it before).

Simone
  • 1,260
  • 1
  • 16
  • 27