Course info

Rating

(1080)

Level

Intermediate

Updated

Oct 21, 2013

Duration

2h 38m

Description

What is Dependency Injection? The answers to the question seem to cause more confusion than help. This course will get us started on the road to understanding. We'll take an example-based approach to see the problems caused by tightly-coupled code. We'll add Dependency Injection to trade that tight-coupling for loose-coupling. And that loose-coupling makes our applications much easier to extend, maintain, and test. Finally, we'll see how Dependency Injection Containers can provide benefits that would be difficult to implement ourselves. This just scratches the surface. Once we're done, we'll be ready to start our journey on the Dependency Injection superhighway.

About the author

Jeremy Clark is an application developer, technical speaker, and Microsoft MVP with over 13 years of experience in all aspects of the development lifecycle. After hours, he puts together demos and technical articles for JeremyBytes.com, a website focusing on .NET technologies.

Section Introduction Transcripts

Loose CouplingWe've seen the problems of having tightly-coupled code. Our application becomes difficult to extend and test. Now it's time to break that Tight-Coupling. As we'll see, we can do this very easily using some of the concepts of Dependency Injection. We want to be able to easily extend our application so that we can add more than one type of data store. We want to be able to add functionality with a Client-Side Cache and we want to isolate our various layers so that we can easily unit test each one. Dependency Injection helps us with all three of these by allowing us to create loosely coupled code. We'll use Constructor Injection to break our Tight-Coupling and move responsibilities to the appropriate classes. This is actually a lot easier than it sounds and once we have this in place, we'll be able to easily extend and test our application.

Taking Advantage of Loose CouplingWe spent some time to make sure that our code is loosely coupled. Now it's time to reap the benefits. This is my favorite part because we get to see the payoff of our effort. We'll see just how easy it is to extend our application now. We'll revisit two of the scenarios that we looked at earlier, Additional Repositories and Client-Side Caching. Now that we have a good foundation built, these are trivial to implement. Let's start by looking at adding other Repositories.

Unit TestingWhen we have good unit tests for our code, we have proof that our code does what we expect it to do. Although it's technically possible to unit test almost any piece of code, our testing gets much easier when we have loosely coupled code. Dependency Injection offers us several benefits. A big one is Testability. As we'll see, our unit tests are very easy to implement now that we have loosely coupled code.

DI ContainersDependency Injection Containers are an extremely important part of Dependency Injection. In fact, I would not recommend going without one in a production application. The reason that we waited so long to talk about DI Containers is that they do a little too much for us when we're first learning about Dependency Injection. They hide a lot of the concepts, which is good in the long term, but it ends up looking like magic in the short term. Our goal is to get comfortable with Dependency Injection. We've learned many of the basic concepts and have seen the benefits for ourselves. Now that we have a good foundation, it's time to see what DI Containers can do for us and why it's a really good idea to have one when we use Dependency Injection in our own code.