Dependency injection
A pattern that implements Inversion of Control (IoC) for resolving dependencies into services.
Dependency injection is a pattern that implements Inversion of Control (IoC) for resolving dependencies into services.
A dependency is one or many parameters that exist in the service constructor.
When the service is created the dependency injection will find the correct implementation of the dependency for each constructor parameter.
The created service does not need to know anything about the implementation of the dependency. The implementation can easily be replaced inside the dependency container to use another implementation without any need to update the usage.
Dependency injection can be used with a service contract or implementation. A service contract is a preferred way.
Types of dependency
A dependency can be registered as three different types:
One instance is created of the implementation and used for all dependencies.
Create a new instance for each dependency.
Create a new instance in each scope. Reuse the instance for all dependencies in the scope.
A scope is specified with a start and stop.
For websites, a scope is created for each request.
For a scheduled task, a scope is created for each execution.