Questions 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.

Filter by
Sorted by
Tagged with
48 votes
8 answers
12k views

When NOT to apply the Dependency Inversion Principle?

I am currently trying to figure out SOLID. So the Dependency Inversion Principle means that any two classes should communicate via interfaces, not directly. Example: If class A has a method, that ...
0 votes
1 answer
119 views

Services should depend on Data or Factories?

I have app which uses IOC container. I have services registered in container, and I can consume either data factory, or particular data object. Which approach is preffereable? Consuming factory object:...
15 votes
5 answers
3k views

Technical term to denote opposite of dependency injection?

This is more of a nomenclature (technical writing) rather than a purely technical question. I am trying to write a refactoring proposal (and get it assigned to myself) centered around expanding ...
158 votes
9 answers
73k views

When is it not appropriate to use the dependency injection pattern?

Since learning (and loving) automated testing I have found myself using the dependency injection pattern in almost every project. Is it always appropriate to use this pattern when working with ...
1 vote
1 answer
75 views

Composite repositories: minimizing dependency injections

I have an application with dependency injection that consumes a REST API. The API calls are abstracted into entity-specific repositories. Some of the repositories are: HttpNotebookRepository, ...
5 votes
5 answers
6k views

Is it a bad practice to use Singleton for DI in Asp.net rather than Scoped, Transient whenever possible?

We had a conversation (with coworkers) about why we needed to use Singleton for Controller, Business Services, and Repositories. The reason for this, they claim, is because singleton ensures that code ...
2 votes
2 answers
186 views

Why do we separate interface when implementing interface injection variant of DI?

With interface injection (wikpedia) we have a method to set the dependency on the client as part of an interfase. public interface ServiceSetter { public void setService(Service service); } Why ...
0 votes
1 answer
53 views

WPF plugin system - Dependency injection in plugin class libraries

The main application loads plugins, list them and when user selects one it is initialized and displayed. Each plugin is a complex class library which contains several views/viewmodels/repositories/etc....
5 votes
3 answers
628 views

How to avoid DI dependency cycle for observer pattern

In my project I'm using the observer pattern in several places, i.e. the subject notifies the observers about something, and expect them to act. The subject does not know anything about the details of ...
157 votes
8 answers
65k views

Criticism and disadvantages of dependency injection

Dependency injection (DI) is a well known and fashionable pattern. Most of engineers know its advantages, like: Making isolation in unit testing possible/easy Explicitly defining dependencies of a ...
1 vote
4 answers
1k views

DI: Injecting interfaces vs actual classes

In my job I work with C# (although the language is irrelevant for my question, and now I'd like to focus on Android) and we usually inject interfaces and not the actual classes, so I was wondering ...
0 votes
4 answers
172 views

Dependency injection in legacy code

I have a legacy project with many classes with high coupling, and I have a necessity to test some of them. So, I decided to introduce dependency injection principle but stumbled upon where to create ...
1 vote
2 answers
353 views

Chain of responsibility or simple dependency injection for Querying API

I am working on refactoring a project code that is wired up in a tangled way. It was started with decent dependency injection and over time with all custom requirements it looks it got tangled up. ...
48 votes
2 answers
48k views

Dependency Injection vs Static Methods

I had an interesting discussion today with another developer about how to approach a class with a method that accepts a string and outputs string. Imagine something like the following which is ...
0 votes
3 answers
390 views

Would Injecting dependencies in C# as default parameters be a bad practice?

Given the (old) debate over whether Singletons are overused/abused/are worth it - would it be a bad idea to inject the dependencies as default parameters? In this way, we could get rid of defining ...
0 votes
1 answer
61 views

Injecting Adapters Into Client

So I'm building a client for a third API and I want to be able to Get() and GetMany() for each type this controller can provide. So I've built this adapter, that provides generically typed methods for ...
1 vote
2 answers
481 views

Angular 7 - Injecting a State/Context Snapshot

Requirement There is some (for arguments sake) “global” application state that I am interested in injecting into components. Each candidate component should receive an immutable snapshot of the ...
0 votes
2 answers
131 views

