Grace
Grace copied to clipboard
Unintended behaviour resolving a constructor parameter
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.
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.
Interesting; your answer now seems very obvious when I read it. Is there any way to disable this implicit behaviour?
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
That'd be much appreciated and quite useful! Thanks :)