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
0 votes
0 answers
39 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 ...
  • 101
0 votes
1 answer
151 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 ...
2 votes
2 answers
162 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
80 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 ...
0 votes
3 answers
126 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
112 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
154 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 ...
  • 151
0 votes
0 answers
31 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
52 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
3 answers
270 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
1 answer
70 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
2 answers
152 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
104 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
4 answers
193 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 ...
0 votes
2 answers
211 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 ...
  • 123
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 ...
  • 451
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 ...
  • 451
0 votes
2 answers
156 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 ...
  • 4,873
-1 votes
2 answers
220 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 ...
  • 303
-2 votes
1 answer
145 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
321 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
61 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); ...
  • 204
0 votes
2 answers
288 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, ...
  • 109
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:...
  • 363
6 votes
5 answers
373 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 ...
  • 303
0 votes
1 answer
63 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 ...
0 votes
1 answer
134 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() ...
  • 185
0 votes
0 answers
145 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
722 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(...
  • 65
0 votes
2 answers
160 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 ...
3 votes
4 answers
2k 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. ...
  • 59
0 votes
1 answer
153 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....
  • 109
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 ...
  • 247
0 votes
6 answers
423 views

If it is a bad practice to use an interface if only one class will implement it, what is the purpose of IoC container?

Okay, first of all I understand the concept of IoC container! It's used to implement automatic dependency injection so you won't have to manually inject dependencies to class. It can automatically ...
  • 17
-2 votes
3 answers
143 views

Singleton as Interface for testability via dependancy injection

It can be found in many advices on topic that having Singletons is an anti-pattern. Especially for cases of testability. Can someone please advice/critique on this way (please see code below) of ...
  • 101
0 votes
1 answer
102 views

How should I provide access to "global" objects down to other objects which are contained in a central "application" object

Note This is a bit lengthy to have give a better understanding of the situation and to get some context. You might spot other architectural flaws (it's from an ancient application). I appreciate any ...
  • 1,191
2 votes
4 answers
138 views

Should I divide the dependencies binding to a DI container between tiers and layers?

I'm developing a microservice application divided into 4 layers (API, Application, Domain and Infrastructure). The composition root of my application is an assembly in the API layer. From what I ...
1 vote
2 answers
292 views

How can I manage dependency injection in test code?

This question is a follow-up question to Clean Architecture use case testing. Suppose the production code injects Use Case Interactor into the Input Boundary - that happens somewhere in the main ...
0 votes
2 answers
209 views

Difference between Efferent Coupling and Dependency Injection

I am working on research that analyzes dependency injection (DI) in Java projects. The more I read, the more I get confused by DI in relation to other frameworks and even software quality metrics. I ...
2 votes
1 answer
143 views

Builder that creates a repository for a specific entity - is this an established pattern?

Suppose I have the following entities: class Employee { public string Id { get; set; } public ICollection<EmployeeBadge> Badges { get; set; } } class Badge { public string Id { get; ...
  • 23
0 votes
2 answers
134 views

Dependency injection vs other design pattern

I really don't love the design pattern I've established here. It's kind of DI but not really, and the alternative is more constructor based DI, but I also hate it. Maybe it is because it is JS and ...
0 votes
2 answers
410 views

Do you usually instantiate dependency objects in the constructor?

I know that we usually inject the dependencies by instantiating them in the constructor of the class we are injecting to. However, in my own experience, I just pass the class of the dependency as a ...
3 votes
4 answers
207 views

Shouldn't the scope of an injected dependency be dictated by the underlying implementation?

So, I'm binding an InMemoryCachedCarRepositoryDecorator to an ICarRepository, and obviously I should bind it as a singleton, because otherwise it wouldn't work (as intended). So this got me thinking: ...
0 votes
2 answers
83 views

Language-independent explanation of "registration" by the IoC container

I've been reading quite a lot on the concept of Inversion of Control (IoC) and Dependency Injection (DI), and I've seen that DI often relies on an IoC container, which among other roles has to ...
5 votes
3 answers
423 views

Qualms over the 'flexibility' advantage of using dependency injection

I'm currently studying dependency injection and I'm having some issues with the so called 'flexibility' advantage of using dependency injection. The flexibility advantage is mentioned in post ...
2 votes
1 answer
256 views

Dependency injection post-serialization

I'm modeling a general RPG-game-style quest system where player choices lead to certain effects. The below examples have been simplified for the sake of clarity. This is a web-based context, which I ...
  • 38.7k
1 vote
3 answers
189 views

Too much parameter in a class' constructor, when trying to conserve the dependency injection principle [duplicate]

I am developing a simulator of a bank software system, where each operation (deposit, withdraw, transfer, ...) is just a string. I have 5 main classes : Bank where bank accounts are stored. ...
  • 65
-2 votes
1 answer
104 views

Is there a proper way of implementing runtime control of dependencies using DI? Is factory pattern okay? [closed]

I'm currently brushing up and learning about a bunch of techniques to hopefully begin implementing in my own workflow; one of which is IoC (and DI in particular). I'm hoping someone could clear up my ...
  • 117

1
2 3 4 5
13