Multiple constructors - how to specify which one to use?
See original GitHub issueHello and thank you for you efforts in developing and maintaining Grace! We’ve selected it as a candidate for replacing Ninject in our team project and I stumbled upon the following scenario which surprised me:
I have a setup in which there is a generic type with multiple constructors. I decorated the parameterless constructor with the [Import]
attribute and expected that Grace would select that one which did not happen. You can see the test I wrote for the scenario:
public class Dependency { }
public class Service<T>
{
[Import]
public Service()
{
ParameterlessConstructorCalled = true;
}
public Service(T value)
{
Value = value;
}
public bool ParameterlessConstructorCalled { get; }
public T Value { get; }
}
[Fact]
public void MultipleConstructors_DecorateSpecificConstructorWithImport_DecoratedConstructorUsed()
{
var container = new DependencyInjectionContainer();
var service = container.Locate<Service<Dependency>>();
Assert.True(service.ParameterlessConstructorCalled);
}
Is that not how the Import
attribute is supposed to work? Or how would you suggest to achieve that explicit constructor selection?
I tried adding the following configuration to the container as well but that didn’t work either:
container.Configure(c => c
.Export(typeof(Service<>))
.ImportConstructor(typeof(Service<>).GetConstructor(Type.EmptyTypes)));
I guess I could make this work by excluding the Dependency type from auto registration but I wouldn’t go that way due to the multiple types that are potentially passed to the generic.
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (8 by maintainers)
Top GitHub Comments
I’ll do a release in the next couple days and comment here when I do it
Thank you!