I'm currently wading my way through articles and questions on the subject of IoC containers, such as:
- Why do I need an IoC container as opposed to straightforward DI code?
- What's the difference between the Dependency Injection and Service Locator patterns?
- Inversion of Control Containers and the Dependency Injection pattern (M.F.)
- Service Locator is an Anti-Pattern (M.S.)
- When to use a DI Container (M.S.)
- Are we using IoC effectively?
There's something that doesn't strike me as obvious right now.
As far as I understand, dependency injection (DI) in its purest form is the following, formulated by myself:
As an object that depends on some abstract services, I don't have to look for these concrete services; concrete instances of them will be provided to me when an instance of myself is created, or very shortly after.
This puts DI directly in contrast with a service locator, as I reformulate:
As an object that depends on some abstract services, I know someone I can call who can provide me with these concrete services; I will ask them for a concrete instance when I require it.
My current understanding of a DI container is this:
As the DI container, I know concrete classes of services that I need to create, and for each class, what abstract services each of them require; I will therefore create instances of these services and provide their dependencies.
I don't find obvious what an IoC container is right now due to conflicting understanding. Depending on the article I'm reading, it looks like:
- Either a IoC container is a synonym for DI container as the two terms are used interchangeably
- This is what I understand when reading M.F. and M.S. articles
- DI container is different from a IoC container as the latter handles instanciation and provides a service locator (i.e.
val x = IoC.resolve<IMyDependency>()
); since a DI container does not require the concrete services to know that a DI container exists at all, they cannot be the same concept.- This would be my understanding when reading questions and answers from StackOverflow
Can you clear that confusion?