I am confused about dependency injection in .NET Core where I have many intercoupled dependencies. I have a class MyClass
implementing an interface IMyClass
as follows:
public class MyClass : IMyClass
{
private IClass classA;
private IClass classB;
public MyClass (ClassA classA, ClassB classB)
{
this.classA = classA;
this.classB = classB;
}
....
}
The classes ClassA
and ClassB
are implementations of interface IClass
as follows:
public class ClassA : IClass
{
public ClassA (many other DI)
{
}
}
public class ClassB : IClass
{
private IClass baseClass;
public ClassB (IClass baseClass, ...)
{
this.baseClass = baseClass;
....
}
}
In my startup file, how should I register my dependencies. I have tried the following, which doesn't work:
services.AddSingleton<ClassA>();
services.AddSingleton<IMyClass, MyClass>();
Can someone please explain what is the issue here and the solution to this?