dadhi / DryIoc

DryIoc is fast, small, full-featured IoC Container for .NET
MIT License
1.03k stars 122 forks source link

Singleton Decorator to Scoped base shouldn't work, but does #516

Closed hutterm closed 2 years ago

hutterm commented 2 years ago

consider this testcase:

    public interface IFace{}

    public class A : IFace { }

    public class ADecorator : IFace
    {
        public IFace Inner { get; }
        public ADecorator(IFace inner)
        {
            Inner = inner;
        }
    }

    [Fact]
    public void CanRegisterSingletonDecoratorToScoped()
    {
        var c = new Container();
        c.RegisterMany<A>(Reuse.Scoped);
        c.Register<IFace, ADecorator>(Reuse.Singleton, setup: Setup.Decorator);

        using (var s1=c.OpenScope())
        {
            var f = s1.Resolve<IFace>();
            f.Should().BeOfType<ADecorator>();
            s1.Resolve<A>().Should().Be(((ADecorator)f).Inner);
        }

        using (var s2=c.OpenScope())
        {
            var f = s2.Resolve<IFace>();
            f.Should().BeOfType<ADecorator>();
            s2.Resolve<A>().Should().Be(((ADecorator)f).Inner); //throws because inner is A from the s1 scope!!!
        }
    }

I just accidentally hit this because I was changing a couple of reuses. This should ideally fail with verification!

dadhi commented 2 years ago

@hutterm Thanks for the test, will look.

dadhi commented 2 years ago

@hutterm Hi, the fix is out with v5.2.1