Skip to content

Commit

Permalink
autofac#1361: explicit test for OnActivated as circular dependency by…
Browse files Browse the repository at this point in the history
…pass
  • Loading branch information
srogovtsev committed Jan 8, 2023
1 parent 890456a commit 3c583ec
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/Autofac.Specification.Test/Lifetime/LifetimeEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,25 @@ public void OnReleaseForSingletonAsInterfaceStillFiresIfNotResolved()
Assert.True(instance.Released);
}

[Fact]
public void ActivatedAllowsBypassingCircularDependencies()
{
var builder = new ContainerBuilder();

builder.RegisterType<OuterService>()
.SingleInstance();
builder.RegisterType<InnerService>();
builder.RegisterType<InnermostService>()
.SingleInstance()
.OnActivated(args => args.Instance.Outer = args.Context.Resolve<OuterService>());

using var container = builder.Build();
var outer = container.Resolve<OuterService>();
var innerMost = container.Resolve<InnermostService>();

Assert.Same(outer, innerMost.Outer);
}

private interface IService
{
}
Expand Down Expand Up @@ -632,4 +651,23 @@ public void Method(int param)
Param = param;
}
}

private class OuterService
{
public OuterService(InnerService service)
{
}
}

private class InnerService
{
public InnerService(InnermostService service)
{
}
}

private class InnermostService
{
public OuterService Outer { get; set; }
}
}

0 comments on commit 3c583ec

Please sign in to comment.