IoC lock - ISubDependencyResolver that Release (...) supports?
Is it possible to build something that works like an ISubDependencyResolver but also supports Release (...)?
I have a situation where I want to be able to enable a derived Fruit class in Blender's constructor:
abstract class Fruit
{
}
class AppleBlender
{
AppleBlender(Apple a)
{
}
}
Apple is unfortunately in a different assembly that I don't want to download until I need it, because there are hundreds of different kinds of fruit with their own assembly.
ISubDependencyResolver works great for this, except that some of my Fruit are disposable, so I need a way to deallocate them.
Is exiting the DefaultDependencyResolver the only way to achieve this?
EDIT: More detailed example.
[TestFixture]
public class SubResolverFixture
{
[Test]
public void ResolvedInstanceShouldBeDisposed()
{
IKernel kernel = new DefaultKernel();
kernel.Resolver.AddSubResolver(new TestResolver());
kernel.Register(Component.For<AppleBlender>().LifeStyle.Transient);
AppleBlender ab = kernel.Resolve<AppleBlender>();
kernel.ReleaseComponent(ab);
Assert.That(ab.IsDisposed);
Assert.That(ab.Apple.IsDisposed);
}
}
public class TestResolver : ISubDependencyResolver
{
public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
{
return new Apple();
}
public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
{
return typeof(Fruit).IsAssignableFrom(dependency.TargetType);
}
}
public abstract class Fruit : IDisposable
{
public bool IsDisposed
{
get;
set;
}
public void Dispose()
{
IsDisposed = true;
}
}
public class Apple : Fruit
{
}
public class AppleBlender : IDisposable
{
public AppleBlender(Apple apple)
{
Apple = apple;
}
public Apple Apple
{
get;
set;
}
public bool IsDisposed
{
get;
set;
}
public void Dispose()
{
IsDisposed = true;
}
}
Basically, I want to treat "new Apple ()" as a transient object that needs to be deleted. I totally agree on a completely different track on this one, but the type "Apple" needs to be loaded at solution time (not startup time).
Windsor 2.5 supports this OOTB for components registered with UsingFactoryMethod
(or more broadly for components with implementation type set to LateBoundComponent
)