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
8
votes
3 answers

Sum ruby hash values

I am trying to sum values from a ruby hash but using either inject or reduce does not return the correct answer. It seems as though these methods are overwriting the current value being stored instead of summing them. My hash look like this: @test =…
JordanC
  • 131
  • 1
  • 1
  • 10
7
votes
4 answers

Inject Spring beans into RestEasy

Is it possible to inject Spring beans into an RestEasy @Path class? I managed to do it with Jersey, with @InjectParam annotation, but for some other reasons, I need to switch to RestEasy, and I can't seem to find a way to do it (tried good ol'…
Robert Kovačević
  • 1,378
  • 4
  • 16
  • 23
7
votes
1 answer

InjectMocks is wrongly injecting the same Mock into 2 different fields of similar type despite creating 2 different mocks

I have a class which has 2 fields of similar types. I have mocked them both. But when I use InjectMocks, inject mocks wrongly injects a single mock into both those fields. Here is the example code class: import lombok.AccessLevel; import…
7
votes
4 answers

How to overwrite Spring service beans by name, using annotations only

Given I have a Spring bean configured as @Service("myService") public class DefaultService extends MyService { } and a class using this bean public class Consumer { @Autowired @Qualifier("myService") private MyService service; …
Lars Blumberg
  • 19,326
  • 11
  • 90
  • 127
7
votes
3 answers

Server-side Blazor does not provide HttpClient for injection

I get an error in razor page when I try to inject HttpClient: Unhandled Promise Rejection: Error: System.InvalidOperationException: Cannot provide a value for property 'Http' on type . There is no registered service of type…
Jane
  • 139
  • 1
  • 8
7
votes
1 answer

Unresolved reference when importing views from a submodule using kotlin-android-extensions

When I try to use kotlin-android-extendions' view injection in a multi-module application I get an error injecting a view from an android.library submodule: Unresolved reference: component_xyz_user_name We have a main app module and an…
7
votes
1 answer

How to inject an opentracing span into RabbitMQ?

I´m using OpenTracing and I am trying to propagate a span through RabbitMQ. However I don't understand how I am supposed to inject the span and how to extract it later. This is the code for sending a message def send_message(self, message, tracer): …
BoliBoom
  • 101
  • 1
  • 6
7
votes
2 answers

Inject the instance of a component in a service

For a any good reaon, I am trying to inject my component into a service, but I obtain a new instance of it. I reproduce my problem with this code : This component will display the instance number in h1 : @Component({ selector: 'my-component', …
Karbos 538
  • 2,977
  • 1
  • 23
  • 34
7
votes
3 answers

Butterknife 8.4.0 - Plugin with id 'android-apt' not found

I keep getting the error Plugin with id 'android-apt' not found. What's going wrong here? plugins { id "me.tatarka.retrolambda" version "3.2.5" } apply plugin: 'com.android.application' apply plugin: 'android-apt' android { …
Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
7
votes
5 answers

Jersey Hk2 injecting @Service annotated classes

For the life of me I cannot get Jersey with hk2 to automatically discover @Service annotated classes and inject them. I have tried to follow every advice on stack overflow, jersey and hk2 documentation and still no luck. I am trying to inject a…
Daniel David Kovacs
  • 226
  • 1
  • 2
  • 13
7
votes
1 answer

Guice dynamic inject with custom annotation

I have some resource, but I can not iterator it and bind them all, I have to use the key to request the resource.So, I have to dynamic inject. I define an annotation like @Target({ METHOD, CONSTRUCTOR, FIELD…
wener
  • 7,191
  • 6
  • 54
  • 78
7
votes
1 answer

how to inject quartz's job with ninject?

I use ninject and quartz.net in my application and I want to inject job with ninject,But I do not know how to ,because all I know is that jobdetail is created by class of Jobimpl instead of an instance,such as: JobBuilder.Create() Does…
TommyLike
  • 1,010
  • 11
  • 20
7
votes
3 answers

Injecting Mouse Input in WPF Applications

I've been working on injecting input into a WPF application. What makes this project hard is that I need to be able to inject the input into the application even though it's running in the background (i.e. another application has the input focus).…
dreijer
  • 654
  • 8
  • 22
6
votes
1 answer

System.TypeLoadException - Method get_*** in type *** from assembly *** does not have an implementation when it does?

I am using the program Reflexil to inject a reference in the executable Foo1.exe to an outside assembly named Foo2.dll. In Foo1.exe, there is a class called Bar. In Foo2.dll, there is an interface IBar which correctly implements all of the fields…
Gbps
  • 857
  • 2
  • 14
  • 29
6
votes
3 answers

How to inject or use Http inside static method in angular 2 final service?

I am using Angular 2 final version. I have a validator service in Angular 2. I am writing a static method for asynchronous validation which uses HttpModule. So in this context, how to inject and use Http, so that I can make a call to backend. I…
revathi
  • 205
  • 1
  • 5
  • 12