Skip to content

Commit

Permalink
put in the new DisposalLock semantics on the StructureMap container
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed May 3, 2016
1 parent 45df949 commit 32f8d1c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
2 changes: 1 addition & 1 deletion rakefile.rb
@@ -1,6 +1,6 @@
COMPILE_TARGET = ENV['config'].nil? ? "debug" : ENV['config']
RESULTS_DIR = "results"
BUILD_VERSION = '4.1.3'
BUILD_VERSION = '4.2.0'

tc_build_number = ENV["BUILD_NUMBER"]
build_revision = tc_build_number || Time.new.strftime('5%H%M')
Expand Down
8 changes: 4 additions & 4 deletions src/CommonAssemblyInfo.cs
Expand Up @@ -3,7 +3,7 @@
[assembly: AssemblyDescription("IoC Container for .Net")]
[assembly: AssemblyProduct("StructureMap")]
[assembly: AssemblyCopyright("Copyright 2004-2016 Jeremy D. Miller, Joshua Flanagan, Frank Quednau, Tim Kellogg, et al. All rights reserved.")]
[assembly: AssemblyTrademark("9d80cf86121690500551b99cc1a9ff8d1dac8160")]
[assembly: AssemblyVersion("4.1.3.51031")]
[assembly: AssemblyFileVersion("4.1.3.51031")]
[assembly: AssemblyInformationalVersion("4.1.3.51031")]
[assembly: AssemblyTrademark("45df94921016394e01a783e309461d70b3e4d6c3")]
[assembly: AssemblyVersion("4.1.3.50951")]
[assembly: AssemblyFileVersion("4.1.3.50951")]
[assembly: AssemblyInformationalVersion("4.1.3.50951")]
6 changes: 6 additions & 0 deletions src/StructureMap.Shared/Container.cs
Expand Up @@ -1086,6 +1086,10 @@ private IContainer GetNestedContainer(IPipelineGraph pipeline)

public void Dispose()
{
if (DisposalLock == DisposalLock.Ignore) return;

if (DisposalLock == DisposalLock.ThrowOnDispose) throw new InvalidOperationException("This Container has DisposalLock = DisposalLock.ThrowOnDispose and cannot be disposed until the lock is cleared");

if (_disposedLatch) return;
_disposedLatch = true;

Expand Down Expand Up @@ -1258,5 +1262,7 @@ public void Release(object @object)
assertNotDisposed();
TransientTracking.Release(@object);
}

public DisposalLock DisposalLock { get; set; } = DisposalLock.Unlocked;
}
}
27 changes: 27 additions & 0 deletions src/StructureMap.Shared/IContainer.cs
Expand Up @@ -401,5 +401,32 @@ public interface IContainer : IDisposable
/// </summary>
/// <param name="object">The object to dispose.</param>
void Release(object @object);


/// <summary>
/// Govern the behavior on Dispose() to prevent applications from
/// being prematurely disposed
/// </summary>
DisposalLock DisposalLock { get; set; }
}



public enum DisposalLock
{
/// <summary>
/// If a user calls IContainer.Dispose(), ignore the request
/// </summary>
Ignore,

/// <summary>
/// Default "just dispose the container" behavior
/// </summary>
Unlocked,

/// <summary>
/// Throws an InvalidOperationException when Dispose() is called
/// </summary>
ThrowOnDispose
}
}
43 changes: 43 additions & 0 deletions src/StructureMap.Testing/Pipeline/ContainerDisposalTester.cs
@@ -1,11 +1,54 @@
using Shouldly;
using System;
using StructureMap.Testing.Acceptance;
using Xunit;

namespace StructureMap.Testing.Pipeline
{
public class ContainerDisposalTester
{

[Fact]
public void default_lock_is_unlocked()
{
new Container().DisposalLock.ShouldBe(DisposalLock.Unlocked);
}

[Fact]
public void try_to_dispose_when_ignored_should_do_nothing()
{
var container = new Container(_ =>
{
_.For<IWidget>().Use<AWidget>();
});

container.DisposalLock = DisposalLock.Ignore;

container.Dispose();
container.Dispose();
container.Dispose();

container.Model.DefaultTypeFor<IWidget>().ShouldBe(typeof(AWidget));
}

[Fact]
public void try_to_dispose_when_locked_should_throw_an_invalid_operation_exception()
{
var container = new Container(_ =>
{
_.For<IWidget>().Use<AWidget>();
});

container.DisposalLock = DisposalLock.ThrowOnDispose;

Exception<InvalidOperationException>.ShouldBeThrownBy(() =>
{
container.Dispose();
});

container.Model.DefaultTypeFor<IWidget>().ShouldBe(typeof(AWidget));
}

[Fact]
public void disposing_a_main_container_will_dispose_an_object_injected_into_the_container()
{
Expand Down

0 comments on commit 32f8d1c

Please sign in to comment.