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

GIN @Inject on variable for Rpc Services

I'm a bit lost with the use of Inject on variable. I got this code working : private XXServiceAsync xxServiceAsync; @Inject protected IndexViewImpl(EventBus eventBus, XXServiceAsync tableManagementServiceAsync) { super(eventBus, mapper); …
Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
3
votes
2 answers

Would ASLR cause friction for the address with DLL injection?

I was reading about the DLL injection technique, and I had this question in mind. Let us assume we want to inject a DLL into a destination process in Windows 7 which has ASLR enabled for kernel32.dll So any piece of the injected code can't use any…
CnativeFreak
  • 712
  • 12
  • 27
3
votes
4 answers

How to inject Javascript in WebBrowser control

There is a great tutorial here about windows forms How to inject Javascript in WebBrowser control? I tried it and it works great But the problem is the objects used there is not recognized at wpf application. So what i am asking is what is the…
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
3
votes
6 answers

Ruby Map/Reduce Function Necessarily Efficient?

b1 = Time.now puts (1..100000).inject(0) { |x, y| x + y } a1 = Time.now puts "Time for inject: #{a1 - b1}" b2 = Time.now sum = 0 (1..100000).each do |value| sum += value end puts sum a2 = Time.now puts "Time for each: #{a2 - b2}" The above…
Terry Li
  • 16,870
  • 30
  • 89
  • 134
3
votes
1 answer

how to vite inject css as http file during development rather inline style tag injection

Vite Bundler environment during development Vite environment inject CSS into the page as inline: how do i inject CSS into the page as a HTTP file instead?
David
  • 1,284
  • 1
  • 11
  • 21
3
votes
1 answer

Ruby inject daisy chaining?

I'm not sure what sugar syntax this is, but let me just show you the problem. def factors num (1..num).select {|n| num % n == 0} end def mutual_factors(*nums) nums .map { |n| factors(n) } .inject(:&) end p mutual_factors(50, 30) …
3
votes
3 answers

Inject sleep() into a function of an external process

I know how to inject a DLL into a running process and also how to utilize functions used internally by the process e.g. void__stdcall remoteMethod(unsigned short id) { typedef void (__stdcall *pFunctionAddress)(unsigned short); pFunctionAddress…
Steve
  • 738
  • 1
  • 9
  • 30
3
votes
1 answer

How to inject viewModel in base classes like baseActivity or baseFragment using hilt. Android kotlin

I tried to inject viewModel to my base activity using hilt abstract class BaseActivity(val bindingFactory(LayoutInflater) -> VB) : AppCompatActivity() { private val viewModel: VM by viewModels<>() …
Milan Jayan
  • 96
  • 1
  • 6
3
votes
1 answer

TypeScript / Vue 3: Injecting mutating function causes TypeScript error "Object is of type 'unknown'"

I’m new in TypeScript and trying to use it with Vue 3 composition API and provide / inject. Let's say in parent component A I have something like this: // Parent component A import { provide, ref } from 'vue'; import ScoreType from…
3
votes
1 answer

Injecting a bean with @Autowired that implements ApplicationListener does not work?

I have a service bean (annotated with @Service) which implements the ApplicationListener inteface for T type of event objects that extend the ApplicationEvent abstract Class. There is a pretty simple and clear example of this in the Spring docs here…
nvrs
  • 720
  • 2
  • 17
  • 25
3
votes
1 answer

Nuxt plugin throwing inject is not a function

I decided to play a little bit with nuxt for the first time from scratch to finish. and now, I am trying to add plugins. the plugin I am trying to add is for my api. But when I inject it, it throws the error "inject is not a function". This is my…
godofjs
  • 93
  • 1
  • 7
3
votes
2 answers

Exist stringByEvaluatingJavaScriptFromString for Android

Exist stringByEvaluatingJavaScriptFromString method in Android like Iphone?. Not with a simple code like this javascript:wave(). But with a complex Java Script function. Thanks
user801661
  • 177
  • 8
  • 15
3
votes
1 answer

How to do method injection in loopback 4

I want to create a method checkAddressEnterContestBefore to check some requirement. this function requires to connect to database (mysql) but I cannot inject repository. What could I do in this situation? import { Contest } from '../models'; import…
3
votes
1 answer

NestJS y TypeORM: InjectRepository undefined

I have a problem in a service that I am developing with nestjs and TypeORM The system runs with a docker, everything works fine on my pc, but when I run it on any other side it stops working. The error says: TypeError: Cannot read property 'findOne'…
Francuchin
  • 33
  • 1
  • 4
3
votes
2 answers

How can I inject parameters into a TestCafé test?

Scenario: I run TestCafé wrapped in code, using the API I have a test I want to parameterize, testing with different dynamic values. Problem Testcafé has no support for sending parameters to a test. Is there a way to inject values?
Sven
  • 892
  • 14
  • 29