Questions tagged [dependency-injection]

Dependency Injection is a set of software design principles and patterns that enables you to reduce coupling between components

Dependency injection (DI) is a set of software and 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.

Benefits of Dependency Injection

  • Separation of concerns.
  • Boilerplate-code reduction in application classes because all work to initialize dependencies is handled by the composer.
  • Configurable components makes application easily extendable.
  • Unit testing is easy with mock objects.

Disadvantages of Dependency Injection

  • If overused, it can lead to maintenance issues because effect of changes are known at runtime.
  • Dependency injection hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.

Background questions

Resources / books

The following list of books (in print) focus specifically on Dependency Injection.

Books are presented in opposite order of publication

Related Patterns

26300 questions
101
votes
5 answers

Replace service registration in ASP.NET Core built-in DI container?

Let us consider a service registration in Startup.ConfigureServices: public void ConfigureServices(IServiceCollection services) { services.AddTransient(); } Is it possible to change IFoo registration to FooB after AddTransient has…
Ilya Chumakov
  • 23,161
  • 9
  • 86
  • 114
99
votes
5 answers

ASP.NET Core initialize singleton after configuring DI

So let's say I have a singleton class instance that I register in the DI like this: services.AddSingleton(); And let's say the Foo class has a number of other dependencies (mostly repository classes that allow it to load data). With my…
pbz
  • 8,865
  • 14
  • 56
  • 70
98
votes
4 answers

Symfony 2 EntityManager injection in service

I've created my own service and I need to inject doctrine EntityManager, but I don't see that __construct() is called on my service, and injection doesn't work. Here is the code and configs:
Andrey Zavarin
  • 1,483
  • 2
  • 13
  • 12
97
votes
4 answers

Difference between "Inversion of Control", "Dependency inversion" and "Decoupling"

I'm reading theory about dependency inversion and decoupling and I can't see the difference between the two. Dependency inversion talks about decoupling functional components so that higher level components don't depend on lower level…
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
95
votes
1 answer

How to inject application context in a repository with Hilt?

I want to inject an application context into a repository class to create a room database instance inside the repository. I am using Hilt for dependency injection. Can we use hilt for passing application context or do I have to manually pass it? I…
Tushar
  • 1,076
  • 1
  • 9
  • 11
94
votes
7 answers

Can I pass constructor parameters to Unity's Resolve() method?

I am using Microsoft's Unity for dependency injection and I want to do something like this: IDataContext context = _unityContainer.Resolve(); var repositoryA = _unityContainer.Resolve(context); //Same instance of…
NotDan
  • 31,709
  • 36
  • 116
  • 156
94
votes
2 answers

Generic repository in ASP.NET Core without having a separate AddScoped line per table in Startup.cs?

I have a generic repository in my project. Consider the following controller snippet public class Lookup1Controller : Controller { readonly MyDbContext _db; public Lookup1Controller(MyDbContext dataContext) { _db = dataContext; …
dfmetro
  • 4,462
  • 8
  • 39
  • 65
93
votes
2 answers

How to do DI in asp.net core middleware?

I am trying to inject dependency into my middleware constructor as follows public class CreateCompanyMiddleware { private readonly RequestDelegate _next; private readonly UserManager _userManager; public…
93
votes
3 answers

AngularJS seed: putting JavaScript into separate files (app.js, controllers.js, directives.js, filters.js, services.js)

I'm using the angular-seed template to structure my application. Initially I put all my JavaScript code into a single file, main.js. This file contained my module declaration, controllers, directives, filters, and services. The application works…
ChrisDevo
  • 1,222
  • 2
  • 13
  • 17
92
votes
3 answers

Reader Monad for Dependency Injection: multiple dependencies, nested calls

When asked about Dependency Injection in Scala, quite a lot of answers point to the using the Reader Monad, either the one from Scalaz or just rolling your own. There are a number of very clear articles describing the basics of the approach (e.g.…
adamw
  • 8,038
  • 4
  • 28
  • 32
89
votes
6 answers

How can I pass a runtime parameter as part of the dependency resolution?

I need to be able to pass a connection string into some of my service implementations. I am doing this in the constructor. The connection string is configurable by user will be added the ClaimsPrincipal as a Claim. All fine so far. Unfortunately, I…
Ant Swift
  • 20,089
  • 10
  • 38
  • 55
88
votes
8 answers

Is it a good practice to have logger as a singleton?

I had a habit to pass logger to constructor, like: public class OrderService : IOrderService { public OrderService(ILogger logger) { } } But that is quite annoying, so I've used it a property this for some time: private ILogger logger =…
Giedrius
  • 8,430
  • 6
  • 50
  • 91
88
votes
8 answers

Spring JUnit: How to Mock autowired component in autowired component

I've got a Spring component I'd like to test and this component has an autowired attribute which I need to change for the purpose of unit testing. The problem is, that the class uses the autowired component inside the post-construct method so I'm…
NeplatnyUdaj
  • 6,052
  • 6
  • 43
  • 76
88
votes
6 answers

re-open and add dependencies to an already bootstrapped application

Is there a way to inject a late dependency to an already bootstrapped angular module? Here's what I mean: Say that I have a site-wide angular app, defined as: // in app.js var App = angular.module("App", []); And in every page:
sa125
  • 28,121
  • 38
  • 111
  • 153
87
votes
2 answers

What is a composition root in the context of dependency injection?

I am exploring dependency injection and the term composition root is used all over the place. So what is it?
Thomas
  • 2,137
  • 1
  • 17
  • 38