Defer component creation until resolve

Nov 6, 2008 at 9:04 PM
Quick (hopefully) question with Unity - deferred creation of components. Autofac lets me do funky things like:

builder.Register(c =>
{
    var factory = new ClientFactory<IMyService>();
    return factory.CreateProxy();
}).As<IMyService>().FactoryScoped();

In this case I'm registering an expression that won't get executed until I attempt to do container.Resolve<IMyService>().

Is there some way to pull this off in Unity?

Thanks,
Matt
Nov 6, 2008 at 10:26 PM
Check out the StaticFactoryExtension we ship in the box. It does exactly this.

Be aware that if you use it, the delegate you register completely replaces the normal creation logic, so it is, for example, responsible for injecting properties directly.

-Chris
Nov 6, 2008 at 11:27 PM
Awesome - thanks Chris - I'll check it out!