DEV Community

Cover image for DEPENDENCY INJECTION.
Clara Situma
Clara Situma

Posted on • Updated on

DEPENDENCY INJECTION.

Dependency injection is a design pattern where the dependencies of a component are provided to that component by an external source, rather than being created or managed by the component itself.

This allows for greater flexibility and modularity, as the component can be used in different contexts and with different dependencies.

In dependency injection, the external source is responsible for injecting the dependencies into the component, hence the name "dependency injection".

Here is an example of dependency injection in React:


import React from 'react';

// A component that accepts a dependency as a prop
function MyComponent(props) {
  // Use the dependency in the component
  const result = props.dependency.doSomething();
  return <div>{result}</div>;
}

// The parent component that injects the dependency
function App() {
  const dependency = {
    doSomething: () => 'Hello from the dependency!'
  };
  return <MyComponent dependency={dependency} />;
}
Enter fullscreen mode Exit fullscreen mode

In this example, the MyComponent component accepts a dependency prop and uses it to perform some action.

The App component is the parent component that injects the dependency by passing it in as a prop to MyComponent. This allows MyComponent to be used in different contexts and with different dependencies.

Dependency injection is a design pattern that implements inversion of control by providing the dependencies of a component to that component from an external source. This external source is responsible for injecting the dependencies into the component, hence the name "dependency injection".

In summary, inversion of control is a principle that states how a component should receive its dependencies, while dependency injection is a pattern for implementing IoC in a specific way.

Benefits of using Dependency Injection

  1. Helps in Unit testing.

  2. Boiler plate code is reduced, as initializing of dependencies is done by the injector component.

  3. Extending the application becomes easier.

  4. Helps to enable loose coupling, which is important in application programming.

Disadvantages of Dependency Injection

  1. It’s a bit complex to learn, and if overused can lead to management issues and other problems.

  2. Many compile time errors are pushed to run-time.
    Dependency injection frameworks are implemented with reflection or dynamic programming.

This can hinder use of IDE automation, such as “find references”, “show call hierarchy” and safe refactoring.

Here is an example of a real code situation that uses dependency injection in React:


import React, { useState } from 'react';

// A component that accepts a "service" as a prop
function TodoList(props) {
  const [todos, setTodos] = useState([]);

  // Use the service to get the todo items from the server
  props.service.getTodos().then(todos => setTodos(todos));

  return (
    <ul>
      {todos.map(todo => (
        <li key={todo.id}>{todo.text}</li>
      ))}
    </ul>
  );
}

// The parent component that injects the "service"
function App() {
  // The "service" object that provides the API for getting todo items
  const service = {
    getTodos: () => fetch('/todos').then(response => response.json())
  };
  return <TodoList service={service} />;
}
Enter fullscreen mode Exit fullscreen mode

In this example, the TodoList component accepts a service prop and uses it to fetch the todo items from the server.

The App component is the parent component that injects the service by passing it in as a prop to TodoList. This allows TodoList to be used in different contexts and with different services for fetching data.

Top comments (0)