Questions tagged [inject]

Design pattern to reduce coupling between components, by dynamically injecting into a software component dependencies that it needs to function.

Synonym for Dependency Injection

Dependency injection (DI) is a design pattern for object-oriented programming involving dynamically injecting (inserting) into a software component dependencies (service components) that it needs to function, without needing the dependent component to hard-code a dependency on the service. This reduces coupling between the dependent consumer and the service.

Resources / background questions

If you need a general introduction to DI you should refer to this question: What is dependency injection?

If you need a non-technical introduction you can refer to this question: How to explain Dependency Injection to a 5-year old.

If you would like to understand the relationship between DI and Inversion of Control (IoC), see Inversion of Control < Dependency Injection.

For book recommendations see Dependency Injection book recommendation(s)(dead link).

For general recommendations for writing DI-friendly code without a DI Container, see Dependency Inject (DI) “friendly” library.

If you are wondering why you should use a DI Container instead of Poor Man's DI, see Why do I need an IoC container as opposed to straightforward DI code?

If you are wondering what the Composition Root is, see What is a composition root in the context of Dependency Injection.

For potential downsides of using DI, see What are the downsides to using dependency injection?

Dependency injection and Inversion of Control are closely related. The difference between them is discussed at where-exactly-is-the-difference-between-ioc-and-di.

Also can read basic here : Dependency Injection For Beginner - 1

Related Patterns

1000 questions
2
votes
4 answers

How to use preg_replace to inject a classname into an html node?

I set myself this rather simple sounding challenge but now I am stuck trying to figure out how to inject a classname onto the dom element of my document. The complexity is because I don't have control over the HTML markup I am getting via the…
Jannis
  • 17,025
  • 18
  • 62
  • 75
2
votes
1 answer

How to inject C# dll to another process

Anybody know how/if I can create C# DLLs then inject them into another process (not .net) and execute my code, overriding existing methods?
Mataharrie
  • 43
  • 1
  • 4
2
votes
2 answers

How to unit test a function that uses the new Angular 14's "inject" function?

I've created a function that utilizes that new Angular 'inject' function. Since the inject function can be used only when initializing a class (or factory) that is part of the dependency injection tree, this function is meant to be used in the…
Shachar Har-Shuv
  • 666
  • 6
  • 24
2
votes
1 answer

Java Quarkus how can I pass @ConfigProperty to an constructor?

I have the following class: @ApplicationScoped public class AppScopeTest { @ConfigProperty(name = "my-config-prop") String test; private TestClass testclass; @Inject public AppScopeTest () { this.testClass= new…
santi254
  • 37
  • 3
2
votes
2 answers

Javascript Iframe Inject to beginning of tag

Does anyone know if there is a way to add a iframe directly under the tag? I see the appendChild but that does it before . Any suggestions will be helpful. Also, I doubt there is a way. But the website that is inside the iframe. Is…
Richard Dev
  • 1,110
  • 7
  • 21
2
votes
0 answers

Can't properly inject my ViewModel error: [Dagger/MissingBinding]

I can't inject my viewModel properly can I please get some help ? I keep getting ViewModel cannot be provided without an @Inject constructor or an @Provides-annotated method. Here is my code @Singleton @Component(modules =…
Mitashkin
  • 21
  • 2
2
votes
2 answers

Injecting .NET 4.0 Library into unmanaged App, Unending loop in clr.dll

I am trying to inject a .net library into a managed app, but it didn't seem to work after some debugging with ollydbg i found it to be looping infinitely. If i forcefully exit the loop it all works. I have created a work around (See code below) but…
user940602
  • 73
  • 4
2
votes
1 answer

Spring.net inject dictionary order question?

I use spring.net inject a Dictionary in this order:
spark
  • 21
  • 2
2
votes
2 answers

Unable to inject dependencies to FastAPI endpoints

I have configured a dependencies.py where I'm injecting a set of dependencies to different services by using python's binder.bind(my_config). The goal is being able to easily inject those services to each endpoint of my API. The problem arises when…
2
votes
1 answer

Rewriting Enum#inject to take a Symbol as a Parameter in Ruby

So, I'm going through The Odin Project's Ruby Path, and in one of the Projects we have to rewrite some of the Enum functions to their basic functionality. Thing is, I want to try to go beyond and recreate those functions as close to the original…
jampamatos
  • 37
  • 5
2
votes
1 answer

Configuring KaTeX (code injection) in Circle.so

I am building an online community for IB Math students and for that I plan to use Circle.so. Circle.so seems like a great option given its features (like code injection) and design. One of the features that the forum tool (such as Circle) needs to…
bru1987
  • 56
  • 2
  • 14
2
votes
1 answer

@Inject interface with two implementation

I am using Microprofile and I have a question. I have an interface with a method inside: public interface CheckData extends Serializable{ MyObject retrieveData(String name); } This interface is implemented by 2 different classes( Class A and…
Prodox21
  • 107
  • 1
  • 2
  • 9
2
votes
2 answers

Inject different implementations based on application property in Quarkus

I have a Repository interface that has two implementations. One reads data from a locally stored CSV file while the other reads from an Amazon Dynamo DB. I would like to be able to switch between which implementation I'm using based on an…
ijm3
  • 71
  • 1
  • 8
2
votes
3 answers

how to "inject" built-in php functions

How can i modify returning value of built-in php function without creating new function with another name and renaming all of used functions with previous name to new one? e.g. function time() { return time()-1000; } Of course this won't pass,…
gadelat
  • 1,390
  • 1
  • 17
  • 25