Key Concepts

Review core concepts you need to learn to master this subject

Dependency

// Object A - the class that depends on Object B class Reviewer { private EmailSender Sender = new EmailSender(); public SendReview(string lesson, string comments) { Sender.SendReview(lesson, comments); } } // Object B - the dependency class EmailSender { public SendReview(string lesson, string comments) { // Send an email with the Lesson and Comments } } static void Main(string[] args) { Reviewer reviewer = new Reviewer(). SendReview("Dependecy Injection", "Learned a ton!"); }

When one object (Object A) references another object (Object B), using its properties and methods, it means that the first object (A) depends on the second (B), making the second object (B) a dependency.

Dependency Injection
Lesson 1 of 1
  1. 1
    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 w…
  2. 2
    Before we get into ASP.NET, let’s take a look at a basic dependency in C#. In this program we have two important classes: * Trainer * LoudSpeaker In order to work correctly, the Trainer class nee…
  3. 3
    Here’s one problem with this situation: if the dependency changes, we also have to change the dependent class. This means that: * The code base is harder to maintain. * We can’t test classes indivi…
  4. 4
    Now that you’ve seen dependencies, imagine a codebase full of these dependent classes. Now imagine that the dependencies ALSO have dependencies. One change in one dependency could break dozens of o…
  5. 5
    Let’s begin by implementing the Dependency Inversion Principle: our class should depend on an abstraction (i.e. interface). An interface does not define any method bodies, but it forces any class …
  6. 6
    Let’s now implement the Inversion of Control Principle: Don’t call us, we’ll call you. In our specific project, it means that the Trainer class should not instantiate any dependency: LoudSpeaker o…
  7. 7
    We’re introducing a lot of terms here, so for recap: * The Dependency Inversion Principle (DIP) attempts to define and resolve the dependency problem through use of abstraction (interfaces). * …
  8. 8
    Well done! You’ve learned how to apply the DIP and IoC principles. So how does this factor into ASP.NET development? If you’ve ever logged information or added a database in your application, you’…
  9. 9
    You’ve seen where the dependency will be injected, but you haven’t seen how it actually happens. You might think that we do it like before: new IndexModel(new Logger()); We won’t need to type this…
  10. 10
    In the past exercise, we registered a service with the method AddScoped(). Each service has a “lifetime” and we wanted our service to have a lifetime “scoped to each user”. This lesson won’t get to…
  11. 11
    There are some common services that the ASP.NET framework has made easy-to-register by providing unique AddX methods. You might recognize: public void ConfigureServices(IServiceCollection services…
  12. 12
    Whew! You made it through a lot of content. Let’s review: * Dependencies in our code can cause problems, like: * The codebase is harder to maintain * We can’t test classes individually * Th…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo