0% found this document useful (0 votes)
50 views

Dependency Injection (DI) in .NET Core

Uploaded by

Rupesh Rai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Dependency Injection (DI) in .NET Core

Uploaded by

Rupesh Rai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Understanding DI

in .NET Core

Keivan Damirchi Let's go


1/12

Dependency Injection
DI is a technique in software
development that manages the
dependencies between different
components or modules in a system.

This is done by providing a component


with its required dependencies from an
external source instead of allowing it to
create or manage its dependencies.

DI achieves inversion of control (IoC)


between classes and their dependencies.

Keivan Damirchi
2/12

inversion of control (IoC)


It’s is a design pattern that allows the
flow of control in a program to be
inverted or reversed. Instead of a
program controlling the flow of
execution, the flow is controlled by an
external framework or container.

So why should we use Ioc?

Keivan Damirchi
3/12

Why IoC?
By applying IoC, an application can
decouple its components, making
them more modular and easier to test
and maintain.

IoC promotes the use of


interfaces and abstraction,
which makes the code more
flexible and extensible.

Keivan Damirchi
4/12

Types of DI
Constructor Injection
This involves injecting dependencies
through a class's constructor. This is the
most common form of DI and is often
used when a class has mandatory
dependencies that it needs in order to
function correctly.

Keivan Damirchi
5/12

Types of DI
Property Injection
This involves injecting dependencies
through a class's public properties.
Property Injection is less common than
Constructor Injection, and is often used
when you need to inject optional
dependencies.

Keivan Damirchi
6/12

Types of DI
Method Injection
This involves injecting dependencies
through a method call. Method Injection
is the least common form of DI, and is
often used when a dependency is only
required for a specific method call.

Keivan Damirchi
7/12

DI in .NET Core
DI 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.

Keivan Damirchi
8/12

Lifetime Management

Lifetime management in
Dependency Injection refers to the
way in which 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.

Keivan Damirchi
9/12

The Three Lifetime


Management Options

Transient

Scoped

Singleton

Keivan Damirchi
10/12 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.

Methods
AddTransient: Registers a service with the transient
lifetime management.
AddTransient<TService, TImplementation>:
Registers a service and its implementation with the
transient lifetime management.

When you have a lightweight and stateless


service that does not maintain any state between
requests, such as a logger or a helper class.

When you want to ensure that a new instance of


the service is created every time it is requested
from the container. This can help prevent issues
with thread-safety or shared state.

Keivan Damirchi
11/12 Scoped

A single instance of the service is created for


each scope. A scope typically corresponds to a
web request in a web application.

Methods
AddScoped: Registers a service with the scoped
lifetime management.
AddScoped<TService, TImplementation>: Registers
a service and its implementation with the scoped
lifetime management.

When you have a service that needs to maintain state


between requests, but that should not be shared
across multiple requests, such as a shopping cart in
an e-commerce application./

When you want to optimize performance by reusing


a single instance of the service across all objects
that are created within a particular scope, such as a
web request in a web application.

Keivan Damirchi
12/12 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.
Be aware that a single instance of the service will be
created and shared throughout the lifetime of the
application. This can lead to issues with shared state or
thread-safety if the service is not designed to be used in
this way.
Be sure to handle any concurrency issues that may
arise when using a singleton service. This can
include locking mechanisms or other techniques to
prevent multiple threads from accessing the service
at the same time.

Keivan Damirchi
Close();

Keivan Damirchi

You might also like