0% found this document useful (0 votes)
16 views10 pages

Dependency Injection

Dependency Injection (DI) is a design pattern that allows an object to receive its dependencies from an external source rather than creating them itself, promoting loose coupling and easier testing. There are three types of DI: Constructor Injection, Setter Injection, and Field Injection, each with its own advantages and drawbacks. DI improves maintainability, scalability, and testability in software development, and is commonly implemented using frameworks like Spring and Google Guice.

Uploaded by

johnytensa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Dependency Injection

Dependency Injection (DI) is a design pattern that allows an object to receive its dependencies from an external source rather than creating them itself, promoting loose coupling and easier testing. There are three types of DI: Constructor Injection, Setter Injection, and Field Injection, each with its own advantages and drawbacks. DI improves maintainability, scalability, and testability in software development, and is commonly implemented using frameworks like Spring and Google Guice.

Uploaded by

johnytensa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

​Dependency Injection (DI)​

​What is Dependency Injection?​


​ ependency Injection​​is a design pattern used to implement​​Inversion of Control (IoC)​​where​
D
​an object​​does not create its dependencies itself​​,​​but instead​​receives them from an​
​external source​​.​

​In simple terms:​

I​nstead of a class creating the objects it needs, those objects are​​injected from​
​outside​​.​

​This helps reduce​​tight coupling​​between classes.​

​Problem Without Dependency Injection​


​Java​

class​​
​ EmailService {​

void​​
​ sendEmail(​
String​​
​ message) {​

[Link](​
​ "Email sent: "​​
​ + message);​

}​

}​

class​​
​ NotificationService {​

private​​
​ EmailService emailService =​​
new​​
EmailService();​
void​​
​ notifyUser() {​

[Link](​
​ "Hello User"​
​ );​

}​

}​

​Issues​

​​ T
● ​ ight coupling​
​●​ ​Hard to​​test​
​●​ ​Hard to​​replace dependency​

NotificationService​
​Example: If we want to switch to​​SMSService​​, we must modify​​ ​.​

​Solution: Dependency Injection​


​Java​

class​​
​ NotificationService {​

private​​
​ EmailService emailService;​

NotificationService(EmailService emailService) {​

this​
​ .emailService = emailService;​

}​

void​​
​ notifyUser() {​
[Link](​
​ "Hello User"​
​ );​

}​

}​

​Usage:​

​Java​

EmailService emailService =​​


​ new​​
EmailService();​

NotificationService service =​​


​ new​
NotificationService(emailService);​

​Now:​

​​ N
● ​otificationService​​does not create EmailService​
​●​ ​It​​receives it from outside​

​Types of Dependency Injection​


​1. Constructor Injection (Most Recommended)​
​Dependency is provided through​​constructor​​.​

​Java​

class​​
​ OrderService {​

private​​
​ PaymentService paymentService;​
OrderService(PaymentService paymentService) {​

this​
​ .paymentService = paymentService;​

}​

}​

​Advantages​

​​ D
● ​ ependency becomes​​mandatory​
​●​ ​Makes object​​immutable​
​●​ ​Easier​​testing​

​2. Setter Injection​


​Dependency is provided through a​​setter method​​.​

​Java​

class​​
​ OrderService {​

private​​
​ PaymentService paymentService;​

void​​
​ setPaymentService(PaymentService paymentService)​​
{​

this​
​ .paymentService = paymentService;​

}​

}​

​Advantages​
​​ D
● ​ ependency can be​​optional​
​●​ ​Can be​​changed later​

​3. Field Injection (Common in frameworks)​


​Java​

class​​
​ OrderService {​

@Inject​

private​​
​ PaymentService paymentService;​

}​

​Common in frameworks like:​

​​ S
● ​ pring Framework​
​●​ ​Google Guice​
​●​ ​Dagger​

​Drawbacks​

​​ H
● ​ arder to test​
​●​ ​Breaks encapsulation​
​●​ ​Dependency hidden​

​ ependency Injection vs Inversion of​


D
​Control​
​These are often confused.​
​Concept​ ​Meaning​

​Inversion of Control (IoC)​ ​Design principle where control is inverted​

​Dependency Injection (DI)​ ​Implementation of IoC​

​Think of it like:​

​None​

IoC = Concept​

DI = Implementation​

​Real World Analogy​


​Imagine a​​car​​.​

​Without DI:​

​None​

Car creates its own Engine​


​With DI:​

​None​

Engine is provided to the Car from outside​


​This allows:​

​​ P
● ​ etrol engine​
​●​ ​Electric engine​
​●​ ​Hybrid engine​

Car​​class.​
​Without changing the​​

​Dependency Injection Improves​


​1. Loose Coupling​

​Classes depend on​​abstractions​​, not concrete implementations.​

​Example:​

​Java​

class​​
​ OrderService {​

private​​
​ PaymentGateway gateway;​

}​

​Instead of:​

​None​

StripePaymentGateway​

RazorpayPaymentGateway​

​2. Testability​

​Easy to​​mock dependencies​​.​

​Example:​
​Java​

MockPaymentService paymentService =​​


​ new​​
MockPaymentService();​

OrderService service =​​


​ new​​
OrderService(paymentService);​

​3. Maintainability​

​Changing dependency​​does not affect other classes​​.​

​4. Scalability​

​Allows plug-and-play architecture.​

​Dependency Injection in LLD Interviews​


​Interviewers expect you to:​

​Use interfaces​

​Java​

interface​​
​ PaymentService {​

void​​
​ pay();​

}​

​Implementations:​

​None​

CreditCardPayment​

UPIPayment​

NetBankingPayment​

​Injection:​

​Java​

class​​
​ OrderService {​

private​​
​ PaymentService paymentService;​

OrderService(PaymentService paymentService) {​

this​
​ .paymentService = paymentService;​

}​

}​

​Dependency Injection with Factory​


​Often combined with​​Factory Pattern​​.​

​None​

Controller​

↓​

Service​

↓​

Repository​

​Each layer receives dependencies through​​DI​​.​

​Dependency Injection Frameworks​


​Popular frameworks that implement DI:​

​​
● ​ pring Framework​
S
​●​ ​Dagger​
​●​ ​Google Guice​
​●​ ​Micronaut​

​These frameworks:​

​​ C
● ​ reate objects​
​●​ ​Manage lifecycle​
​●​ ​Inject dependencies automatically​

You might also like