Dependency Injection in Scala -- Guide

Click for: original source

This is well written guide about dependency injection on Scala. Dependency Injection (DI) is a popular pattern which encourages loose coupling between a services’ clients and service implementations.

Dependency Injection is a simple concept, and it can be implemented using relatively few simple constructs. We should avoid over-complicating and over-using containers or frameworks, without thoroughly analysing the costs.

This guide describes how to do Dependency Injection using the Scala language constructs as much as possible, while remaining practical, with the help of the MacWire library where necessary.

DI is all about decoupling client and service code (the client may happen to be another service). Services need to expose information on what dependencies they need. Instead of creating dependent service implementations inside the service itself, references to dependent services are “injected”. This makes the code easier to understand, more testable and more reusable.

The guide then explains:

  • What is Dependency Injection?
  • Other approaches
  • Manual Dependency Injection
  • Using MacWire for wiring
  • Simple scoping
  • Modularising object graph creation
  • Multiple implementations
  • Testing
  • Interceptors
  • Advanced scoping
  • Factories
  • Accessing the object graph dynamically
  • Multiple instances
  • DI in Akka

Well organised and informative resource for any Scala lover. Top notch work!

[Read More]

Tags scala programming java oop