Learning about dependency injection in C# and .NET

Another course finished on my journey in learning C# and .NET. It was about one of the most important things in modern software development - dependency injection. I've learned how to use it in C# and .NET, and how to implement it in a real-world application. One chapter was about more advanced topics, such as using Scrutor to automatically register services.

Scrutor — Assembly scanning and decoration extensions for Microsoft.Extensions.DependencyInjection

It reminded me of the Spring Framework in Java, which has a similar feature called @ComponentScan. It allows you to automatically register services in the container, which is very useful when you have a lot of services and don't want to register them manually. Although one of the downsides of this approach is that it can be hard to understand what services are registered in the container. And there is no quick way to find out just by looking at the code as in Program.cs or Startup.cs.

As a side note, in Spring Boot, you don't have to explicitly specify this annotation because it's automatically enabled when using @SpringBootApplication.

The course I took was from Nick Chapsas - From Zero to Hero: Dependency injection in .NET with C#, a well-known YouTube instructor with many years of experience building production APIs in .NET. I already finished his course about REST APIs in .NET, and I wasn't disappointed in the new course also.

The course covered some topics, including:

  • Basic dependency injection and use cases
  • How to use ServiceCollection and ServiceProvider with different lifetimes
  • Resolving dependencies in different scenarios and projects
  • Advanced topics, such as using ServiceDescriptor, Add vs. TryAdd, Open Generics, and deep dive into lifetime scopes, etc.
  • Advanced techniques about custom scopes, decorators, avoiding service locator, and more
  • Introduction to Scrutor and ways it can simplify service registration with potential pitfalls
  • Creating your own minimal dependency injection framework by using a Dictionary

After completing the course, I got another certificate. Here it is:

Certificate for completing From Zero to Hero: Dependency injection in .NET with C#

It was a valuable investment to learn more about dependency injection in .NET and C#. If you have any questions or feedback, feel free to contact me.