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 vote
1 answer
120 views

Can DI without the dependency inversion be replaced by protected properties?

Many people come from frameworks that implement Dependency Injection and IoC containers for everything (in my case Angular 2+), so, this group of people will try to use dependency injection and IoC ...
Vitor Figueredo Marques's user avatar
4 votes
5 answers
279 views

Dependency injection into method calls

Let's use PHP here for examples and illustraion purposes but the question is language and framework agnostic. Many experts say Service Locator (anti)pattern should be avoided and recommend using ...
Mikhail Karakulov's user avatar
0 votes
2 answers
144 views

What is considered best practice for managing dependency injection for multiple libraries with ASP.NET Core Web API?

Suppose we are building a REST API with ASP.NET Core. Suppose there are 3 projects in our solution: The ASP.NET Core Web API project. A class library project for the businnes layer. A class library ...
user1969903's user avatar
0 votes
0 answers
102 views

better structure of a software project to prevent circular dependencies

I am contacting you today regarding a recent problem with circular dependencies. It's about a Spring boot application that accepts data via a Rest API and then forwards it to an internal Spring ...
Phoenix's user avatar
  • 29
0 votes
3 answers
249 views

How to reconcile the fact that dependency Injection break encapsulation? (Especially when others are allowed to wire up your dependencies for you)

I was reading Martin Fowlers take on Dependency Injection, and in general have been trying to discuss it a bit online to help get rid of my own misconceptions and to understand this principle better. ...
Scotty Jamison's user avatar
1 vote
2 answers
190 views

Please explain the "swapable dependency" arguments for IOC containers

I get that IOC containers can be useful to help break dependencies and allow you to test a class in isolation. I don't wish to focus on that right now, instead, I'm trying to understand some of the ...
Scotty Jamison's user avatar
0 votes
1 answer
98 views

Passing data into Repository via dependency injection

Let's say I have a blog, I use DDD approach, I need to pass an instance of PaginationParameters class to PostsRepository, is it ok to bind DI container to concrete instance of PaginationParameters ...
cool_cat's user avatar
2 votes
2 answers
638 views

Relevance of dependency Injection

During my career I only used DI to insert object of certain class based on certain interface. So there was never the situation when I needed two different bindings for one interface. So natural ...
cool_cat's user avatar
0 votes
3 answers
299 views

Why is Dependency Injection called "alternative of global state"?I think global state still exists

According to Why is Global State so Evil?, I believe we should avoid global state, so suppose I have an App that count user clicks in all pages like it: public class GlobalState{ public int ...
wcminipgasker2023's user avatar
-1 votes
7 answers
591 views

If you use Inversion of Control, what alternatives to obfuscated function calls exist?

Consider a class that follows the obfuscated function call anti-pattern. I've also seen these called "stupid classes". The definition of such a class is that it only has one public method ...
J. Mini's user avatar
  • 1,007
0 votes
0 answers
127 views

Dependency Injection Usage

I'm very new to DI, and must admit, am having a hard time understanding to what extent it should be used in an application. Even in a basic project, you can have hundreds of classes that have ...
wforl's user avatar
  • 19
2 votes
3 answers
349 views

Is having many build-once factories a sign of bad dependency injection design?

I have a form. It contains things like grids. Users do things with these grids and what they do with them is sent to a SQL server. Clearly, the server is a volatile dependency and should be dependency ...
J. Mini's user avatar
  • 1,007
2 votes
2 answers
220 views

Is it okay to use Dependency injection only because of unit testing?

I have a class that has dependencies that I know are not going to change. class ConversationFinder { public function __construct( protected Conversation $conversationDbFinder = new ...
Ghassan Barghouti's user avatar
5 votes
3 answers
1k views

In .net 6 dependency injection and Program.cs, what is the best practice for handling a large number of dependencies?

I've seen a ton of examples of how to do DI, and the example always shows two dependencies. But what about when you have 50 or 100 dependencies (or more) in a large-ish application? The Program.cs ...
NovaDev's user avatar
  • 159
0 votes
6 answers
813 views

Is it best practice for each call to a SQL Server to be in its own class, even when using dependency injection?

I often see/write classes that contain every part of the application where the application will talk to a SQL server. For example, you may have a class like this public class Data { private Func&...
J. Mini's user avatar
  • 1,007
0 votes
0 answers
106 views

How to handle dependency injection in a library to avoid frequent breaking changes?

Let's say I have a C# .NET library with the following classes: public class FooService { private readonly IDependencyA a; public FooService(IDependencyA a) { this.a = a; } ...
srk's user avatar
  • 127
2 votes
4 answers
831 views

Dependency injection using method injection vs constructor injection

Where should I inject the dependency when I write a class? Should it be given to __init__ or to the specific method that uses the dependent object? Take the below two pieces of code for example, to me ...
alson_y's user avatar
  • 41
3 votes
1 answer
137 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, ...
Amal K's user avatar
  • 101
0 votes
1 answer
204 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....
James0n's user avatar
0 votes
4 answers
364 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 ...
Ruslan's user avatar
  • 9
-1 votes
1 answer
105 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 ...
ScottishTapWater's user avatar
0 votes
1 answer
97 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 ...
test test's user avatar
2 votes
4 answers
3k 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 ...
PieterV's user avatar
  • 215
1 vote
2 answers
197 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 ...
Nishant Chauhan's user avatar
0 votes
0 answers
122 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 ...
Robin's user avatar
  • 103
1 vote
2 answers
3k 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 ...
EspressoCoder's user avatar
2 votes
2 answers
220 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 ...
Der_Reparator's user avatar
0 votes
2 answers
666 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 ...
Robin Kreuzer's user avatar
0 votes
3 answers
444 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 ...
EspressoCoder's user avatar
1 vote
1 answer
1k 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 ...
chrisxfire's user avatar
1 vote
3 answers
1k 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 ...
Treker's user avatar
  • 189
0 votes
1 answer
388 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 ...
Diego Perez's user avatar
0 votes
4 answers
5k 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 ...
Diego Perez's user avatar
0 votes
1 answer
654 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 ...
Diego Perez's user avatar
1 vote
2 answers
635 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 ...
Union3008's user avatar
0 votes
1 answer
589 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 ...
Florentin Alexandru Iftimie's user avatar
0 votes
4 answers
221 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 ...
Vinicius Scheidegger's user avatar
0 votes
2 answers
492 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 ...
sidgate's user avatar
  • 123
1 vote
3 answers
323 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 ...
pro100tom's user avatar
  • 449
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 ...
pro100tom's user avatar
  • 449
0 votes
2 answers
209 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 ...
void.pointer's user avatar
  • 5,003
-1 votes
3 answers
541 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 ...
Veverke's user avatar
  • 423
-2 votes
1 answer
247 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 ...
Dink McDinkleman's user avatar
1 vote
0 answers
1k 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 ...
user2190492's user avatar
-3 votes
1 answer
141 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 ...
Vitor Figueredo Marques's user avatar
5 votes
4 answers
9k 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); ...
Batesias's user avatar
  • 234
0 votes
2 answers
1k 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, ...
selamba's user avatar
  • 109
0 votes
2 answers
205 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:...
Shadow's user avatar
  • 361
6 votes
5 answers
464 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 ...
Veverke's user avatar
  • 423
0 votes
1 answer
187 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 ...
ManuelSchneid3r's user avatar

1
2 3 4 5
14