Questions tagged [ioc-container]

In object oriented languages, an inversion of control container (ioc-container) can be used for configuring and managing objects in an application.

In object oriented languages, an inversion of control container (ioc-container) can be used for configuring and managing objects in an application. The container is responsible for managing object lifecycles (creation, initialization, destruction) and configures objects by wiring them together.

Objects managed by the container obtain references to other objects (their dependencies):

  • by explicitly asking the container, using dependency lookup methods on the container
  • automatically, because the container injects the dependencies using one or more dependency injection techniques

Frameworks that provide an ioc-container support one of these methods and often both.

The following frameworks are discussed here at Stackoverflow

For .NET:

For Java:

2163 questions
598
votes
30 answers

Why do I need an IoC container as opposed to straightforward DI code?

I've been using Dependency Injection (DI) for a while, injecting either in a constructor, property, or method. I've never felt a need to use an Inversion of Control (IoC) container. However, the more I read, the more pressure I feel from the…
Vadim
  • 21,044
  • 18
  • 65
  • 101
582
votes
11 answers

How does autowiring work in Spring?

I'm a little confused as to how the inversion of control (IoC) works in Spring. Say I have a service class called UserServiceImpl that implements UserService interface. How would this be @Autowired? And in my Controllers, how would I instantiate an…
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
355
votes
10 answers

How to avoid Dependency Injection constructor madness?

I find that my constructors are starting to look like this: public MyClass(Container con, SomeClass1 obj1, SomeClass2, obj2.... ) with ever increasing parameter list. Since "Container" is my dependency injection container, why can't I just do…
JP Richardson
  • 38,609
  • 36
  • 119
  • 151
319
votes
7 answers

How do the major C# DI/IoC frameworks compare?

At the risk of stepping into holy war territory, What are the strengths and weaknesses of these popular DI/IoC frameworks, and could one easily be considered the best? ..: Ninject Unity Castle.Windsor Autofac StructureMap Are there any other…
ocodo
  • 29,401
  • 18
  • 105
  • 117
257
votes
4 answers

.NET Core DI, ways of passing parameters to constructor

Having the following service constructor public class Service : IService { public Service(IOtherService service1, IAnotherOne service2, string arg) { } } What are the choices of passing the parameters using .NET Core IOC…
user9124444
152
votes
5 answers

Is there a pattern for initializing objects created via a DI container

I am trying to get Unity to manage the creation of my objects and I want to have some initialization parameters that are not known until run-time: At the moment the only way I could think of the way to do it is to have an Init method on the…
97
votes
2 answers

Adding services after container has been built

Is it possible to register a service at run-time, meaning after the ContainerBuilder has been built and the Container has been created (and ContainerBuilder disposed of)?
Paul Knopf
  • 9,568
  • 23
  • 77
  • 142
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
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…
76
votes
3 answers

Laravel: Difference App::bind and App::singleton

I get a bit confused over all the nice things laravel has to offer in terms of the IOC container and facades. Since I'm not an experienced programmer it gets overwhelming to learn. I was wondering, what is the difference between these two…
Luuk Van Dongen
  • 2,391
  • 6
  • 26
  • 40
73
votes
8 answers

Self injection with Spring

I tried the following code with Spring 3.x which failed with BeanNotFoundException and it should according to the answers of a question which I asked before - Can I inject same class using Spring? @Service public class UserService implements…
Premraj
  • 7,802
  • 8
  • 45
  • 66
72
votes
8 answers

Is it possible to use Dependency Injection with xUnit?

I have a test class with a constructor that needs an IService. public class ConsumerTests { private readonly IService _service; public ConsumerTests(IService servie) { _service = service; } [Fact] public void…
Rookian
  • 19,841
  • 28
  • 110
  • 180
70
votes
1 answer

How do I effectively design my application where most classes depend on ILogger?

I am injecting a Logger component into all my classes. Most of my classes have the Logger property defined, except where there is inheritance chain (in that case only the base class has this property, and all the deriving classes use that). When…
user1178376
  • 928
  • 2
  • 10
  • 24
64
votes
1 answer

Creating an instance using Ninject with additional parameters in the constructor

I decided to start using Ninject and face an issue. Say I have the following scenario. I have an IService interface and 2 classes implementing this interface. And also I have a class, which has a constructor getting IService and an int. How can I…
StuffHappens
  • 6,457
  • 13
  • 70
  • 95
56
votes
4 answers

Inject Generic Implementation using Guice

I would like to be able to inject a generic implementation of a generic interface using Guice. public interface Repository { void save(T item); T get(int id); } public MyRepository implements Repository { @Override public void…
Sean Carpenter
  • 7,681
  • 3
  • 37
  • 38
1
2 3
99 100