Dependency Injection (DI) in .NET Core
Dependency Injection (DI) in .NET Core
in .NET Core
Dependency Injection
DI is a technique in software
development that manages the
dependencies between different
components or modules in a system.
Keivan Damirchi
2/12
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.
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.
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
Transient
Scoped
Singleton
Keivan Damirchi
10/12 Transient
Methods
AddTransient: Registers a service with the transient
lifetime management.
AddTransient<TService, TImplementation>:
Registers a service and its implementation with the
transient lifetime management.
Keivan Damirchi
11/12 Scoped
Methods
AddScoped: Registers a service with the scoped
lifetime management.
AddScoped<TService, TImplementation>: Registers
a service and its implementation with the scoped
lifetime management.
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