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
-1 votes
0 answers
11 views

Why do we need interface injection?

With interface we have a method to set the dependency. Why do we need to put the setter method in different interface instead of client class? What's the benefit of having interface?
1 vote
2 answers
253 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 ...
0 votes
0 answers
42 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 ...
0 votes
3 answers
277 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 ...
1 vote
2 answers
316 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. ...
2 votes
2 answers
165 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 ...
0 votes
2 answers
86 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 ...
44 votes
2 answers
44k 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
2 answers
463 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
3 answers
131 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 ...
0 votes
1 answer
125 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 ...
0 votes
3 answers
168 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
40k 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 ...
0 votes
2 answers
105 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 ...
0 votes
0 answers
40 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 + ...
0 votes
1 answer
62 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 ...
0 votes
1 answer
80 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 ...
0 votes
1 answer
96 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:...
0 votes
4 answers
194 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 ...
1 vote
2 answers
155 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 ...
0 votes
1 answer
115 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 ...
0 votes
2 answers
212 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 ...
1 vote
3 answers
257 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
3k 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
157 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
2 answers
222 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 ...
-2 votes
1 answer
146 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 ...
1 vote
0 answers
362 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
689 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 ...
-3 votes
1 answer
64 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 ...
3 votes
4 answers
1k 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); ...
0 votes
1 answer
159 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....
0 votes
2 answers
308 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 ...
605 votes
13 answers
261k 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, ...
12 votes
3 answers
11k 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
374 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 ...
0 votes
1 answer
64 views

Plugin system design core component access: Common practice?

I wonder how to design a good plugin architecture in C++/Qt. The main concern I have is about dependency injection vs globals when it comes to the core components. Plugins should have access to ...
5 votes
2 answers
1k views

Handling disposables with dependency injection

I'm struggling to implement disposable objects via dependency injection, as the dependencies are injected in the constructor (and live as long as their containing object does), whereas I want the ...
0 votes
1 answer
135 views

Implement Dependency Inversion in C with UML diagram

As per Robert C. Martin in Clean Architecture, he gives a simple UML diagram to illustrate Dependency Inversion. To put it simply, HL1 initially referred to ML1 without interface to invoke F() ...
0 votes
0 answers
148 views

Microservice code reuse strategy for static classes

Hope this is the right place to post an opinion-based question like this. I'm in the process of converting an aging API monolith to a set of .NET 5/C#-based microservices and working on the code reuse ...
-1 votes
1 answer
809 views

Dependency injection of parameters that depends on each other

When an object creation depends on other objects, it is better to have those objects injected into the constructor of the first object's class (dependency injection). For example: Car car1 = new Car(...
0 votes
2 answers
168 views

Is it safe to use the same IoC container for your own and third-party services?

I am developing an application with ASP.NET Core with the ability to dynamically add new libraries that implement the necessary functions. They can be included at the start. I have already written ...
12 votes
2 answers
8k views

What is the motivation behind the @ImplementedBy annotation in Guice?

I recently read about the @ImplementedBy annotation available in Google Guice. It allows the programmer to specify a binding between an interface and its implementation for future use in dependency ...
48 votes
7 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 ...
3 votes
4 answers
3k 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 ...
0 votes
3 answers
123 views

Is it acceptable to inject dependency container in factories?

When trying to design a factory service in a project where i am using a dependency container i sometime find myself in this situation. I have an interface, in this example ISettingsLoader. ...
12 votes
4 answers
18k views

Dependency injection for a library with internal dependencies

Background I am working on a class library in support of a web site. The library combines related APIs from several different vendors, all of which have their own particular nuances and domain ...
0 votes
1 answer
32 views

Inject dependencies which map to settings locator

At the moment, the application I work on relies on an ISettingsService which is injected via the constructor; classic dependency injection. As time goes and the application's capabilities (understand ...

1
2 3 4 5
13