Skip to content

Commit

Permalink
TypedFactory works with multi-Dispose calls and Release after Dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Danilov committed Dec 10, 2019
1 parent 5624286 commit 3d60707
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,31 @@ public void Disposing_factory_does_not_destroy_singleton_components()
Assert.IsFalse(component.Disposed);
}

[Test]
public void Disposing_factory_twice_does_not_throw()
{
Container.Register(
Component.For<IDisposableFactory>().AsFactory());
var factory = Container.Resolve<IDisposableFactory>();
factory.Dispose();
Assert.DoesNotThrow(() => factory.Dispose());
}

[Test]
public void Release_product_after_disposing_factory_does_not_throw()
{
Container.Register(
Component.For<IDisposableFactory>().AsFactory(),
Component.For<DisposableComponent>().LifeStyle.Transient);
var factory = Container.Resolve<IDisposableFactory>();
var component = factory.Create();
Assert.IsFalse(component.Disposed);

factory.Dispose();
Assert.IsTrue(component.Disposed);
Assert.DoesNotThrow(() => factory.Destroy(component));
}

[Test]
public void Factory_interface_can_be_hierarchical()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ public TypedFactoryInterceptor(IKernelInternal kernel, ITypedFactoryComponentSel

public void Dispose()
{
if (disposed)
{
return;
}

disposed = true;
scope.Dispose();
}

public void Intercept(IInvocation invocation)
{
if (disposed)
{
throw new ObjectDisposedException("this", "The factory was disposed and can no longer be used.");
}

// don't check whether the factory was already disposed: it may be a call to Dispose or
// Release methods, which must remain functional after dispose as well
FactoryMethod method;
if (TryGetMethod(invocation, out method) == false)
{
Expand Down Expand Up @@ -88,6 +90,11 @@ public void SetInterceptedComponentModel(ComponentModel target)

private void Release(IInvocation invocation)
{
if (disposed)
{
return;
}

for (var i = 0; i < invocation.Arguments.Length; i++)
{
scope.Release(invocation.Arguments[i]);
Expand All @@ -96,6 +103,11 @@ private void Release(IInvocation invocation)

private void Resolve(IInvocation invocation)
{
if (disposed)
{
throw new ObjectDisposedException("this", "The factory was disposed and can no longer be used.");
}

var component = ComponentSelector.SelectComponent(invocation.Method, invocation.TargetType, invocation.Arguments);
if (component == null)
{
Expand Down

0 comments on commit 3d60707

Please sign in to comment.