3

First of all, I'm aware of the related post here, but that post is quite old, and what's more important, not answered straightly.

So now I use the latest Ninject (stable, 3.2 Nuget packages) and the above mentioned extensions, and still see a non-expected behavior.

public interface IFoo {}
public class Foo {}

public class Parent {
  public IFoo foo;
  public IFoo foo2;
  public Func<IFoo> fooFactory;
  public Parent(IFoo foo, Func<IFoo> factory) {
    this.foo = foo;
    this.fooFactory = factory;
  }
  public void init() { this.foo2 = this.fooFactory(); }
}

...

kernel.Bind<IFoo>().To<Foo>().InCallScope();
var instance = kernel.Get<Parent>();
instance.init();
instance.foo.ShouldEqual(instance.foo2);

This test fails, so it seems like the context is not preserved for the factory function, and it creates a new Foo.

How to achieve the expected behavior?

UPDATE

Based on a comment I've tried the same code with a declared IFooFactory interface bound with ToFactory(). The behavior is the same though.

UPDATE 2

I've just tried with the latest unstable factory and context preservation extensions, and the result is still the same.

Community
  • 1
  • 1
Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
  • Did you try to use [Factory interface](https://github.com/ninject/Ninject.Extensions.Factory/wiki/Factory-interface) instead of [Func](https://github.com/ninject/Ninject.Extensions.Factory/wiki/Func)? – kayess Feb 12 '16 at 10:47
  • Not yet, I prefer using the delegates if there is no other logic beyond that. I'm gonna try it now. – Zoltán Tamási Feb 12 '16 at 10:48
  • The note by author might indicate you worth a try. Quote: _Even if it takes no arguments I personally think factory interfaces are a cleaner way to manage factories; while you do have to write a little more code, the improved readability vs a Func is worth the effort_ – kayess Feb 12 '16 at 10:50
  • Actually it's not working with `IFooFactory` either. Anyway, readability is sometimes subjective, if I read `Func` I immediately know there are no parameters. Only thing I see an advantage of explicitly declared interfaces in this particular case is a bit more flexible extensibility. – Zoltán Tamási Feb 12 '16 at 10:59
  • can you please verify that injecting two `IFoo` instances gives the same result? Just to check that the ContextPreservationExtension is installed correctly. I guess it's best to extend the test to inject two `IFoo`s into constructor and create one by factory. – BatteryBackupUnit Feb 12 '16 at 18:36
  • @BatteryBackupUnit Injecting two `IFoo` instances won't test context preservation, or am I wrong? It would test only `InCallScope` behavior. It's the factory that would test context preservation. – Zoltán Tamási Feb 13 '16 at 14:07
  • @Zoltan, oh yeah of course, you're right... – BatteryBackupUnit Feb 14 '16 at 21:09
  • I've also checked if the context preservation module was loaded, and yes it was. Any idea please? – Zoltán Tamási Feb 16 '16 at 11:40
  • ... This is still an active question. The context preservation module's GitHub repository seems to be dead, as I've posted this issues there and still haven't received any kind of reactions. – Zoltán Tamási Nov 28 '16 at 10:43

1 Answers1

0

There are 2 calls. One is kernel.Get<Parent>(), the other is instance.init().

Scott Xu
  • 11
  • 1