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

What is @Inject for in Kotlin?

I've read what the annotation says but I'm kinda dumb, so couldn't understand propertly Identifies injectable constructors, methods, and fields. May apply to static as well as instance members. An injectable member may have any access modifier…
RodParedes
  • 378
  • 1
  • 2
  • 12
11
votes
1 answer

Ruby: sum vs. inject(:+) produces different results

I've noticed array.sum and array.inject(:+) produce different results. What's the reason for this? a = [10, 1.1, 6.16] a.inject(:+) # => 17.259999999999998 a.sum # => 17.26
Ilya Cherevkov
  • 1,743
  • 2
  • 17
  • 47
11
votes
1 answer

what is the purpose of @Nonbinding annotation in a Qualifier supposed to be in Java EE7?

I am reading through the CDI injections in JavaEE 7 particularly using @Qualifier and @Produces to inject a custom Data type into a bean. I have the following code taken from JBoss documentation towards ends of the…
brain storm
  • 30,124
  • 69
  • 225
  • 393
10
votes
2 answers

How should I extend Injectable from another Injectable with many Injections in angular2?

Is it possible to do something like this? (cause I tried, and haven't succeed): @Injectable() class A { constructor(private http: Http){ // <-- Injection in root class } foo(){ this.http.get()... }; } @Injectable() class B…
karina
  • 789
  • 9
  • 26
10
votes
3 answers
9
votes
1 answer

CDI Replacement for @ManagedProperty

I'm trying to convert some code from Richfaces 4 showcase to use CDI instead of JSF annotations. I understand that I can use @Named to replace @MangedBean and @Inject to replace @ManagedProperty. But I'm having some trouble. I'm trying to convert…
Jflywheel
  • 135
  • 3
  • 8
9
votes
1 answer

How can I use/inject a service in a "normal" c# class like in Blazor @inject ClassName classObject

I have a Blazor Project, in the Program.cs(formaly aka Startup.cs) I added a service builder.Services.AddSingleton(); I can use/access that service on a razor/blazor page like this : @inject…
nogood
  • 1,117
  • 1
  • 11
  • 34
9
votes
4 answers

django best way to pass data to javascript

I used to "tie" my data to the DOM until I've discovered the data binding libraries. My question, say I have a table which contains model records, how can I build that table with JS, i.e pass the objects into javascript rather then straight build…
user3599803
  • 6,435
  • 17
  • 69
  • 130
9
votes
2 answers

What is the difference between `@Bind` and `@BindView` in butterknife?

I Just started using butterknife. In the project, colleagues using butterknife, version is 7.0.0. I saw him write @Bind(R.id.tv_name). But I see butterknife official website butterknife version is 8.0.1, syntax is @BindView(R.id.tv_name) Is…
iRuoBin
  • 123
  • 1
  • 8
9
votes
1 answer

Can Shopify app automatically inject liquid code inside shop's theme?

I'm building a Shopify application and I'm interested in automatically adding a liquid content into the shop's theme.
vovafeldman
  • 585
  • 10
  • 17
8
votes
6 answers

Ruby longest word in array

I built this method to find the longest word in an array, but I'm wondering if there's a better way to have done it. I'm pretty new to Ruby, and just did this as an exercise for learning the inject method. It returns either the longest word in an…
clem
  • 3,524
  • 3
  • 25
  • 41
8
votes
1 answer

how to get my configuration values in yml - using dropwizard (microservice) Jersey D.I @Injection?

here's my code snippets. here's my yml file: productionServer: host: production-server.amazonaws.com publicIp: xx.xx.xx.xx privateIp: xx.xx.xx.xx userName: xx.xx.xx.xx password: xx.xx.xx.xx remoteFilePath: fake/path/ fileName:…
teodoro
  • 247
  • 7
  • 21
8
votes
2 answers

using npm scripts to Inject *.js and *.css into index.html

I´m looking into switch from gulp/grunt to only use npm scripts. But I cant really solve how to get *.js and *.css from a given path and add it to the index.html file. must I add it thru a "index.js" file or can I do something like... "scripts": { …
Mackelito
  • 4,213
  • 5
  • 39
  • 78
8
votes
4 answers

Scala (Play 2.4.x) How to call a class with @inject() annotation

I'm looking at the scaly code example from play-mailer: https://github.com/playframework/play-mailer It goes basically like this: class MyComponent @Inject() (mailerClient: MailerClient) { ... } simple enough and it compiles without compliant…
Techmag
  • 1,383
  • 13
  • 24
8
votes
1 answer

Symfony Inject Entity Manager to Custom Validator

In my symfony application i need a custom validation constraint, which should check if an email is unique. This Constraint should be used as an annotation. I followed the guideline from the symfony cookbooks, but i get exception: Attempted to load…
BloodandDeath
  • 163
  • 1
  • 5
1 2
3
66 67