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
4
votes
2 answers

Add new binding to a Guice Module?

I've my own Guice module and in the configure method, I've provided my own bindings as under - public void configure() { MapBinder m = MapBinder.newMapBinder(binder(), String.class,…
4
votes
1 answer

Ruby #inject behavior is different from documentation

Taking a look at Ruby documentation on Enumerable class I noticed something interesting and I'd like to know why it happens this way. At #inject description I've found these examples: # Sum some numbers (5..10).reduce(:+) …
Ed de Almeida
  • 3,675
  • 4
  • 25
  • 57
4
votes
1 answer

Change Variable Value in JVM with GDB

Currently I have a simple Java program: public class Test { public static void main(String[] args) { boolean test = true; while (test) { System.out.println("Hello World"); try { Thread.sleep(1000); } catch (Exception e) {} …
Doz Parp
  • 279
  • 4
  • 23
4
votes
2 answers

How to create new object in typescript having angular-injected parameters

I have a typescript class, in which constructor I have a normal and an angular-injected argument: export class MyClass { private translation:string; public static $inject = ['$filter']; constructor(name:string, $filter:…
iberbeu
  • 15,295
  • 5
  • 27
  • 48
4
votes
1 answer

Why is my stubbed method returning null?

I have a class Dummy. I inject 3 variables. However, one of them is not injectable because it is an interface. So I inject an object, one of whose methods return the needed type. Class Dummy { private final Class1 class1; private final Class2…
Thiyagu
  • 17,362
  • 5
  • 42
  • 79
4
votes
2 answers

Drag new DOM element and push others out of the way before injecting

So I'm about to hang up the gloves and give up, but before that I'm going to try StackOverflow. :) What I'm trying to achieve: I'm building an app where one of its functionalities is to create new DOM elements (mostly styled divs) and inject those…
YOMorales
  • 1,926
  • 1
  • 15
  • 25
4
votes
2 answers

0xC0000022 before RtlUserThreadStart

I'm injecting some code to hook apis in processes but I have some issues in some applications like chrome.exe My test app launches a suspended process, do injection and api hooking and then resumes it. CreateProcessW is hooked in order to be able to…
Mauro H. Leggieri
  • 1,084
  • 11
  • 25
4
votes
2 answers

Inject events in Android without root

I've been trying to figure out how to inject touch/ keyboard events into an Android device for a while now (within and outside of you application). I found an app that does this without root…
Habeeb Ahmed
  • 217
  • 4
  • 9
4
votes
3 answers

Ruby: Sum selected hash values

I've got an array of hashes and would like to sum up selected values. I know how to sum all of them or one of them but not how to select more than one key. i.e.: [{"a"=>5, "b"=>10, "active"=>"yes"}, {"a"=>5, "b"=>10, "active"=>"no"}, {"a"=>5,…
Digger
  • 67
  • 3
  • 8
4
votes
2 answers

How to automatically inject JavaScript into web pages such that its variables can be accessed from the developer console

I have a library of JavaScript functions that I want to be able to access from the developer console of a web browser. I want to be able to run my functions side-by-side with the functions and variables defined by the webpage. One solution I see is…
crouleau
  • 156
  • 1
  • 1
  • 6
4
votes
1 answer

Memory efficiency of each versus reduce/inject in Ruby

I have a large array (10+ million objects) that I want to combine into a single object (for simplicity, let's assume here a string) and run each element in the array through some processing (abstracted by the function do_stuff). There are two…
Gordon Seidoh Worley
  • 7,839
  • 6
  • 45
  • 82
4
votes
1 answer

How to add a "Virtual"/"Shadow" Group to SimpleCursorTreeAdapter?

I have a problem concerning a SimpleCursorTreeAdapter which I subclassed in my project. It currently communicates with a SQLite-DB over a ContentProvider. Now, i have the problem that i would like to insert some kind of "Shadow" or "Virtual" Group…
4
votes
1 answer

$(document).ready(function() after HTML injection

I'm trying to live edit data. To do this I found this script http://www.9lessons.info/2011/03/live-table-edit-with-jquery-and-ajax.html The script works perfectly when the table get's created in the php file but I'm trying to do this by injecting…
eric22269
  • 65
  • 1
  • 8
4
votes
1 answer

How to use javax.inject Provider within spring xml configuration

I have a class called let's say A with such a setter: //class A @Inject public void setAProvider(Provider b) { this.b = b; } It works fine with javax.inject and annotation configuration when I want to have only one kind of A instance.. My…
Pawel Batko
  • 761
  • 7
  • 19
4
votes
1 answer

Is possible to inject method with arguments in Spring?

From docs: In the client class containing the method to be injected (the CommandManager in this case), the method that is to be 'injected' must have a signature of the following form: [abstract]
user710818
  • 23,228
  • 58
  • 149
  • 207