Externally relying on the method call of the DI class means introducing a dependency on the implementation detail?

So I've got a class that calculates a discount (Let's call it class "A"). It has a price fetcher class as a dependency (constructor injection) (Class "B"). The price that is ...
1 vote
1 answer
75 views

Benefit of using Dependency Injection inside ASP.NET Core MVC

I am reading this article about using DI inside ASP.NET Core @ https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-6.0 .. but i can not understand its ...
3 votes
4 answers
720 views

Dependency Injection with multiple, dynamically created objects

All guides and blog posts that I have read about dependency injection use a simple example where one object A depends on one object B. They apply DI by creating object B somewhere else and passing it ...
2 votes
2 answers
1k views

Dependency Injection vs Static Dependencies

I am building a wrapper for a library that requires little complex logic. The whole project is 8 builder classes, 4 classes for doing some pre-processing, and a couple visitor classes. Essentially I ...
1 vote
0 answers
54 views

Best practices when it comes to extending Service collections in .NET for dependency injection

Most of the information that I can find online is about how to do basic dependency injection in the common flows. eg one DI container where services are registered. For a project we are writing a ...
3 votes
2 answers
182 views

Abstracting calls to common library methods in C#

I‘m working on making a legacy code base more testable and made good progress with (constructor)-injecting dependencies to classes. I noticed something in all classes that access the file system: they ...
1 vote
2 answers
250 views

Inheritance/Composition VS "Direct Injection Construction"

my following example seems to go into the direction Inheritance VS Composition. But that's not, what i want to ask. I see the concept Inheritance and Composition on one side and the alternative, which ...
1 vote
3 answers
261 views

Java Library - How to do Pure Dependency Injection When State is a Factor?

To set the stage, I am trying to do pure dependency injection for a Java Library I am creating to make it more testable. As it is a library, I want to do pure dependency injection without creating a ...
1 vote
1 answer
549 views

Correct IHttpClientFactory implementation with dependency injection?

In Microsoft's documentation, in the Named Clients example, they retrieve an HttpClient from the IoC container: var httpClient = _httpClientFactory.CreateClient("GitHub"); In the Typed ...
1 vote
3 answers
579 views

Is It Considered Good Practice to Use Dependency Injection with Strategies from a NuGet Package?

I am developing a NuGet package which contains shared code for various ASP.NET Core projects. I am planning to use the strategy pattern to solve the same problem in a few different ways. So there will ...
128 votes
11 answers
41k views

(Why) is it important that a unit test not test dependencies?

I understand the value of automated testing and use it wherever the problem is well-specified enough that I can come up with good test cases. I've noticed, though, that some people here and on ...
7 votes
3 answers
1k views

Is the architecture of my project a common one? What's its name?

I am responsible for a software project for a few years now (a Java desktop application, which is mostly event-driven). I started it from scratch, and at the beginning of this project I made some ...
1 vote
0 answers
199 views

Android + Kotlin + Hilt: Injecting into static methods classes

I'm not very clear about the best way to inject into a static methods helper class. I'm kinda new to Kotlin, and as I've learnt we can access a method statically in two ways: Object class. Class + ...
1 vote
1 answer
210 views

Android + Kotlin + Hilt + multi-module app: Should I "migrate" all classes with static methods to "injection"?

With the purpose of learning Hilt I started "migrating" my multi-module Kotlin app from using classes with static methods as helpers to Hilt injection. After a lot of headaches, now ...
1 vote
1 answer
316 views

Android + Kotlin + Hilt: Dependency Injection vs Static Methods

I've already read this carefully, but still need more clarification. I'm not new to dependency injection, but new to Hilt, and trying to implement Hilt in my multi-module app. The reason? I currently ...
1 vote
4 answers
207 views

Is going through network during DI anti pattern?

So I want to inject an Application Client, say a Rest Api client, in my code. In order to create this client I need to go over the network to get it's user and password that is in a HTTP Vault. This ...
2 votes
2 answers
163 views

