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
1 answer
98 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 ...
user avatar
  • 121
1 vote
3 answers
192 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 ...
user avatar
0 votes
2 answers
87 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 ...
user avatar
0 votes
2 answers
130 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 ...
user avatar
  • 4,795
-1 votes
2 answers
199 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 ...
user avatar
  • 293
-2 votes
1 answer
130 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 ...
user avatar
0 votes
0 answers
69 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 ...
user avatar
-3 votes
1 answer
53 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 ...
user avatar
3 votes
4 answers
325 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); ...
user avatar
  • 204
0 votes
2 answers
144 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, ...
user avatar
  • 109
0 votes
1 answer
79 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:...
user avatar
  • 353
6 votes
5 answers
359 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 ...
user avatar
  • 293
0 votes
1 answer
53 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 ...
user avatar
0 votes
1 answer
117 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() ...
user avatar
  • 185
0 votes
0 answers
124 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 ...
user avatar
-1 votes
1 answer
311 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(...
user avatar
  • 65
0 votes
2 answers
146 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 ...
user avatar
0 votes
4 answers
1k 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 ...
user avatar
0 votes
3 answers
122 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. ...
user avatar
  • 59
0 votes
1 answer
122 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....
user avatar
  • 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 ...
user avatar
  • 227
0 votes
6 answers
358 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 ...
user avatar
  • 17
-2 votes
3 answers
101 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 ...
user avatar
  • 1
0 votes
1 answer
101 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 ...
user avatar
  • 1,191
2 votes
4 answers
137 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 ...
user avatar
0 votes
0 answers
33 views

How to use a custom scope in multi-module Android project?

My project consists of following modules: -app -presentation -domain -data -injection In injection module i create Dagger modules, components and custom Activity scope (@ActivityScope). Since, ...
user avatar
1 vote
2 answers
263 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 ...
user avatar
0 votes
2 answers
194 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 ...
user avatar
2 votes
1 answer
116 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; ...
user avatar
  • 23
0 votes
2 answers
131 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 ...
user avatar
0 votes
2 answers
303 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 ...
user avatar
3 votes
4 answers
205 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: ...
user avatar
0 votes
2 answers
82 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 ...
user avatar
5 votes
3 answers
414 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 ...
user avatar
2 votes
1 answer
198 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 ...
user avatar
  • 37.5k
1 vote
3 answers
187 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. ...
user avatar
  • 65
-2 votes
1 answer
79 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 ...
user avatar
  • 117
6 votes
1 answer
177 views

Service Design Pattern

I'm working with services and I found out there are at least 3 ways to use them inside controllers... Statically: Like helper, Text::uppercase('foo') Instancing it: $text = new Text(); $text->...
user avatar
  • 171
-3 votes
1 answer
74 views

Should you use your own implementation of inversion of control instead of a dependency injection container? [duplicate]

I have a small project that has 6 service classes and a main class. Since they are service classes and does not store any state in it, I declared one static method in each one: class Main calls A....
user avatar
2 votes
1 answer
719 views

Dependency Injection: Dealing with late property initialization

I am using some C# pseudo to illustrate here. Below, you will see a Service and a Client (in the context of using DI). The service has a required property that will need to be initialized by the ...
user avatar
0 votes
1 answer
143 views

Session Scoped Dependency Injection vs Caching in Data Access Layer

The issue at hand is that I don't want to repeatedly hit the DB to look up user information for the logged in user over the course of the many requests made within a single session. My first ...
user avatar
  • 459
4 votes
1 answer
456 views

Dependency Injection: What are advantages of using a framework? [duplicate]

Introduction and Question I understand what the advantages of dependency injection, e.g. constructor injection or setter injection and that it is one way of doing inversion of control. I also ...
user avatar
  • 297
2 votes
0 answers
148 views

Dependency injection in .NET Core 3.x Worker Services: Why so hard to consume Transient services now?

I have a Repository service that should be Transient. It is used in many applications. I have a new application that's a Console App, and current guidance suggests implementing my business logic in ...
user avatar
  • 1,611
0 votes
2 answers
151 views

Inversion of control

I have been reading about inversion of control and dependency injection and I was wondering the following. Is there a good way to tell when it is okay to initialise an object inside a method body of a ...
user avatar
0 votes
1 answer
158 views

How can i handle many children viewmodels in MVVM?

Let's suppose i have a viemwodel which requires a lot of different children viewmodels. An example could be a settings menu: the main viewmodel should be able to load large number of children ...
user avatar
  • 59
5 votes
4 answers
328 views

How to avoid injecting uninitialized dependencies?

I have some components which require some asynchronous calls to be finished before those components are fully initialized and ready for work. I do not want dependant objects to know about these ...
user avatar
3 votes
3 answers
418 views

How to decouple chain hierarchy project dependency by dependency injection? (C# .Net Standard)

I'm working on decoupling a Xamarin (.NetStandard) solution. It was using DryIOC container but I didn't see the IOC structure from the dependency, as the projects are still tightly coupled. Three main ...
user avatar
  • 133
0 votes
0 answers
35 views

Dependency Injector and Collections

I have been searching for an answer in this topic but I haven’t been able to find a satisfactory one like in other topics, where the consensus is solid. The situation To keep things simple: I am ...
user avatar
-2 votes
2 answers
185 views

What are the disadvantages to using this form of pure DI in Python?

I want to perform dependency injection in my Python application without using reflection. My position on reflection is informed largely by the "don't hide things" philosophy (I am unable to ...
user avatar
  • 105
0 votes
1 answer
131 views

Is it bad practice to mutate your HttpClient objects?

Im in the process of creating a base class for typed HttpClient services (yes, I'm utilizing IHttpClientFactory). I wrote the process in such a way that for each CRUD method invoked from the service, ...
user avatar
  • 1,349

1
2 3 4 5
13