Grace icon indicating copy to clipboard operation
Grace copied to clipboard

Unintended behaviour resolving a constructor parameter

Open silkfire opened this issue 4 years ago • 4 comments

Today I stumbled upon a peculiar (although unintended) behaviour with Grace.
It seems as if providing a dependency that inherits from another dependency automatically resolves that dependency.

Repro:

public static class App
{
   public static void Main()
   {
      var container = new DependencyInjectionContainer();

      container.Configure(_ =>
      {
         _.Export<Service>().WithCtorParam(() => new SpecificDependency2());
      });

      var service = container.Locate<Service>();
   }
}

public class Service
{
   public Service(GenericDependency genericDependency, SpecificDependency2 specificDependency)
   {

   }
}

public class SpecificDependency : GenericDependency { }

public class SpecificDependency2 : GenericDependency { }

public abstract class GenericDependency { }

In the constructor of the Service class, GenericDependency is automatically resolved as SpecificDependency2, despite not being exported as such. This behaviour thus also overrides any exports I've specifically made for GenericDependency, they are instead always resolved as the provided specific constructor parameter.

silkfire avatar Jun 16 '20 11:06 silkfire

When resolving a parameter for a constructor the list of provided values is traversed looking for a matching parameter. In both cases the provided value were compatible so they matched . You can do something like .WithCtorParam(() => new SpecificDependency2()).Named(“specificDependency”) to make the matching more specific.

ipjohnson avatar Jun 16 '20 12:06 ipjohnson

Interesting; your answer now seems very obvious when I read it. Is there any way to disable this implicit behaviour?

silkfire avatar Jun 16 '20 18:06 silkfire

Looking at it there is a consider method but it's actually lacking the parameter so you can't actually do what you want. Seems like it would be worth while to add an overload that passes in the strategy and the ParameterInfo so you could force it to only match exact type

ipjohnson avatar Jun 17 '20 11:06 ipjohnson

That'd be much appreciated and quite useful! Thanks :)

silkfire avatar Jun 17 '20 12:06 silkfire