pcah/python-clean-architecture

DI: enhance injection mechanism to allow relative qualifiers

lhaze opened this issue · 1 comments

lhaze commented

Allow qualifiers to be defined not by a static value (known at the import time), but by the dependant (to be computed at the runtime).

  • This simplifies the relationship between the Repository pattern and the Dao pattern (they are transitively related through the Factory pattern).
  • The very dependency that can be now statically declared in the class scope (by the Inject descriptor), allowing DI introspection (i.e. get_dependencies_contexts) to gather such a relationship.

Example:

class Foo:
   qualifier = 'qualifier'

    # a qualifier the classic way, statically defined
    bar: Bar = Inject(qualifier=Foo)
    # the new way to relatively define a qualifier, determined in the runtime by the value of a function
    # of the component of the DI
    baz: Baz = Inject(get_qualifier=lambda foo: foo.qualifier)

    @property
    def bazoo(self):
        # the old way to relaively define a qualifier
        return self.container.find_by_interface(Baz, qualifier=self.qualifier)
lhaze commented

Already done.