Lazy <> Injection Injection

I use the ninject framework. In my code, I have a Lazy object. I can create an instance, but when I call the value property, I got an exception.

  private Lazy<IPsoriasisReportUserControl> psoriasisReportUserControl; [Inject] public Lazy<IPsoriasisReportUserControl> PsoriasisReportUserControl { get { return psoriasisReportUserControl; } set { psoriasisReportUserControl = value; } } 

I got

The personally-initialized type does not have a public constructor without parameters

because injection does not inject the method into the constructor. I think I need to write a binding method that creates a new instance.

+6
source share
3 answers
 Bind(typeof (Lazy<IPsoriasisReportUserControl>)).ToMethod( ctx => new Lazy<IPsoriasisReportUserControl>(() => Kernel.Get<IPsoriasisReportUserControl>())); 
+8
source

Use the factory extension for Ninject https://github.com/ninject/ninject.extensions.factory

+12
source

You need a standard default constructor in Lazy:

 public Lazy() {} 
+1
source

Source: https://habr.com/ru/post/946938/


All Articles