mvvmlight icon indicating copy to clipboard operation
mvvmlight copied to clipboard

Constructor injection based on key with SimpleIoc

Open marinasundstrom opened this issue 6 years ago • 1 comments

In my scenario, I want to inject an instance into a constructor as a parameter based on a key.

Is this something that the SimpleIoc could support, or should I choose another IoC container instead?

I'm thinking about adding support for an InjectAttribute.

Concept:

public class Foo 
{
  public Foo([Inject("bar1")] IBar bar) 
  {

  }
  // Omitted
}

marinasundstrom avatar Dec 13 '17 10:12 marinasundstrom

Robert,

I am trying to learn C# and MVVM and often confused, but I did stumble across a small class that does what you want in https://www.amazon.com/Using-MVVM-Light-your-Xamarin/dp/1484224744 page 59.

Interestingly, I can find almost the same code and almost the same description here: http://www.guruumeditation.net/ioc-and-di-with-mvvm-light/

Both writeups are very similar with the Mvvm Light book having the following line:
Public static T GetInstanceDI<t>(this SimleIoc simpleIoc, string key = "") where T : Class

The <t> is an addition from the book.

Anyway, I don't understand how to work it exactly, but both pages say it should do what you want... If this would work for you, seems easy enough to implement.

floppydisk525 avatar Apr 18 '18 07:04 floppydisk525