C# - Class creating instances of other classes requiring different services

Context I have a service class whose sole purpose is to interact with a specific API, let's say the Automotive API. The API mostly works with generic AutomativeRecord which basically represents a ...
1 vote
1 answer
274 views

Dependency Injection for dynamic objects

I am learning about Dependency Injection and I have been recently implementing the following classes for an app that executes commands over ssh using Python. I am confused about whether I am using it ...
1 vote
2 answers
300 views

Dependency injection vs function parameter

I am working on a project that is structured in hexagonal architecture. It is a multi module gradle project where web layer is a separate module that depends on the domain module. Sample code related ...
2 votes
3 answers
271 views

Is it a bad practice to manipulate the behaviour of a class by replacing it's DI arguments?

I am well aware of a programming-to-an-interface term. That's a good thing! I understand that these allow to add decorators; or perhaps one can implement a service that works faster and by injecting ...
6 votes
1 answer
4k views

ASP.NET Core - Is using [FromServices] attribute bad practice?

I have a Controller in ASP Core MVC. I'm trying to trim down the dependency injected services in the constructor so I can start building unit tests more easily. However, I have some services being ...
0 votes
2 answers
176 views

Struggle with catch 22 in initialization code

I have a command line application written in C#. Here's some facts about the program that are relevant to my question: The application has a "data directory" (e.g. ~/.config/myapp) where ...
-1 votes
1 answer
171 views

How Should I Go About Refactoring My Code With Dependency Injection? [closed]

I work on WPF applications used in testing hardware. I need to make my code base more testable and re-usable. I believe DI would help, but I'm not quite sure where to start. I've done my best with the ...
2 votes
0 answers
814 views

WPF Best way to create viewmodel from another viewmodel

Often I want to open a new window from within some view. I see opening a window as view logic. So I don't want code in my viewmodel that creates/opens/changes windows or anything else that interacts ...
3 votes
1 answer
721 views

How to fix a circular dependency

I have a IItemProcessor class which takes each item from a list and send it to a web API (IApiService). The processing is done asynchronously, on another thread. If web API responds with 'user not ...
-2 votes
1 answer
86 views

Best way make a complex libraries as a plugin with dependency inversion principle [closed]

For instance, if we have a complex library with many functionalities, how can we make this lib a plugin such that it can be replaced with another lib more easily. The answer obviously is the ...
6 votes
4 answers
4k views

How to get an ILogger instance from extension methods?

Let's say I have this service abstraction exposed from a library. public interface INavigator { ImmutableList<IPageViewModel> Entries { get; } void NavigateForward(IPageViewModel page); ...
1 vote
1 answer
201 views

Inject configuration files into the injector is it an antipattern?

I'm trying to design a system that inject configuration of each component into each class. project structure: |features --|component-a component-a.service.js component-a.config....
1 vote
2 answers
649 views

Golang interface-implementation circular dependency

In trying to separate interface from implementation, I ran into a circular dependency problem. There is a best practice in the world of Java: consume interfaces instead of concrete classes. That is, ...
1 vote
1 answer
2k views

MVVM with DI, view model location and shared service data

I have a couple of questions regarding MVVM view models. I have 3 view models in my scenario, which I have put together a shorter sample below. I was looking for a nice solution for 3 views working ...
607 votes
13 answers
264k views

So Singletons are bad, then what?

There has been a lot of discussion lately about the problems with using (and overusing) Singletons. I've been one of those people earlier in my career too. I can see what the problem is now, and yet, ...
16 votes
3 answers
12k views

Java: Is it okay to abuse Spring beans (@Component) rather than use static final utility classes?

I'm torn between using DI and static utility classes on my code. I currently created a DateUtil static class that is only meant to be accessed statically. This class is responsible for just creating ...
6 votes
5 answers
409 views

Instability metric vs Dependency Injection

Below is an excerpt of Fundamentals of Software Architecture, page 65, which I am reading: The instability metric determines the volatility of a code base. A code base that exhibits high degrees of ...

1
2 3 4 5
13