2

I'm currently wading my way through articles and questions on the subject of IoC containers, such as:

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?

Hay
  • 2,246
  • 20
  • 30
  • 2
    One can take one of a few stances on this, but the bottom line, as I see it, is that most people aren't particularly precise in the way that they use words like that. Lots of people use *IoC* and *DI* as synonyms, which also carries over to *IoC Container* versus *DI Container*. I've never heard that these should be two separate concepts. For Fowler's differentiation between *IoC* and *DI*, though, see my answer in the question marked as the duplicate. If you feel that doesn't properly address your question, then please let me know. – Mark Seemann Sep 04 '18 at 13:42

0 Answers0