I study different DI patterns.
And now I interested in the lazy life-time implementation.
For example, I want to write a proxy class that hides the factory behind a service’s interface.
Can any of existing IoC containers (.NET) create this kind of proxy class dynamically at runtime?
interface IService
{
void Foo();
void Bar();
}
class ServiceFactoryProxy : IService
{
private readonly Func<IService> _factory;
public ServiceFactoryProxy(Func<IService> factory)
{
if (factory == null) throw new ArgumentNullException("factory");
_factory = factory;
}
public void Foo()
{
_factory().Foo();
}
public void Bar()
{
_factory().Foo();
}
}
Here is how it can be done in Castle:
http://litemedia.info/lazy-loading-property-with-castle-dynamicproxy2