Skip to content

Commit

Permalink
Allows to resolve IOwned to manage disposable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Apr 10, 2024
1 parent b7735b4 commit 349f9b6
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Pure.DI.Core/Features/Default.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ private static void Setup()
Lifetime.Transient,
Lifetime.PerResolve,
Lifetime.PerBlock)
.Bind<global::Pure.DI.IOwned>().To<Owned>()
.Bind<global::Pure.DI.IOwned>().To(ctx =>
{
ctx.Inject<Owned>(out var owned);
return owned;
})
.Bind<global::Pure.DI.Owned<TT>>()
.As(Lifetime.PerBlock)
.To(ctx => {
ctx.Inject<Owned>(out var owned);
ctx.Inject<IOwned>(out var owned);
ctx.Inject<TT>(ctx.Tag, out var value);
return new Owned<TT>(value, owned);
})
Expand Down
144 changes: 143 additions & 1 deletion tests/Pure.DI.IntegrationTests/AccumulatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public static void Main()
}

[Fact]
public async Task ShouldSupportOwned()
public async Task ShouldSupportGenericOwned()
{
// Given

Expand Down Expand Up @@ -352,6 +352,148 @@ public static void Main()
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(ImmutableArray.Create("True", "False", "True"));
}

[Fact]
public async Task ShouldSupportIOwned()
{
// Given

// When
var result = await """
using System;
using System.Collections.Generic;
using Pure.DI;

namespace Sample
{
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; }
}

class Service : IService
{
public Service(IDependency dependency)
{
Dependency = dependency;
}

public IDependency Dependency { get; }
}

partial class Composition
{
void Setup() =>
DI.Setup(nameof(Composition))
.Bind<IDependency>().To<Dependency>()
.Bind<IService>().To<Service>()
.Root<(IService Service, IOwned Owned)>("Root");
}

public class Program
{
public static void Main()
{
var composition = new Composition();
var root1 = composition.Root;
var root2 = composition.Root;
root2.Owned.Dispose();
Console.WriteLine(root2.Service.Dependency.IsDisposed);
Console.WriteLine(root1.Service.Dependency.IsDisposed);
root1.Owned.Dispose();
Console.WriteLine(root1.Service.Dependency.IsDisposed);
}
}
}
""".RunAsync();

// Then
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(ImmutableArray.Create("True", "False", "True"));
}

[Fact]
public async Task ShouldSupportOwned()
{
// Given

// When
var result = await """
using System;
using System.Collections.Generic;
using Pure.DI;

namespace Sample
{
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; }
}

class Service : IService
{
public Service(IDependency dependency)
{
Dependency = dependency;
}

public IDependency Dependency { get; }
}

partial class Composition
{
void Setup() =>
DI.Setup(nameof(Composition))
.Bind<IDependency>().To<Dependency>()
.Bind<IService>().To<Service>()
.Root<(IService Service, Owned Owned)>("Root");
}

public class Program
{
public static void Main()
{
var composition = new Composition();
var root1 = composition.Root;
var root2 = composition.Root;
root2.Owned.Dispose();
Console.WriteLine(root2.Service.Dependency.IsDisposed);
Console.WriteLine(root1.Service.Dependency.IsDisposed);
root1.Owned.Dispose();
Console.WriteLine(root1.Service.Dependency.IsDisposed);
}
}
}
""".RunAsync();

// Then
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(ImmutableArray.Create("True", "False", "True"));
}

#if ROSLYN4_8_OR_GREATER
[Fact]
Expand Down

0 comments on commit 349f9b6

Please sign in to comment.