Play Framework dependency injection Object vs @Singleton Class

Murat Mustafin

Play Framework 2.4 has support for Google Guice Dependency Injection

What is the advantage of using

@Singleton class A

instead of

object A

for singletons?

jazmit

I can see three advantages of using @Singleton class over object if A has no dependencies:

  1. If you wanted to test A, and declare it as @Singleton class you have the option of subclassing it to mock out some of the functions in your test, whereas this is not possible with an object.

  2. If you use object, it is very tempting for clients of A to reference it directly, leading to a strongly coupled system, but using @Singleton class forces them to think about where A is instantiated, probably leading to a more decoupled design.

  3. If you later change your mind, and allow multiple instances of A, the refactoring will be much easier to do if you chose to use @Singleton class.

If A has dependencies, however, using @Singleton class allows them to be dependency injected on construction, whereas with an object this is only possible if you declare the dependencies as mutable (eg: a var with getter/setter methods) .

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Dependency injection with abstract class and object in Play Framework 2.5

Play framework compile-time dependency injection and singleton

Dependency injection vs singleton, Initialization

Dependency Injection and/vs Global Singleton

ScalaWS in play framework 2.5 and dependency injection of WSClient in custom class

Play Framework 2.5 dependency injection in form object bean (Java)

Play framework dependency injection is not working

Scala Dependency Injection in Play Framework

Play Framework without dependency injection?

Play Framework PathBindable with Dependency Injection

How to move towards dependency injection with singleton class

Dependency Injection of Singleton Class that extends from Interface

Spring Dependency Injection not working in singleton class

Blazor Global Singleton Object - use Dependency Injection?

Play Framework: Dependency Injection in Sub-Templates

Singleton or dependency injection

Singleton with Dependency Injection

Dependency injection Singleton into Transient

IMemoryCache - Singleton or Dependency injection

Blazor Dependency injection vs. simple class

Provide singleton dependency vs object initialization

Scala Play Framework - controller as class or singleton

Play Framework 2.4 - Dependency injection to replace GlobalSettings.onStart()

Play framework java dependency injection - when to use singletons

Dependency injection with Play Framework and Guice leads to NullPointerException on Application start

MVVM + Services + Entity Framework and Dependency Injection vs Service Locator

Python Dependency Injection Framework

Entity Framework Dependency Injection

Dependency injection and Entity Framework