I think this is a matter of applying the right patterns, such as the proxy pattern:
public class LazyFooProxy : IFoo
{
private readonly Lazy<IFoo> foo;
public LazyFooProxy(Lazy<IFoo> foo) {
this.foo = foo;
}
public bool IsCreated {
get { return this.foo.IsValueCreated; }
}
void IFoo.Method() {
this.foo.Value.Method();
}
}
Here we created a LazyFooProxy
proxy class that is able to delay the creation of IFoo
and it also allows checking whether the foo has been created (by calling the Lazy<T>.IsValueCreated
property).
Now you can make the following registration:
var lazyFoo = new LazyFooProxy(new Lazy<IFoo>(kernel.Get<MyFoo>));
kernel.Bind<IFoo>().ToInstance(lazyFoo);
kernel.Bind<MyFoo>().InSingletonScope();
Now you can use the LazyFooProxy.IsCreated
property to check whether MyFoo
was created or not.