ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

Wrong constructor overload gets called #231

Closed tdctaz closed 5 years ago

tdctaz commented 5 years ago

When Locating a class with a default constructor and a constructor with params object[] args the overload with the args always gets invoked even though no configuration for args was Exported. Also the args contains a single object instance.

Version: 7.0.0

Repo:

    var container = new DependencyInjectionContainer();
    container.Configure(x => x.Export<Test>());
    var test = container.Locate<Test>();
    public class Test
    {
        public Test() {}

        public Test(params object[] args)
        {
            throw new Exception("Wrong constructor called");
        }
    }
ipjohnson commented 5 years ago

Grace will always take the constructor with the most parameters it can satisfy. array's and lists are something it will satisfy by default (it's empty but it's still satisfied). So not what you're expecting but it's not a bug because the container can't tell if you intended to have >= 0 exports or > 0.

You have a couple options. If this is just a one off you can tell the container which constructor to use specifically. The other option is to provide your own constructor selector that takes into account if a collection will be null and not pick it.

tdctaz commented 5 years ago

I'll try to make a constructor selector.

But the array i'm getting is not an empty array. I'm getting an array with one object element in it.

ipjohnson commented 5 years ago

Sounds like the container auto registered the class Object and returned an instance in the array.

While not proper I don’t want to change the behavior of discovering object till 8.0 as it’s a breaking change.

ipjohnson commented 5 years ago

Were you able to work out a solution?

ipjohnson commented 5 years ago

I'm going to close this out