dependency-injectioninversion-of-control

Simplest explanation of how a DI container works?


In simple terms and/or in high-level pseudo-code, how does a DI container work and how is it used?


Solution

  • At its core a DI Container creates objects based on mappings between interfaces and concrete types.

    This will allow you to request an abstract type from the container:

    IFoo f = container.Resolve<IFoo>();
    

    This requires that you have previously configured the container to map from IFoo to a concrete class that implements IFoo (for example Foo).

    This in itself would not be particularly impressive, but DI Containers do more:

    Once you start trying to manually manage composition and lifetimes you should start appreciating the services provided by a DI Container :)

    Many DI Containers can do much more than the above, but those are the core services. Most containers offer options for configuring via either code or XML.

    When it comes to proper usage of containers, Krzysztof Kozmic just published a good overview.