Lazy Resolution with Func<TService>
description
Kzu,
In some of my classes I like to delay the loading of dependencies for as long as possible, and I do this by accepting a Func<TService> as a ctor parameter rather than an instance of TService.
I was wondering whether it would be possible to add the ability to resolve a Func<TService> from a container even when I've only registered TService. Since you know how to instantiate TService, if I ask for a Func<TService> then you should be able to hand me a function that wraps up the Func<Container, TService> I've already given you, automatically passing in "this" as the parameter.
Then this sort of thing would be possible:
interface IBar { ... }
class Foo
{
public Foo(Func<IBar> barFactory) { ... }
}
builder.Register<IBar>(c => new Bar());
builder.Register<IFoo>(c => new Foo(c.Resolve<Func<IBar>>()));
The container would resolve Func<IBar> by returning a function that resolves IBar, passing itself into "c".
Does that make sense? I really think that would be a killer feature. I don't know if other IoC containers do this.
Matt