What is Dependency Injection?

A picture


Donny Bottse



Reading time

6 minutes

A set of software design principles and patterns that enable us to develop loosely coupled code.

Why the loose coupling of code

A couple of benefits from loosely coupled code is

  • easy to extend,
  • easy to test
  • easy to maintain.

It also facilitates late binding and even though our code supports it we won’t be dealing with this in depth in this article.

Application architecture

Our application follows the mvvm pattern and thus we have the following layers

  • View contains the UI elements of our application
  • Presentation is our UI logic
  • Data access interacts with our data store
  • Data store is where our actual data resides

A picture

If we take a closer look after we’ve downloaded of cloned our application we can see a layered structure.

Unfortunately our code, despite it being structured, is tightly coupled. This means our classes and objects are dependent on each other and therefore reduces flexibility and re-usability.

A picture
A picture

If we take a look in our view we can see that our constructor is creating a new MediaViewModel.

When taking a closer look at our presentation layer, the same problem occurs.

If we take a look at the other layers we can see a similar problem which eventually means that our view is tightly coupled to our data storage.

A picture

As we can see the flexibility and re-usability is heavily reduced because of tight coupling. If we wish to extend our presentation layer to not only use a service reader but also some another type of reader (cloud, text) we’d have to include all kinds of conditional logic in our presentation layer which will make it very hard to maintain (this makes sense since we would be violating the single responsibility rule).

Improvements to loosely coupled code

After refactoring of the code, we can see that both single responsibly principle and the dependency inversion principle are coming into play.

Now the responsibility for object creation has shifted.

A picture
A picture

When creating tests for our code we don’t have to use or instantiate any production code.

Instead we get our own implementation for whatever object we need.

A couple of notes when running the application(s)

  • Clone or download the source code ( this contains two folders, one with the Tightly coupled code and another with Loosely coupled code)
  • In order for the tightly coupled code to run make sure you start Media.Service project in the DataAccess folder. Then run the MediaViewer
  • The solution that contains the loosely coupled code uses the CSVReader instead of the ServiceReader. In the App.xaml.cs this can be changed.

Final Note

Use of dependency injection is good but be careful not to overuse it. Its added complexity is sometimes not the worth the effort.

We also have not used ioc containers just so we could get a good grasp of the concepts. Our next article will cover (popular) ioc containers.

Blijf op de hoogte met Craft

Wil jij geen events meer missen en via blogs op de hoogte blijven van alle ontwikkelingen rondom .NET Development? Meld je dan aan voor de Craft-update!