JasperFx / lamar

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

Decoartors: Inner Resolution Fails When Parameter Name Matches Type Name #146

Closed sdepouw closed 5 years ago

sdepouw commented 5 years ago

This may be related to #28 but I am not certain. I'm using v3.0.1 NuGet package of Lamar.

Say I have the following (based on Decorator documentation):

public interface IWidget
{
    void DoStuff();
}

public class WidgetDecorator : IWidget
{
    private readonly IWidget _widget;
    public IWidget Widget => _widget;

    public WidgetDecorator(IWidget widget)
    {
        _widget = widget;
    }

    public void DoStuff() { }
}

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

I have the following (NUnit) unit test:

[Test]
public void UseCorrectTypes()
{
    Container container = new Container(_ =>
    {
        _.For<IWidget>().DecorateAllWith<WidgetDecorator>();
        _.For<IWidget>().Use<Widget>();
    });

    IWidget instance = container.GetInstance<IWidget>();

    Assert.IsInstanceOf<WidgetDecorator>(instance); // Passes
    WidgetDecorator widgetDecorator = (WidgetDecorator)instance;
    Assert.IsInstanceOf<Widget>(widgetDecorator.Widget); // Fails: "Widget" is null
}

This unit test currently fails, because the constructor of WidgetDecorator takes in a parameter named widget, which somehow screws Lamar's registration up. If I change the parameter name to anything but widget (whether renaming it to something else, or just changing the casing to Widget, wiDget, etc.), the test passes.

It appears to be related to the concrete type Widget's name. If the parameter name matches the concrete type name, it blows up.

This is being done in part of a project of converting StructureMap over to Lamar. The original StructureMap implementation does not run into this issue (the unit test passes without issue).

jeremydmiller commented 5 years ago

It's not the registration that gets screwed up, but the generated Expressions. I've got the fix for this done that will be in Lamar 3.0.2 either later today or at least early this week. Thank you for the repro steps here.

jeremydmiller commented 5 years ago

@sdepouw Misspoke, Lamar 3.0.3, and I'm starting the mechanics to release that right now