Search code examples
c#.netdependency-injectionioc-containerlate-binding

(Automatic) Dependency Injection Binding Mechanisms


The two common mechanisms for creating dependency injection bindings, such as through an IOC container, is from an XML configuration or a block of imperative code. In these cases, the key value pair is explicit (i.e. key = requested type, value = returned type).

Still, there is a third "heuristic" approach where an application/IOC container is given only [IMyClass] keys and the container then reflects over a set of application assembly dependencies to find all name-matched concrete classes [MyClass]. Said differently, the "return type" values are discovered rather than declared.

What I'd like to know is twofold:

  1. Which IOC containers (or other late-binding tools) permit the heuristic approach? Does this approach have a more common name?
  2. Are there other binding techniques, besides the three I've listed, which are used in practice?

Solution

  • This is called Convention-based Configuration or Auto-registration and is supported by these .NET DI Containers:

    The most common configuration mechanisms used for DI Containers are

    • XML
    • Code as Configuration
    • Convention-based Configuration

    A fourth, but uncommon, approach is to use attributes. The Managed Extensibility Framework is the most prominent example of this approach, which is more common in Java.