I'm using the attached image to explain what I meant.
I have a few classes managed by NInject. Some of them have a few singleton instances, and others are in transient scope. In the image, blue rectangles are singltons, red are transient. The Processor depends on other classes or instances.
I want to get the instance of Processor each time by using kernel.Get. However, each time I want to use different values for the objects used by the Processor. See Action1 and Action2 in the image. The code is not real but just for explanation here.
Is there any existing way can meet my needs?
You should be able to pass constructor arguments given that your Processor takes those dependencies as arguments in the constructor.
var foo = new Ninject.Parameters.ConstructorArgument("foo", new Foo());
var bar = new Ninject.Parameters.ConstructorArgument("bar", new Bar());
var processor = kernel.Get<IProcessor>(foo, bar);
public Processor (Foo foo, Bar bar){
this.foo = foo;
this.bar = bar;
}
Use the OnActivation() function can hook the event when a dependency is activated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With