From Zero to Hero: Dependency Injection in .NET

Master one of .NET's core pillars

About This Course

It is not an exaggeration to say that Dependency Injection is one of the core pillars of building modern .NET applications. It allows us to write cleaner code, both in structure but also in testability. There is a reason why since .NET Core launched (now simply .NET), it has been part of the framework and part of every codebase that is using it. It is a skill that every single .NET developer needs to master, and in this course, Nick will show you how to do just that.

Course Curriculum

4h 40m 9 sections
Welcome
02:34 Free preview
What will you learn in this course?
01:49 Free preview
Who is this course for and prerequisites
01:21 Free preview
The problem with dependencies
06:47
Why Dependency injection is necessary
05:13
A practical example of the dependency problem
10:25
A less obvious example of a Dependency Injection use-case
04:24
Dependency injection benefits past testability
01:11
Injecting Classes vs Abstract classes vs Interfaces
01:51
So do you have to do all that manually??
01:21
Section recap
01:09
Introduction
00:38
The simplest setup with Dependency Injection
06:34
The ServiceCollection
02:00
The ServiceProvider
01:46
All of the above, in an API
05:27
The different types of dependency lifetimes
04:30
The Transient lifetime
02:08
The Singleton lifetime
02:08
The Scoped lifetime
02:31
GetService vs GetRequiredService
03:38
Generic-based registration vs implementation-based registration
03:31
Registration approaches
06:04
The Startup.cs and changes after .NET 6
02:09
Third party libraries
01:49
Section recap
01:09
Resolving dependencies in different project types
00:21
Resolving dependencies from the constructor
00:59
Resolving dependencies from the method
01:27
Resolving dependencies in a console setup
01:17
Resolving dependencies from the HttpContext
01:09
Resolving dependencies from Action Filters as Attributes
02:42
Resolving dependencies from Service Filters
02:42
Resolving dependencies from Middleware
02:32
Resolving dependencies in Minimal APIs
03:13
Resolving dependencies in Razor Views & Pages
01:28
Resolving dependencies in Blazor
01:00
Resolving dependencies in gRPC Services
01:16
Resolving dependencies in Hosted Services
02:38
Resolving dependencies in service registration
01:54
Section recap
00:31
Do you need an interface for everything?
04:59
Choosing the right dependency lifetime
02:31
Dependency resolving issues and limitations
07:43
Registering open generics
02:38
Registering multiple interface implementations
03:22
The ServiceDescriptor
04:22
Add vs TryAdd
04:22
TryAddEnumerable
03:31
Replacing dependencies
05:47
Cleaning up service registration
03:22
Section recap
00:59
Creating a custom scope
05:42
The service locator anti-pattern
02:48
When service locator makes sense
20:37
Avoiding capturing dependencies
03:14
Avoiding multiple service providers
03:18
Creating decorators
07:11
The future of dependency injection
03:55
Section recap
00:47
What is Scrutor?
01:34
Registering service decorators
03:58
Surprise optional refactoring lecture
08:26
Service registration by scanning
13:09
Interface marking
03:28
Attribute marking
03:13
Namespace filtering
02:19
Using the ServiceDescriptor attribute
02:47
Using RegistrationStrategies
02:24
Potential pitfalls
01:52
Section recap
00:46
Why should we even bother?
01:25
The design
02:57
The implementation
19:57
Extending the main implementation
10:36
Section recap
00:33
Course conclusion
00:32

Meet Your Instructor

Nick Chapsas

Nick Chapsas

Nick Chapsas is a .NET & C# content creator, educator and a Microsoft MVP for Developer Technologies with years of experience in Software Engineering and Engineering Management.

He has worked for some of the biggest companies in the world, building systems that served millions of users and tens of thousands of requests per second.

Nick creates free content on YouTube and is the host of the Keep Coding Podcast.

View all courses by Nick Chapsas

What's New

Getting Started: Building .NET Applications on AWS
course

Getting Started: Building .NET Applications on AWS

Learn how to build and deploy .NET applications on AWS using CDK, Lambda, DynamoDB, S3, and more.

Learn More
What's new in C# 14
blog

What's new in C# 14

This guide covers every new C# 14 feature, explains its benefits, and provides practical code examples to help you navigate how you can use them.

Learn More
Let's Build It: AI Chatbot with RAG in .NET Using Your Data
course

Let's Build It: AI Chatbot with RAG in .NET Using Your Data

Build a Retrieval-Augmented Generation (RAG) chatbot that can answer questions using your data.

Learn More
From Zero to Hero: SignalR in .NET
course

From Zero to Hero: SignalR in .NET

Enable enterprise-grade real-time communication for your web apps with SignalR.

Learn More
Deep Dive: Solution Architecture
course

Deep Dive: Solution Architecture

Master solution architecture and turn business needs into scalable, maintainable systems.

Learn More
Migrating: ASP.NET Web APIs to ASP.NET Core
course

Migrating: ASP.NET Web APIs to ASP.NET Core

A step-by-step process to migrate ASP.NET Web APIs from .NET Framework to ASP.NET Core.

Learn More
Getting Started: Caching in .NET
course

Getting Started: Caching in .NET

Let's make the hardest thing in programming easy for .NET software engineers.

Learn More
From Zero to Hero: Testing with xUnit in C#
course

From Zero to Hero: Testing with xUnit in C#

Learn how to test any codebase in .NET with the latest version of xUnit, the industry-standard testing library.

Learn More
Create a ChatGPT Console AI Chatbot in C#
blog

Create a ChatGPT Console AI Chatbot in C#

This walkthrough is your hands-on entry point to create a basic C# console application that talks to ChatGPT using the OpenAI API.

Learn More