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
0
votes
1 answer

Set the gravity of an element when zooming?

I'm injecting an iframe into a webpage, to act as a toolbar for another element, but unfortunately the elements act weirdly when zooming. The toolbar seems to gravitate towards the topleft corner, and the element towards the top-center. This means…
user1460217
0
votes
1 answer

Detect when a method is being run in javascript (event)

Im looking to inject a variable into a page before a method is run. The situation is as follow CUSTOM JS CAN GO HERE my.var = 'cake'; my.function(); I dont have access to be able to modify the page directly, however there is a content area at the…
0
votes
2 answers

post json to java server

I've been googleing a bit around internet looking for a (correctly) way to @Consume an "application/json" file in a web resource on my server. I'm using glassfish app server, so it's a java resource. here is the calling javascvript code: var…
Dario Rusignuolo
  • 2,090
  • 6
  • 38
  • 68
0
votes
2 answers

Ruby equilibrium index - odd usage of inject

I spent a bit of time today tackling the equilibrium index problem (described here) After writing my own solution (which performed badly with large numbers), I decided to find one that would be a perfect score. I found this (which Codility scores as…
Stu
  • 154
  • 1
  • 11
0
votes
1 answer

JSF - Cannot inject Spring Data JPA repository into Converter

I am certain I have got this working in the past, but it doesn't want to work today. Example: @FacesConverter(value = "inputConverter") public class InputConverter implements Converter { private InputRepository inputRepository; @Override …
8bitjunkie
  • 12,793
  • 9
  • 57
  • 70
0
votes
1 answer

Library insertion/injection not work with open command

I writed a dynamic library that loaded into target application by environment variable DYLD_INSERT_LIBRARIES, the dylib works fine when application executed from terminal by this commands: $ export DYLD_INSERT_LIBRARIES=/path/to/mylib.dylib $…
mh taqia
  • 3,506
  • 1
  • 24
  • 35
0
votes
1 answer

Is it possible to inject code from script into running rails app

Assume I have a running ruby on rails app. I’m creating classes (db-models) defined by a description provided at runtime. Right now, I’m using the rails app (via browser) to trigger the code generation — and therefore the app “knows” about its new…
Johannes
  • 55
  • 4
0
votes
2 answers

Rails summing the values in a hash thats sorted using Regex

I have a query here that involves a few elements, as a result my brain is climbing out my ears. i have a hash that's populated with symbols as keys and numbers as values, the hash is used as both a storage for methods(via method_missing) and the…
legendary_rob
  • 12,792
  • 11
  • 56
  • 102
0
votes
1 answer

How can I add to an image an HTML link using Google Chrome extension?

I have inserted an image in a div using CSS to all the pages on a certain domain, using my Google Chrome extension. Users keep asking me to link the image to the domain's home page. I don't know how to do this. The image is at the top right of the…
user1356682
-1
votes
1 answer

NullInjectorError: No provider for Auth

I have a problema with this service. enter image description here This is my app.module. enter image description here The injection in InicioPage: enter image description here I tried , add in providers in iniciopage.module, but not work
-1
votes
1 answer

Trigger Vue actions / events from outside a Vue app?

I have my own JavaScript code injected to the scope of a 3rd party Vue web app. I want to trigger actions in the Vue App (same as when a user interacts with its components) simply from my JS code. Is it possible to do somehow?
amiregelz
  • 1,833
  • 7
  • 25
  • 46
-1
votes
1 answer

Constructor with 2 parameters actually has 3

When using reflection to inquire information about the Constructor of a class in runtime. public class amongUs { ... ... ... @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.METHOD}) @interface Message { …
-1
votes
1 answer

why do the latest iPhones dislike my Javascript?

I can tell from my webserver logs that one of my Javascript files is treated by (some?) iOS 14 as "injected". My script is run in a separate environment (namespace?). And its attempts to do AJAX fail, apparently because it's assigned an "origin"…
Chuck Kollars
  • 2,135
  • 20
  • 18
-1
votes
1 answer

Add HTML code in .html files with javascript

I'm developing a website with bootstrap. If I want to modify the navbar, I don't want to go to any html files and make changes. So would like to use javascript to "inject" the html code into the actual html file but I don't know how to do it. This…
-1
votes
2 answers

How do I sum the elements of the inner array? (Ruby)

Assuming I have an array like this, a = [[1, 2], [1, 4], [1, 8], [1, 16]] how do I convert the above into the following in Ruby? a = [3, 5, 9, 17] Thx in advance!
J.Yan
  • 55
  • 2
  • 8