question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple constructors - how to specify which one to use?

See original GitHub issue

Hello 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:closed
  • Created 3 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ipjohnsoncommented, May 13, 2020

I’ll do a release in the next couple days and comment here when I do it

0reactions
bangievcommented, May 22, 2020

Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best way to handle multiple constructors in Java
Whichever way you choose, it's a good practice to have one main constructor, that just blindly assigns all the values, even if it's...
Read more >
Multiple constructors / Examples / Processing.org
A class can have multiple constructors that assign the fields in different ways. Sometimes it's beneficial to specify every aspect of an object's...
Read more >
Java Constructors
A class can have multiple constructors, as long as their signature (the parameters they take) are not the same. You can define as...
Read more >
Are Multiple Constructors possible in Java
There can be multiple constructors in a class. However, the parameter list of the constructors should not be same.
Read more >
Removing repetitive code (overloading methods and ...
The technique of having two (or more) constructors in a class is known as constructor overloading. A class can have multiple constructors that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found