mlp/src/injector.py

Overview:

This Python code defines a class named Injector, which is designed to implement a simple Dependency Injection (DI) mechanism. Dependency Injection is a design pattern that allows a program to remove hard-coded dependencies and instead inject them at runtime, thereby increasing modularity and testability.

Injector (class):

Attributes:

  • _registry: a class-level dictionary that serves as a registry for interfaces and their corresponding implementations. The keys are the interfaces (typically class or function references), and the values are the concrete implementations that should be used at runtime

Methods:

  • register(interface, implementation): a static method used to register a mapping between an interface and its implementation. The interface parameter is the abstract base or the concept being registered, and implementation is the concrete class or function that fulfills the interface’s contract. This mapping is stored in the _registry dictionary.
  • resolve(interface): another static method used to resolve and instantiate an implementation for the given interface. It checks if the interface is present in the _registry. If it is, the method retrieves the corresponding implementation and examines its constructor (__init__ method) using the inspect.signature function to obtain information about its parameters. For each parameter (excluding self), the method attempts to resolve its type annotation as another dependency by recursively calling Injector.resolve. If successful, it instantiates the implementation with the resolved dependencies and returns the instance. If the interface is not registered in the _registry, it raises a ValueError indicating the lack of a registered implementation.