I am new to using dependency injection, and am trying on Ninject. As I am exploring the function of Ninject Factory, there is something that puzzled me.
From https://github.com/ninject/ninject.extensions.factory/wiki/Factory-interface,
public class Foo
{
readonly IBarFactory barFactory;
public Foo(IBarFactory barFactory)
{
this.barFactory = barFactory;
}
public void Do()
{
var bar = this.barFactory.CreateBar();
...
}
}
public interface IBarFactory
{
Bar CreateBar();
}
We can create Bar using
var bar = this.barFactory.CreateBar();
But what approach should we take to create an instance of Foo?
You need to do exactly one
kernel.Get<>()in the composition root of your application.