Skip to Content
Learn
Dependency Injection
Introduction to Dependency Injection

You may have noticed that ASP.NET apps aren’t built from the ground up. We rely on other programmers’ existing code to get started and quickly add new features. For example, think how little code we need to add a database to a web app.

When we rely on other programmer’s code, we say that our code has dependencies. Our code depends on other code. We can do a lot with dependencies, but they also add a new set of problems. Dependencies make our code:

  • Difficult to unit test
  • Difficult to update
  • Messy, i.e. responsibilities of classes are mixed

In this lesson, we are going to better understand those problems, learn about some design principles that avoid those problems, and finally look at some tools in ASP.NET that employ those principles.

By the end of this lesson you will be able to:

  • Define the dependency inversion and inversion of control principles
  • Define dependency injection and describe its benefits
  • Use basic dependency injection in C#
  • Use the dependency injection and the IoC container in ASP.NET

Instructions

Look at Trainer.cs. This class depends on the LoudSpeaker class. Why do you think that’s so?

Folder Icon

Sign up to start coding

Already have an account?