Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to Dependency Injection

Intro to Dependency Injection

Start with the basic premise of dependency injection and move onto some intermediate to advanced topics. We'll explore some basic functionality of popular dependency injection frameworks including Unity, Ninject, and Autofac.

John Callaway

February 02, 2016
Tweet

More Decks by John Callaway

Other Decks in Programming

Transcript

  1. John Callaway • Professional developer since 1999 • Specialize in

    web technologies • Primarily with line-of-business • Enjoy learning new tech
  2. What is Dependency Injection In software engineering, dependency injection is

    a software design pattern that implements inversion of control for resolving dependencies. A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it.
  3. Martin Fowler says: The basic idea of the Dependency Injection

    is to have a separate object, an assembler, that populates a field in the [target] class with an appropriate implementation for the finder interface
  4. Let’s look at some code! • Ex01 - File =>

    New Project • Ex02 - “new” everywhere! • Ex03 - Field level service • Ex04 - Poor Man’s DI • Ex05 - Poor Man multi-layer • Ex06 - Unity • Ex07 - Ninject • Ex08 - Autofac • Ex09 - Put it all together
  5. DI vs IoC Dependency Injection is a tool that Inversion

    of Control uses. If you’re doing Inversion of Control then you’re doing Dependency Injection. If you’re doing Dependency Injection you may not necessarily be doing IoC. The distinguishing feature of IoC is instead of the dependency flowing from top to bottom, the flow is from bottom to the top. In other words, the data layer would depend on the business layer, your business layer depends on UI, etc. Inversion of Control is a topic for another time.
  6. Appendix - Links Wiki - https://en.wikipedia.org/wiki/Dependency_injection Martin Fowler - http://www.martinfowler.com/articles/injection.html

    Unity - https://github.com/unitycontainer/unity Ninject - http://www.ninject.org/ Autofac - http://autofac.org/ Moq - https://github.com/Moq/moq4/wiki/Quickstart