Skip to main content

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
2 answers
111 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 ...
Akira's user avatar
  • 247
0 votes
2 answers
126 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 ...
JohnyProkie's user avatar
3 votes
2 answers
192 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 ...
jpaugh's user avatar
  • 239
1 vote
1 answer
100 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 ...
Veverke's user avatar
  • 461
2 votes
2 answers
184 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 ...
Артем Антонов's user avatar
-2 votes
1 answer
75 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 ...
TPBKK's user avatar
  • 1
4 votes
2 answers
521 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 ...
Mad Scientist's user avatar
0 votes
1 answer
108 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 ...
user1785960's user avatar
-1 votes
1 answer
141 views

Will I lose confidence of my code working in mocking dependency injected services? [duplicate]

Let's say that I have a class with a service that is going to be injected at runtime: class Thing { private magic: IMagic; // Magically injected service public doStuff(){ // Do a lot ...
Tizio Fittizio's user avatar
1 vote
1 answer
161 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
373 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
589 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
174 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
  • 67
0 votes
3 answers
339 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
197 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
104 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
649 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
1 vote
3 answers
372 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
641 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,017
0 votes
0 answers
143 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
422 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,017
3 votes
2 answers
380 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
2k 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
1k 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,017
0 votes
0 answers
147 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
3 votes
4 answers
1k 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
  • 151
3 votes
1 answer
187 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
262 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
459 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
137 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
106 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
5k 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
201 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
180 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
2 votes
2 answers
4k 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
256 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
949 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
546 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
2k 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
2 votes
3 answers
2k 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
  • 199
0 votes
1 answer
440 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
8k 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
967 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
1k 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
791 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
236 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
622 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
389 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
249 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,083

1
2 3 4 5
14