Contents
What is static and dynamic dependency injection?
Knowing dynamic dependencies (who calls who at runtime) can make sense for example to profile performance or to fix some particular bugs. But most of the time invested to understand a program is generally spent in browsing static dependencies.
Can singleton class be static?
A singleton allows a class for which there is just one, persistent instance across the lifetime of an application. While a static class allows only static methods and and you cannot pass static class as parameter. A Singleton can implement interfaces, inherit from other classes and allow inheritance.
What are different types of dependency injection?
There are three types of dependency injection — constructor injection, method injection, and property injection.
What do you mean by dependency injection in.net?
.NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in.NET is a first-class citizen, along with configuration, logging, and the options pattern. A dependency is an object that another object depends on.
What should you avoid when using dependency injection?
When designing services for dependency injection: Avoid stateful, static classes and members. Avoid creating global state by designing apps to use singleton services instead. Avoid direct instantiation of dependent classes within services. Direct instantiation couples the code to a particular implementation.
When to use direct instantiation or dependency injection?
Direct instantiation couples the code to a particular implementation. Make services small, well-factored, and easily tested. If a class has many injected dependencies, it might be a sign that the class has too many responsibilities and violates the Single Responsibility Principle (SRP).
How to avoid service locator in dependency injection?
Avoid using the service locator pattern. For example, don’t invoke GetService to obtain a service instance when you can use DI instead. Another service locator variation to avoid is injecting a factory that resolves dependencies at runtime. Both of these practices mix Inversion of Control strategies.
https://www.youtube.com/watch?v=GAOCe-2nXqc