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

Java Rest Easy WebContext Injection

I'm setting a java application with RestEasy, and I created an authentication filter to get jwt token and extract from it user information (for example a user DTO). I'd like to inject a bean (RequestScope?) to save user information, but I don't know…
duns
  • 83
  • 1
  • 16
-1
votes
1 answer

How are variables injected into JavaScript in HTML?

I realised that I don't know how variables are injected into JavaScript in HTML. I would like to test overriding a clickTag variable which is simply a var in the main body of a JavaScript ( a default value ), but to override it with the injected…
gingrrr
  • 87
  • 1
  • 8
-1
votes
1 answer

why @inject method is not working on the live server

I am developing a laravel 5 app. In my code I inject a class that I created in App/Utilities/class.php. On my homestead machine it's working fine, I pushed the files to a server hosted on Digital ocean using forge and everything worked perfectly…
arakibi
  • 441
  • 7
  • 24
-1
votes
1 answer

Inject the mock object where an actual implementation

I want to know what does this code mean: mathApplication.setCalculatorService(calcService); Why should I use an interface and make object from it? And what does this injection means? Here is my Tester Code: import org.easymock.EasyMock; import…
Wooopsa
  • 320
  • 1
  • 6
  • 22
-1
votes
1 answer

Does guice allow injectMembers into a constructor?

My problem is here; I don't know if i can do that with guice: public class Foo { private final Bar bar; @Inject public Foo(Injector injector) { this.bar = new Bar(); injector.injectMembers(bar); } } *bar will be injected correctly or not ?…
Romain-p
  • 518
  • 2
  • 9
  • 26
-1
votes
1 answer

Inject javascript command on webbrowser c#

I want to inject this commands on webbrowser.. How I can do it? oFormObject = document.forms['formid']; oFormObject.elements["name"].value = 'Some Value'; document.getElementById('formid').submit();
stefy97100
  • 41
  • 7
-1
votes
2 answers

Ruby Inject Block acting weird?

I have arrays. Their values are either 0 or a float. I wanted to find out how many non-zero values (hits) there were in my array. So I used inject for the following. entry[5] is where I am storing the number of hits, entry[6..56] is where the data…
-1
votes
1 answer

Groovy building a list with inject

All, I am having an issue here with Groovy. Specifically I would like to use the inject method on a current list that I have. I need this list to be Immutable and built per element. Here is what my list looks like: def initialList = [ "A", "B", "C"…
DaGr8Gatzby
  • 21
  • 2
  • 7
-1
votes
1 answer

PDO Class and MYSQL request

I have a PDO class wrapper: class DB { private $dbh; private $stmt; public function __construct($user, $pass, $dbname) { $dsn = 'mysql:host=localhost;dbname=' . $dbname; $options = array( …
XTRUST.ORG
  • 3,280
  • 4
  • 34
  • 60
-1
votes
1 answer

refactoring ruby code inject issues

Hi can someone explain to me why my inject isn't working here? Am I using inject correctly here? For some reason my code gets caught up in an endless loop once this scenario comes into play (the 5th move usually in my game) def cpu_block_player …
JaTo
  • 2,742
  • 4
  • 29
  • 38
-1
votes
1 answer

jquery mobile and append/injecting HTML

check this out.
AfricanMatt
  • 145
  • 1
  • 9
-1
votes
1 answer

When adding book values to @Entity values turn out null JavaEE

i have probably done something very stupid and i have tried and tried for about 4 hours now and i can't figure out what is wrong. When i try to add a book to the database, MySQL increments but all the values set in the Mysqltable columns are…
Toerktumlare
  • 12,548
  • 3
  • 35
  • 54
-1
votes
1 answer

Can I inject JavaScript code into an image?

I have a gallery section in my website, and I want to upload an image that's injected with JavaScript. I need the JavaScript to run when it's opened and enlarged by a modal box. How can I do that?
-2
votes
2 answers

Do we have to write setter method to use @Inject annotation?

is it mandatory to write a setter function when we use @Inject annotation
-2
votes
1 answer

Symfony access parameters from services.yaml in Entity

In Symfony 5.0 I need to access parameters defined in services.yaml inside an entity. When injecting the parameter_bag in services.yaml into my entity like App\Entity\MyEntity: class: App\Entity\MyEntity calls: - [setParameterBag,…
1 2 3
66
67