0% found this document useful (0 votes)
4 views4 pages

Understanding Dependency Injection in .NET

Dependency Injection is a design pattern aimed at creating loosely-coupled code, enhancing maintainability and simplifying unit testing. It allows for easy swapping of implementations and breaking long dependency chains, utilizing a service collection to register services. The IServiceScopeFactory interface enables the creation of service scopes for resolving scoped services outside the normal request pipeline.

Uploaded by

summykumar236
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)
4 views4 pages

Understanding Dependency Injection in .NET

Dependency Injection is a design pattern aimed at creating loosely-coupled code, enhancing maintainability and simplifying unit testing. It allows for easy swapping of implementations and breaking long dependency chains, utilizing a service collection to register services. The IServiceScopeFactory interface enables the creation of service scopes for resolving scoped services outside the normal request pipeline.

Uploaded by

summykumar236
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

Dependency Injection
-> It is a design pattern
->allows to develop "loosely-coupled" Code
->With primary aim of making our code more maintainable

What does it solve?


->swap the implementation with ease
->avoids "tight-coupling"
->breaks long dependency chains
->Unit-testing becomes easier

Service Collection basically is where you register your services that you want to make
available to your application
Interface is called as the service, and there should be a concrete implementation of the
service which will be referred.
We can have any number of services in the IServiceCollection

->We use something called "BuilderServiceProvider" method, and that will create a service
container.

It creates Service Provider, which will then be used whenever the it is referred.
It is the container, which will serve you all the references.

[Link]() is behind the scenes creates the service container for us.

Note: DI does not particularly needs the container, but we are using it here. as it comes in
.net.
->But DI is design pattern and it will work with or without the container.
Detailed explanation about the types of service with each request and how they will behave
and which instance will they give

Eg: You can have a New GUID() example for this to demonstrate:
->Create an interface, and then the concrete implementation of the interface, and then
register these in the services(scoped, transient, singleton) and the call it
output:

Transient: New GUID every time you refresh the page.


Scoped: Same GUID during a single request, but different across requests.
Singleton: Same GUID across all requests.

Scope Factory

a Scope Factory refers to the use of the IServiceScopeFactory interface to manually


create a service scope. This is useful when you need to resolve scoped services outside
of the normal request pipeline — for example, in background tasks, hosted services, or
middleware.

A scope is a boundary within which scoped services are created and shared. Outside of a
scope, scoped services cannot be resolved directly.

What is IServiceScopeFactory ?
IServiceScopeFactory is an interface provided by .NET Core DI that allows you to create a
new IServiceScope . This is especially useful when you're outside of the HTTP request
pipeline but still want to use scoped services.

When to Use IServiceScopeFactory

In background services ( IHostedService , BackgroundService )


In middleware where you need scoped services
In event handlers or message queues
In custom service containers or factories

Hello

You might also like