Aller au contenu

Concrete Dependency Injection (ARCH4199)

Ce contenu n’est pas encore disponible dans votre langue.

Key java.quality.concrete-dependency · Severity info · Category quality

Constructor dependencies typed as concrete classes violate the dependency inversion principle; they pin the implementation and make test doubles harder to use. Depend on an interface.

@Service
class CheckoutService {
private final StripePaymentGateway gateway;
CheckoutService(StripePaymentGateway gateway) { this.gateway = gateway; }
}
@Service
class CheckoutService {
private final PaymentGateway gateway;
CheckoutService(PaymentGateway gateway) { this.gateway = gateway; }
}

Type the dependency as an interface/abstraction rather than a concrete class.