0% found this document useful (0 votes)
2 views12 pages

Dependency Injection 1726816536

Dependency Injection (DI) is a software development technique that manages dependencies between components by providing them from an external source, promoting Inversion of Control (IoC). DI enhances code reusability, testing simplicity, and loose coupling, while also adhering to SOLID principles. In .NET Core, DI is a built-in feature that allows for effective lifetime management of services through transient, scoped, and singleton options.

Uploaded by

Shweta Gadage
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)
2 views12 pages

Dependency Injection 1726816536

Dependency Injection (DI) is a software development technique that manages dependencies between components by providing them from an external source, promoting Inversion of Control (IoC). DI enhances code reusability, testing simplicity, and loose coupling, while also adhering to SOLID principles. In .NET Core, DI is a built-in feature that allows for effective lifetime management of services through transient, scoped, and singleton options.

Uploaded by

Shweta Gadage
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
You are on page 1/ 12

WHAT IS

DEPENDENCY
INJECTION
BY CHAYMAE DIHAJI

SWIPE FOR MORE


01 DEFINITION OF
DEPENDENCY INJECTION

Dependency Injection (DI) is a technique in


software development that manages the
dependencies between different components
or modules in a system.

This technique provides a component with its


required dependencies from an external source,
rather than allowing it to create or manage its
own dependencies.

DI helps achieve Inversion of Control (IoC) by


decoupling classes from their dependencies .

SWIPE FOR MORE


02 INVERSION OF
CONTROL (IOC)

Inversion of Control (IoC): Objects do not create their


own dependencies , instead an external container
manages the creation and injection of dependencies,
thereby inverting the usual control of object
management.

Link to Dependency Injection (DI)

Dependency Injection (DI) is a technique that achieves


IoC by providing a class with its dependencies via an
external container, making the code more flexible and
easier to modify.

Why It Matters?

Understanding IoC is crucial to grasp the utility of DI, as


DI allows classes to focus on their core logic by
delegating the management of dependencies to an
external system.

SWIPE FOR MORE


03 TYPES OF
DEPENDENCY INJECTION

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.

SWIPE FOR MORE


04 KEY
ADVANTAGES OF (DI)
Reusability

Permits code reuse by sharing dependencies across


classes, reducing duplication and improving organization.

Simplicity of Testing

Facilitates testing by using mock objects, enhancing code


quality and making it easier to identify and fix issues.

Loose Coupling

Reduces class dependencies, ensuring changes in one


class don’t impact others, which improves system
robustness.

SWIPE FOR MORE


Extensibility

Enhances scalability by allowing easy updates and feature


additions without disrupting the existing system.

Respect for SOLID Principles

SRP (Single Responsibility Principle)


Allows a class to focus on a single responsibility by
managing dependencies separately.

DIP (Dependency Inversion Principle)


Promotes high-level modules depending on abstractions
rather than concrete implementations, making
dependencies easier to modify and replace

SWIPE FOR MORE


05 DEPENDENCY INJECTION
IN .NET CORE

Dependency Injection in .NET Core is a built-in feature


that allows you to separate the creation of object
graphs from your application code.

To use DI, you need to register your services with


a service container by specifying which services
your application needs and how to create them

SWIPE FOR MORE


06 LIFETIME
MANAGEMENT

Lifetime management in Dependency Injection refers to


how instances of a service are created and managed
by the Dependency Injection container. In other words, it
determines the lifespan of an object created through
Dependency Injection.

The Three Lifetime Management Options

TRANSIENT

SCOPED

SINGLETON

SWIPE FOR MORE


TRANSIENT

This is the default lifetime management in .NET Core. A


new instance of the service is created each time it is
requested from the container.

An instance with this scope will be constructed every


time it is requested. This means that every service that
has this type injected will get a different instance.

METHODS

AddTransient: Registers a service with transient lifetime


management.

AddTransient<TService, TImplementation>: Registers a


service and its implementation with transient lifetime
management.

WHEN TO USE

When you have a lightweight and stateless service (like a


logger or a helper class), to make sure a new instance is
created every time, avoiding thread safety or shared state
issues.

SWIPE FOR MORE


SCOPED

A single instance of the service is created for each


scope. A scope typically corresponds to a web request
in a web application.
This scope will generate an instance of this type per
request. For web applications, it means that every time
this type is injected, it will get the same instance during
a client request.

METHODS

AddScoped: Registers a service with scoped lifetime


management.

AddScoped<TService, TImplementation>: Registers a


service and its implementation with scoped lifetime
management.

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.

When you want to reuse a single instance of the service


for all objects created during a specific scope, such as a
web request.

SWIPE FOR MORE


SINGLETON

A single instance of the service is created and shared


throughout the lifetime of the application. This can be
useful for services that are expensive to create or for
services that need to maintain state.

METHODS

AddSingleton: Registers a service with the singleton


lifetime management.
AddSingleton<TService, TImplementation>: Registers a
service and its implementation with the singleton lifetime
management.

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.

When you want one instance of the service shared


throughout the application's lifetime for better
performance and resource use.

SWIPE FOR MORE


IF YOU
FIND THIS
HELPFUL, FEEL FREE
TO LIKE AND SHARE
IT WITH YOUR
NETWORK.

You might also like