Skip to content

Commit

Permalink
autofac#1361: adding (FAILING) test for OnActivated behavior when usi…
Browse files Browse the repository at this point in the history
…ng decorator
  • Loading branch information
srogovtsev committed Jan 8, 2023
1 parent 3c583ec commit 90284ef
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions test/Autofac.Specification.Test/Lifetime/LifetimeEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,17 +593,31 @@ public void OnReleaseForSingletonAsInterfaceStillFiresIfNotResolved()
Assert.True(instance.Released);
}

[Fact]
public void ActivatedAllowsBypassingCircularDependencies()
private static readonly Action<ContainerBuilder>[] ConfigurationsForCircularDependenciesImpl =
{
builder => { },
builder => builder.RegisterDecorator<InnerServiceDecorator, IInnerService>(),
builder => builder.RegisterDecorator<IInnerService>((_, __, toDecorate) => new InnerServiceDecorator(toDecorate)),
};

public static readonly object[][] ConfigurationsForCircularDependencies =
ConfigurationsForCircularDependenciesImpl.Select(c => new object[] {c}).ToArray();

[Theory]
[MemberData(nameof(ConfigurationsForCircularDependencies))]
public void ActivatedAllowsBypassingCircularDependencies(Action<ContainerBuilder> additionalContainerConfiguration)
{
var builder = new ContainerBuilder();

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

additionalContainerConfiguration(builder);

using var container = builder.Build();
var outer = container.Resolve<OuterService>();
Expand Down Expand Up @@ -654,18 +668,29 @@ public void Method(int param)

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

private class InnerService
private interface IInnerService
{
}

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

private class InnerServiceDecorator : IInnerService
{
public InnerServiceDecorator(IInnerService toDecorate)
{
}
}

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

0 comments on commit 90284ef

Please sign in to comment.