CARVIEW |
Select Language
HTTP/2 200
date: Wed, 30 Jul 2025 23:19:13 GMT
content-type: text/html; charset=utf-8
cf-ray: 9678946268ddc1cc-BLR
cf-cache-status: DYNAMIC
cache-control: private
set-cookie: prov=9bdaaa81-78b3-438d-8312-4b9927a61c2e; expires=Thu, 30 Jul 2026 23:19:13 GMT; domain=.stackexchange.com; path=/; secure; httponly
strict-transport-security: max-age=31536000; includeSubDomains
vary: Accept-Encoding
content-security-policy: upgrade-insecure-requests; frame-ancestors 'self' https://stackexchange.com
x-clacks-overhead: GNU Terry Pratchett
x-frame-options: SAMEORIGIN
x-request-guid: 637a70ee-595f-4b1f-a63f-7148c86d3d8a
x-worker-origin-response-time: 476000000
x-dns-prefetch-control: off
set-cookie: prov=9bdaaa81-78b3-438d-8312-4b9927a61c2e; Path=/; HttpOnly; Domain=stackexchange.com
set-cookie: __cf_bm=SMq10LMIeZLx6I7CgJ7J5bPEraopTbAGgGMJaWwRHxo-1753917553-1.0.1.1-xSQSBc8sw6e0xhSmgK8Ev.Ap1CDuGLkaMDkJp_oSyNYn6r5AL7oJmxJ9xDoDFjgXq78RtTISG74VdtX19Qh5SW4ZnZ_5H.49fMTvpH_Y6YA; path=/; expires=Wed, 30-Jul-25 23:49:13 GMT; domain=.stackexchange.com; HttpOnly; Secure; SameSite=None
set-cookie: _cfuvid=9cNXdnlC4nuGIfcQTzwO.S8tEcrCDperiGA1vb1Kje0-1753917553519-0.0.1.1-604800000; path=/; domain=.stackexchange.com; HttpOnly; Secure; SameSite=None
server: cloudflare
content-encoding: gzip
Newest 'dependency-injection' Questions - Software Engineering Stack Exchange
Skip to main content
Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack Exchange
The 2025 Developer Survey results are in. Explore insights into technology and tools, careers, community and more.
View results.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsQuestions tagged [dependency-injection]
Dependency Injection, is a design pattern where dependencies (instances of objects, properties) of a component are set through the constructor(s), methods or fields (properties). It is a special form of the more general dependency inversion.
669 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
13
votes
9
answers
4k
views
Is Dependency Injection useful when implementations are never expected to change?
Dependecy Injection makes mostly sense when using interfaces, since one of DI's strengths is to switch out implementations. It also decouples classes, since the consumer of the dependency does not ...
0
votes
0
answers
75
views
When DI goes too deep - what is another architecture/design pattern you should use? [duplicate]
I like using dependency injection but sometimes I find that I have to nest some dependencies so deep that I would almost rather use a global variable (despite this being bad practice). Here is an ...
2
votes
4
answers
182
views
Is it possible to do black box tests when I inject dependencies in the class?
This is my case.
I have a class in which I inject a service as dependency, an IService.
I want to test one method of this class. This method use one of the methods of the service.
Now I want to test ...
1
vote
0
answers
66
views
Implementing ServiceLocator in middleware
Before I begin, I'm aware the Service Locator pattern is not recommended and it's better to inject services into the classes where they are used. However, I have a large legacy codebase that creates a ...
4
votes
4
answers
379
views
Primary constructors for depedency injection
Microsoft identifies dependency injection as one of the uses for primary constructors.
public class ExampleController(IService service) : ControllerBase
{
[HttpGet]
public ActionResult<...
18
votes
11
answers
6k
views
Why is global state hard to test? Doesn't setting the global state at the beginning of each test solve the problem?
According to Why is Global State so Evil?, I know one of the disadvantages of "global state" is that it makes code "harder to test". I do not disagree with this, but what I don't ...
3
votes
5
answers
747
views
Why is "dependency injection" ok, but not "the opposite of preserve whole object (pass required parameters only)"?
According to Why should I use dependency injection?, "dependency injection" has some advantages, for example:
"Non dependency injection" version:
public class Client{
private ...
0
votes
2
answers
136
views
Resolving dependencies independently of the IoC container
I'm working on a component, let's call it MyComponent, which is intended to be used by various applications. These applications will get MyComponent as a NuGet package for instance.
Assume that ...
0
votes
2
answers
135
views
Dependency injection - passing responsibility for dependency to a Client?
I have long standing argue about dependency injection and SOLID principles with my teammate. We both want to make an Exporter, to export data into various formats.
My approach (in PHP):
class Exporter ...
3
votes
2
answers
644
views
What value does the mediator pattern add beyond dependency injection?
Does the mediator pattern add any value beyond dependency injection? I am encountering the mediator pattern for the first time in context of this sample application, which is meant to demonstrate how ...
1
vote
1
answer
200
views
Benefits of resolving dependencies with IServiceCollection and IServiceProvider for Unit Tests instead of inheriting mocks from a base test class
Articles such as this point out some of the pitfalls of manually instantiating dependencies in UTs, while showing some of the benefits of doing it, instead, with the .NET's dependency container by use ...
2
votes
2
answers
205
views
How to manage long dependency injection list for composite pattern?
Let's say I have a tree(composite pattern) of nodes,every node is a process that can contain other nodes/processes.
Tree constructed with UI where user can create node that copy one row of data from ...
-2
votes
1
answer
120
views
Pros/Cons of the following code with Autofac DI
I had a discussion with my boss on DI pattern in C# and I would like to get some reviews and discussion on the following code snippets.
Key points to look into is that the instantiation of the ...
4
votes
2
answers
1k
views
Is the entire web request the right scope for a DbContext in EF Core?
The usual advice for using EF Core in a web application is to use a scoped DbContext per HTTP request. This is even the suggested default for web application in the Microsoft documentation. I recently ...
0
votes
1
answer
170
views
IOC life cycle and static class
Does discussing the behavior of static classes make sense for IOC?
It seems that in the case of a life cycle transient such a description is misleading and untrue:
However, it is guaranteed to be ...
- The Overflow Blog
-
-
- Upcoming Events
-
2025 Community Moderator Electionends August 13
- Featured on Meta
-
-
-
Related Tags
c# × 156
design-patterns × 126
inversion-of-control × 77
object-oriented × 57
design × 55
unit-testing × 55
java × 47
architecture × 41
php × 41
.net × 34
ioc-containers × 34
service-locator × 27
dependency-inversion × 24
interfaces × 24
spring × 23
domain-driven-design × 22
asp.net-core × 20
c++ × 20
solid × 19
dependencies × 17
tdd × 15
asp.net-mvc × 14
factory × 14
more related tags
Hot Network Questions
- Why not use the "age" tool for encrypted backups?
- Puzzlin’ Puzzle
- How Does Code Reuse Work at NASA
- Was the Silk Road really a conduit of ideas?
- What happens if a witness is required to travel to the US to make a deposition but is refused entry?
- Visa Free Transit Facility (VFTF): Indian passport but resident in Saudi Arabia
- How can I secure an old iron gate with a padlock?
- Can I bring mycelium pellets into Greenland?
- "One can" in technical writing--good, bad, indifferent?
- How many possible MD5 hashes are there, 16³²? Or do MD5 checksums have error correction (in which case <16³² possible hashes)?
- How was the Universal Pictures 1936 opening logo created?
- Stepping down voltage from Li-ion to simulate 2x AAA batteries
- Problem with tikzcd in beamer
- In a TVS, is directionally bounded the same as bounded?
- Rim brakes and drop bar brifters
- Eleven Special Flashcards
- Reason why golems do not run on wheels
- Taking small amounts till there's nothing left
- Finite projective plane with trivial automorphism group
- If the police observe evidence of a different crime while in "hot pursuit" can they act on it?
- What constitutes a 'structure' made of Force for the purposes of a Forcebreaker Weapon?
- Is Samson referred to/mentioned in the siddur/liturgy?
- General Two-dimensional Elliptical Gaussian Image Generator in C++
- Middleschool student shows unusual level of insight. How to take the next step?