JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
563 stars 118 forks source link

Wrong container passed to OnCreation() lambda when using nested container #330

Closed tmc101 closed 2 years ago

tmc101 commented 2 years ago

When Lamar constructs an instance using a nested container, should it pass the nested container to the OnCreation lambda?

Currently, Lamar passes the root container.

The unit test below fails on the last check - but I think it should pass.

This is related to issue #324 which was closed. I do not have permission to re-open that ticket as suggested, so I added this one - which distills the issue:

using System;
using System.Diagnostics;
using Shouldly;
using Xunit;

namespace Lamar.Testing.Bugs
{
    public class Bug_324_scoping_within_on_creation
    {
        public interface IWidget
        {
        }

        public class Widget : IWidget
        {
            public Widget(){}
        }

        [Fact]
        public void oncreation_should_receive_the_constructing_container()
        {
            IServiceContext containerPassedToOnCreationLambda = null;

            var rootContainer = new Container(x =>
            {
                x.For<IWidget>().Use<Widget>()
                .OnCreation((context, concrete) => {
                    containerPassedToOnCreationLambda = context;
                    return concrete;
                })
                .Scoped();

            });

            var rootWidget = rootContainer.GetInstance<IWidget>();

            // The rootContainer should have been passed to the OnCreation lambda.
            containerPassedToOnCreationLambda.ShouldBeSameAs(rootContainer);

            var nestedContainer = rootContainer.GetNestedContainer();

            var nestedWidget = nestedContainer.GetInstance<IWidget>();

            // The nestedContainer should have been passed to the OnCreation lambda.
            containerPassedToOnCreationLambda.ShouldBeSameAs(nestedContainer);
        }
    }
}
jeremydmiller commented 2 years ago

@tmc101 Got it. Fix will be in Lamar v8.0.1 later today.

jeremydmiller commented 2 years ago

Closed by https://github.com/JasperFx/lamar/commit/a7c0faa196b8af14852c1c5742df02d11bab1848