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.

41 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
4 votes
0 answers
2k views

Spring bean injection into a hibernate validator constraint

I have a controller method like listed below whose argument is annotated with @Valid to validate PasswordChange object using a Hibernate validator @Constraint. Both PasswordChange and a sample ...
3 votes
0 answers
765 views

Dependency injection - Nested objects

# Introduction I am working on a CMS application in PHP with about 200 classes. The CMS, in general, does the same thing every CMS does: generate sites. I am learning a lot about OOP and design ...
  • 223
3 votes
0 answers
2k views

Dependency Injection and class Inheritance

The project I am working on for about a year now was used to create a new mysqli connection to the database each time there was a Query to the database. In order to quickfix this problem at places ...
  • 225
3 votes
0 answers
504 views

Mocking third party web service

I am working on a project to create a wrapper library which will be used to interact with a web service provided by the client's IT team. The reason for this library is that we interact with this ...
  • 1,240
3 votes
0 answers
647 views

State Pattern Code for embedded device - Dependency Injection of Device Objects in Context and State Classes

I'm wrestling with some code and I think I've finally gotten some state pattern code working with my events. I've got a simplified system that will work like this: This is my first go with State ...
  • 379
3 votes
0 answers
346 views

Manual dependency injection or abstract factory

We're starting to use dependency injection in a fairly large, interactive program. It's early yet, but I have a feeling that the majority of the objects being injected are going to want runtime data ...
  • 139
3 votes
0 answers
779 views

ASP.NET Mvc3 - application/request lifetime and dependency injection

I thought of asking on SO, but it seems this is more of a "concept" type question than a "problem" type question. If it needs to be moved, please do so. Anyway, I'm having a tough time finding ...
  • 1,177
2 votes
0 answers
173 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 ...
  • 1,615
2 votes
0 answers
111 views

Dependency injection of UI components via React context

In React, it's common for libraries, and app-internal utilities, to used context-based dependency injection. Some examples: function MyComponent() { const client = useApolloClient(); // Apollo ...
2 votes
0 answers
58 views

Better approach of testing groups of small services with or without dependency injection

Let's assume, that frameworks I use, provide some kind of dependency injection automatically available in testing environment. Let's also assume, that I have a lot of micro services in my application....
  • 336
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 ...
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. ...
  • 643
1 vote
0 answers
102 views

How can I load service dependencies into model classes?

My model objects are generated by the library using hard-wired new operators, which makes dependencies injection using the constructor impossible. However, they also have methods, which are called by ...
1 vote
0 answers
544 views

Opening new Window from ViewModel using Castle Windsor

I've been following the quick tutorial on using Castle Windsor in MVVM WPF: TUTORIAL I've set everything up (as for the basics) but I came to the point of creating a new Window after a button click. ...
1 vote
0 answers
503 views

Dependency Injection with global objects (sessions)

TL;DR: My question is about how to use in conjunction: the dependency injection design pattern with global / immutable objects (ex: a session that references a customer object) I get the idea of ...
1 vote
0 answers
118 views

disadvantages of classpath-based dependency injection for testing

DI frameworks like Guice and Spring are the common tools to abstract out dependencies to make Java code testable. However, there are several alternate methods to achieve the same goals. One in ...
1 vote
0 answers
59 views

Dealing with stubbed external dependency

In sut I should I have two kinds of dependencies to be stubbed: 3rd party dependencies (mongodb & co) and own dependencies: var async = require('async'), // This dependency should not be stubbed ...
  • 141
1 vote
0 answers
200 views

How to clean composition root from anything other than object construction?

I'm doing poor man's DI (manual injection) in my application. My (pseudo-) composition root looks something like this: var connectionSettings = new Settings(SqlParameters) var dataLoader = new ...
  • 11
1 vote
0 answers
542 views

Am I following a good design thought process? Is the Abp framework overkill?

As I finish one project and start another, I have a small time window to come up with an architecture for the new. I am coming from the following design: Web applicationReceives domain models & ...
1 vote
0 answers
269 views

Single write, multiple read of stateful objects

Scenario: One data input feed (call this Source) Multiple objects (call these Layer1) read this Source, and analyze the feed. This analysis is stateful. Layer1 objects have configuration parameters. ...
  • 7,540
0 votes
1 answer
51 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 ...
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 ...
  • 101
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
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
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
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 ...
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 ...
0 votes
0 answers
87 views

How to design for optional dependency / optional functionality

Let's say for a Net Core library that I will deliver to the customer The library define a Interface like IGetData and also a default GetData class. The library also define a ICacher interface with a ...
  • 131
0 votes
0 answers
129 views

Registering dependency implemented in separate assembly with shared interfaces

Situation: I have a project with a Business Layer (WizBang.BL) that contains nearly all of my application logic. It communicates with a Data Layer (WizBang.Data) that contains by entity classes and ...
  • 101
0 votes
0 answers
1k views

Inherit a class that inherits a generic class to make it easier to read

I'm creating a simple Dependency Injection library for Unity (no constructors available) and I want to use a generic class that implements a generic call for each class that would inherit it allowing ...
0 votes
0 answers
127 views

Is using IoC with data classes a good practice

I usually use IoC frameworks to inject dependencies that are services. Is it ok to mark classes that are data objects as IoC components? To be more clear I will give an example. I have an abstract ...
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
0 answers
553 views

WPF DI with DbContext per form

Trying to figure out how to keep class methods clean, follow EF's guidance of short-lived DbContexts, and use DI in WPF. Issue I'm having is implementing transactions with DI. I'm trying to figure ...
  • 1,179
0 votes
0 answers
100 views

Manual DI and Managing Concrete Implementations

Considering a solution that has some manual DI going on such as the below class, is there a better way of managing the concrete implementations without adding a container. This is theoretical, there ...
  • 1,709
0 votes
0 answers
414 views

Dependency Injection with EF - is mapping required?

I'm implementing the Repository pattern with using DI and EF6. I created the database (that inherits from DbContext) object with the Visual Studio wizard ADO.NET Entity Data Model I have a generic ...
  • 121
0 votes
0 answers
2k views

Are prototype beans with constructor parameters an anti-pattern?

Reading on this, Misko Hevery has said “Newables can ask for other Newables in their constructor, but not for Injectables” Now it appears to me that requesting a prototype be instantiated at run-time ...
-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 ...
  • 303
-1 votes
1 answer
146 views

Laravel Service Providers count and DI

I understand how service prividers in Laravel works and how to use them. But I'm not sure about how to keep clean code, specificaly in count of method parameters. For example I have this route: Route:...
-1 votes
1 answer
727 views

IOC principle - Servlet container implementation

IOC principle can be implemented, using either: Dependency Injection Service locator pattern This article also supports these two approaches for implementing IOC principle. Spring IOC container is ...
-1 votes
3 answers
359 views

Refactoring - Resolve dependencies between legacy code used by third parties

I am developing two libraries in .Net Firs one it's a library with core functionality (named it Library.Core.dll) Let's focus in User class public class User { //set of constructors and ...
  • 592
-1 votes
3 answers
785 views

Factory needs list of available objects. Should it be static or not?

I have a list of IReader that I read at the beginning of my program. Later on I need ReaderFactory to get appropriate IReader based on Extensions it can use. The problem is the factory needs to know ...
  • 492