So I use windsor dependency injection:
container.Register(Component.For<IX>().ImplementedBy<X>().LifestyleSingleton());
My problem (such as it is) is that in the X class's constructor, I cannot easily see that that constructor is used. In fact, Visual Studio tells me that it is not in use:
This is mildly annoying - it becomes difficult to spot dead code.
How do you fix this?
1 Answer 1
There is nothing to fix.
The constructor is invoked by the container to initialize the object when being resolved.
IX x = container.Resolve<IX>();
Thus Visual Studio wont see any explicit calls to that constructor from the code.
Ideally, the target class should only have one constructor following explicit dependencies principle.
public class X : IX {
private readonly IDependency dependency;
public X(IDependency dependency) {
this.dependency = dependency;
//...
}
//...
}
This will ensure that the lone constructor is the one used by the container when resolving the class.
If one is actively testing their code or is following TDD, then there should be references to the used code in supporting tests.
[TestMethod]
public void X_Should_Do_Something() {
// Arrange
//...
IX subject = new X(...);
// Act
var result = subject.SomeMember();
// Assert
//...
}
where explicit calls are used to arrange the subject under test.
4 Comments
services.AddTransient<IX, X>() or in OP's case .ImplementedBy<X>())Explore related questions
See similar questions with these tags.