Dependency Injection 1726816536
Dependency Injection 1726816536
DEPENDENCY
INJECTION
BY CHAYMAE DIHAJI
Why It Matters?
Constructor Injection
Dependencies are provided at the time of object
creation via the constructor.
How It Works ?
Dependencies are passed as arguments to the
constructor during instantiation.
Property Injection
Dependencies are provided through public properties.
How It Works ?
Dependencies are assigned to properties after the
object has been created.
Method Injection
Dependencies are provided through specific methods
after the object has been created.
How It Works ?
Dependencies are injected through methods called
after instantiation.
Simplicity of Testing
Loose Coupling
TRANSIENT
SCOPED
SINGLETON
METHODS
WHEN TO USE
METHODS
WHEN TO USE
When you have a service that needs to keep state
between requests but should not be shared across
different requests, like a shopping cart.
METHODS
WHEN TO USE
When you have a service that is costly to create or needs
to keep state, like a configuration service or database
connection.