Skip to content

Commit

Permalink
Add sample "Tracking disposable instances with different lifetimes"
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Nov 17, 2024
1 parent 3dad52b commit 2e372c6
Showing 1 changed file with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
$v=true
$p=101
$d=Tracking disposable instances with different lifetimes
*/

// ReSharper disable CheckNamespace
// ReSharper disable UnusedMember.Local
// ReSharper disable UnusedParameterInPartialMethod
// ReSharper disable ArrangeTypeModifiers
// ReSharper disable ArrangeTypeMemberModifiers
// ReSharper disable UnusedMemberInSuper.Global

namespace Pure.DI.UsageTests.Advanced.TrackingDisposableWithDifferentLifetimesScenario;

using Xunit;

// {
interface IDependency
{
bool IsDisposed { get; }
}

class Dependency : IDependency, IDisposable
{
public bool IsDisposed { get; private set; }

public void Dispose() => IsDisposed = true;
}

interface IService
{
public IDependency Dependency { get; }

public IDependency SingleDependency { get; }
}

class Service(
Func<Owned<IDependency>> dependencyFactory,
[Tag("single")] Func<Owned<IDependency>> singleDependencyFactory)
: IService, IDisposable
{
private readonly Owned<IDependency> _dependency = dependencyFactory();
private readonly Owned<IDependency> _singleDependency = singleDependencyFactory();

public IDependency Dependency => _dependency.Value;

public IDependency SingleDependency => _singleDependency.Value;

public void Dispose()
{
_dependency.Dispose();
_singleDependency.Dispose();
}
}

partial class Composition
{
static void Setup() =>
DI.Setup()
.Bind().To<Dependency>()
.Bind("single").As(Lifetime.Singleton).To<Dependency>()
.Bind().To<Service>()

// Composition root
.Root<Service>("Root");
}
// }

public class Scenario
{
[Fact]
public void Run()
{
// {
var composition = new Composition();
var root1 = composition.Root;
var root2 = composition.Root;

root2.Dispose();

// Checks that the disposable instances
// associated with root1 have been disposed of
root2.Dependency.IsDisposed.ShouldBeTrue();
// But the singleton is still not disposed of
root2.SingleDependency.IsDisposed.ShouldBeFalse();

// Checks that the disposable instances
// associated with root2 have not been disposed of
root1.Dependency.IsDisposed.ShouldBeFalse();
root1.SingleDependency.IsDisposed.ShouldBeFalse();

root1.Dispose();

// Checks that the disposable instances
// associated with root2 have been disposed of
root1.Dependency.IsDisposed.ShouldBeTrue();
// But the singleton is still not disposed of
root1.SingleDependency.IsDisposed.ShouldBeFalse();

composition.Dispose();
root2.SingleDependency.IsDisposed.ShouldBeTrue();
root1.SingleDependency.IsDisposed.ShouldBeTrue();
// }
new Composition().SaveClassDiagram();
}
}

0 comments on commit 2e372c6

Please sign in to comment.