-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample "Tracking disposable instances with different lifetimes"
- Loading branch information
1 parent
3dad52b
commit 2e372c6
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
tests/Pure.DI.UsageTests/Advanced/TrackingDisposableWithDifferentLifetimesScenario.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |