Disadvantages of constructor injection

When it comes to DI, sadly there is no silver bullet. Despite the utility of constructor injection, it cannot be used in all cases. This section covers the disadvantages and limitations of constructor injection.

Can cause lots of changes—When applying constructor injection to existing code, it can result in a lot of changes. This is particularly true if the code was initially written as functions.

Consider the following code:

// Dealer will shuffle a deck of cards and deal them to the playersfunc DealCards() (player1 []Card, player2 []Card) {   // create a new deck of cards   cards := newDeck()   // shuffle the cards   shuffler := &myShuffler{}   shuffler.Shuffle(cards)   // deal   player1 = append(player1, cards ...

Get Hands-On Dependency Injection in Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.