What is the point in one type argument in Register<T> instead of two argument types Register<T,U>?

96 Views Asked by At

In dependency injection I understand why we need Register() with 2 type arguments.

myContainer.Register<I1,A>();

From now on, parameters of type I1, in ctors, will get the instance of A. It's great. We don't need to deal with the creation and the Dependency injection container will bring us what we need (no matter if it is Singleton or not).

Now, what I don't understand is

myContainer.Register<B>();

We don't map any interface to this concrete type B.

What is it useful for?

1

There are 1 best solutions below

0
Paulo On

You don't need interfaces for dependency injection. But dependency injection is much more useful with them!

you can register concrete types with the service collection and ASP.NET Core will inject them into your classes without problems.

The benefit you get by injecting them over simply creating instances

Should you do it? No.

Dependency Injection is a tool for the principle of Dependency Inversion : https://en.wikipedia.org/wiki/Dependency_inversion_principle

as it's described in SOLID

one should “depend upon abstractions, not concretions."