问题
I have a class like this:
public class MyClass
{
public MyClass()
{
this.Dependency = new Dependency(this);
}
}
I want to move the new Dependency()
call to the constructor.
public class MyClass
{
public MyClass(IDependency dependency)
{
this.Dependency = dependency;
}
}
I can't work out how to bind it so that the IDependency
is created with the 'this' constructor argument.
Bind<IDependency>()
.To<Dependency>()
.WithConstructorArgument("myClass", ctx => ctx.???); // How do I do this?
回答1:
The way your example is laid out, I think you have that backards. MyClass
has a dependency on IDependency
, so I'd expect you to be doing:
Bind<IMyClass>().To<MyClass>().WithConstructorArgument("dependency", new Dependency());
来源:https://stackoverflow.com/questions/19253827/how-do-i-bind-a-class-which-takes-its-parent-class-in-the-constructor-with-ninje