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

Why not use an IoC container to resolve dependencies for entities/business objects?

I understand the concept behind DI, but I'm just learning what different IoC containers can do. It seems that most people advocate using IoC containers to wire up stateless services, but what about using them for stateful objects like…
85
votes
16 answers

Injecting a Spring dependency into a JPA EntityListener

I am trying to inject a Spring dependency into an JPA EntityListener. Here is my listener class: @Configurable(autowire = Autowire.BY_TYPE, dependencyCheck = true) public class PliListener { @Autowired private EvenementPliRepository…
balteo
  • 23,602
  • 63
  • 219
  • 412
83
votes
5 answers

Use DbContext in ASP .Net Singleton Injected Class

I need to access my database in a Singleton class instantiated in my Startup class. It seems that injecting it directly results in a DbContext that is disposed. I get the following error: Cannot access a disposed object. Object name:…
Tjaart
  • 3,912
  • 2
  • 37
  • 61
82
votes
3 answers

Injecting dependencies into ASP.NET MVC 3 action filters. What's wrong with this approach?

Here's the setup. Say I have some action filter that needs an instance of a service: public interface IMyService { void DoSomething(); } public class MyService : IMyService { public void DoSomething(){} } I then have an ActionFilter that…
BFree
  • 102,548
  • 21
  • 159
  • 201
81
votes
6 answers

How to inject a repository into a service in Symfony?

I need to inject two objects into ImageService. One of them is an instance of Repository/ImageRepository, which I get like this: $image_repository = $container->get('doctrine.odm.mongodb') ->getRepository('MycompanyMainBundle:Image'); So how…
ChocoDeveloper
  • 14,160
  • 26
  • 79
  • 117
80
votes
13 answers

How to get bean using application context in spring boot

I am developing a SpringBoot project and I want to get the bean by its name using applicationContext. I have tried many solution from web but could not succeed. My Requirement is that I have a controller ControllerA and inside the controller I…
Qasim
  • 9,058
  • 8
  • 36
  • 50
80
votes
6 answers

ASP.NET Core Dependency Injection with Multiple Constructors

I have a tag helper with multiple constructors in my ASP.NET Core application. This causes the following error at runtime when ASP.NET 5 tries to resolve the type: InvalidOperationException: Multiple constructors accepting all given argument types…
79
votes
4 answers

How to use log4net with Dependency Injection

I'm trying to figure out what the right patter and usage of log4net is with a dependency injection framework. Log4Net uses the ILog interface but requires me to call LogManager.GetLogger(Reflection.MethodBase.GetCurrentMethod().DeclaringType) in…
Micah
  • 111,873
  • 86
  • 233
  • 325
79
votes
5 answers

Dependency injection and named loggers

I am interested in learning more about how people inject logging with dependency injection platforms. Although the links below and my examples refer to log4net and Unity, I am not necessarily going to use either of those. For dependency…
wageoghe
  • 27,390
  • 13
  • 88
  • 116
79
votes
7 answers

Explain why constructor inject is better than other options

In a Pro Spring 3 Book, Chapter 4 - Introduction IOC and DI in Spring - Page 59, In "Setter Injection vs. Constructor Injection" section, a paragraph says Spring included, provide a mechanism for ensuring that all dependencies are defined when …
minil
  • 6,895
  • 16
  • 48
  • 55
78
votes
6 answers

Asp.Net Core: register implementation with multiple interfaces and lifestyle Singleton

Considering the following interface and class definitions: public interface IInterface1 { } public interface IInterface2 { } public class MyClass : IInterface1, IInterface2 { } is there any way to register one instance of MyClass with multiple…
Maxim
  • 1,413
  • 2
  • 10
  • 15
77
votes
3 answers

How to setup app settings in a .Net Core 3 Worker Service

I have been looking at a number of tutorials and SO questions (such as App Settings .Net Core) regarding reading appsettings.json in .Net Core 3 and I cannot find any pointers on how-to when dealing with the Worker service. There is no Startup…
77
votes
1 answer

Resolve IContainer

What is the suggested method of getting the Autofac container from inside a class in the application? Does Autofac provide for resolving an IContainer property on a class or do I need to store the container globally once I've build it?
stimms
  • 42,945
  • 30
  • 96
  • 149
77
votes
2 answers

How to use dependency injection with an attribute?

In an MVC project I'm creating I have the following RequirePermissionAttribute that gets put on any action that needs specific permissions (it's been simplified for this example): public class RequirePermissionAttribute : ActionFilterAttribute,…
TheCloudlessSky
  • 18,608
  • 15
  • 75
  • 116
77
votes
9 answers

How would one do dependency injection in scala?

I'm still at the beginning in learning scala in addition to java and i didn't get it how is one supposed to do DI there? can or should i use an existing DI library, should it be done manually or is there another way?
Fabian
  • 2,428
  • 2
  • 21
  • 19