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
21
votes
6 answers

Null after @InjectMocks

I am having some troubles passing a dependency while unit testing with JUnit. Consider these pieces of code: This is the dependacy injecton into the class which i want to test, lets call it Controller. @Inject private FastPowering fastPowering; …
radpet
  • 701
  • 1
  • 5
  • 16
20
votes
1 answer

How can I use Gulp to add a line of text to a file

I've been trying to figure out how to add a line of text to any type of file using Gulp. For instance add: @import 'plugins' to my main.sass file. Or add a CDN to an index.html file. I did try: gulp.task('inject-plugins', function(){ …
Darryl Morley
  • 699
  • 1
  • 7
  • 20
20
votes
2 answers

Mockito @InjectMocks doesn't work for fields with same type

I was very surprised to find out that following simple code example doesn't work for all Mockito versions > 1.8.5 @RunWith(MockitoJUnitRunner.class) public class MockitoTest { @Mock(name = "b2") private B b2; @InjectMocks private A…
Oleg Tsal-Tsalko
  • 225
  • 1
  • 3
  • 8
16
votes
4 answers

The function '$initGetIt' isn't defined while using injectable pub package

I am trying to implement stacked architecture by following this video uploaded by FilledStacks. I encountered a problem while using injectable pub package https://pub.dev/packages/injectable My locator.dart class code is as follow import…
Faizan Kamal
  • 1,732
  • 3
  • 27
  • 56
16
votes
4 answers

What is the difference between @RequiredArgsConstructor(onConstructor = @__(@Inject)) and @RequiredArgsConstructor?

What is the difference between Lombok's @RequiredArgsConstructor and @RequiredArgsConstructor(onConstructor = @__(@Inject)) I know that RequiredArgsConstructor injects all the final dependencies in constructor only.
rishabhjainps
  • 410
  • 1
  • 5
  • 14
16
votes
6 answers

Spring JavaConfig for java.util.Properties field

Can you please tell me how to use Spring Javaconfig to directly load/autowire a properties file to a java.util.Properties field? Thanks! Later edit - still searching for the answer: Is it possible to load with Spring JavaConfig a properties file…
Roxana
  • 1,569
  • 3
  • 24
  • 41
15
votes
1 answer

Android & RoboGuice - Inject views on Fragment?

I have a fragment that I need to display on the screen. I want to be able to use InjectView to inject my UI elements. InjectView works fine on activities because the view (xml) is set during onCreate, however on fragments the view is set on…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
15
votes
2 answers

Angular 2 can't resolve all parameters for service

I have two services: LoginService and UserService. I am trying to inject UserService into LoginService and the app won't run. In the console, I have the error: Error: Can't resolve all parameters for UserService: ([object Object], ?). at…
Yassine CHABLI
  • 3,459
  • 2
  • 23
  • 43
15
votes
5 answers

ruby inject with conditional in block?

doing the first Project Euler question: summing the multiples of 3 and 5 between 1 and 1000, I came up with this (pretty simple) sum = 0 1.upto(999) { |i| sum += i if 0 == i%3 || 0 == i%5 } sum but I thought this would work but it doesn't, can…
Tonys
  • 3,349
  • 4
  • 24
  • 27
14
votes
2 answers

How to test implementations of Guice AbstractModule?

How to test implementations of Guice AbstractModule in a big project without creating fake implementations? Is it possible to test bind() and inject() methods?
Nikolas
  • 2,322
  • 9
  • 33
  • 55
14
votes
5 answers

Sails.js - How to inject a js file to a specific route?

For example, I have a page /locations/map which I need to include Google Map library, and include a .js file (e.g. location.js) specifically for this page only. I want to inject these 2 files to after this line Is it possible to…
Js Lim
  • 3,625
  • 6
  • 42
  • 80
12
votes
2 answers

Chrome extension, replace HTML in response code before browser displays it

i wonder if there is some way to do something like that: If im on a specific site i want that some of javascript files to be loaded directly from my computer (f.e. file:///c:/test.js), not from the server. For that i was thinking if there is a…
lukasz
  • 867
  • 1
  • 8
  • 17
12
votes
3 answers

Why do I need to use .inject(0) rather than .inject to make this work?

I am creating a rails app and have used this code in one of my methods item_numbers.inject(0) {|sum, i| sum + i.amount} item_numbers is an array of objects from my item_numbers table. The .amount method that I apply to them looks up the value of an…
brad
  • 9,573
  • 12
  • 62
  • 89
12
votes
1 answer

Can someone explain what com.google.inject does?

I've seen a class declared with its only constructor being annotated with @Inject. And I don't see the one constructor being called anywhere in the entire project. So two questions: What does @Inject mean? (What does it do? Why is the constructor…
One Two Three
  • 22,327
  • 24
  • 73
  • 114
11
votes
6 answers

Using #inject to join strings from an array

I'm going through an online lesson, which usually has a very simple one line solution. A problem states that, given the following array: ["emperor", "joshua", "abraham", "norton"] I must use #inject to get a single string of all names joined…
Darek Rossman
  • 1,323
  • 1
  • 10
  • 12
1
2
3
66 67