Skip to content

How to restrict DependenciesContainer type? #513

@chbndrhnns

Description

@chbndrhnns

I am trying to re-create an onion-layer architecture with the dependency injector library. Here is a snippet:

from dependency_injector import containers, providers

class DbAdapter:

  def get_all(self):
    return []

class Repo:
  def __init__(self, adapter):
    self._adapter = adapter


class Adapters(containers.DeclarativeContainer):
    db_adapter = providers.Singleton(
        DbAdapter
    )


class Repositories(containers.DeclarativeContainer):
    adapters: Adapters = containers.DependenciesContainer()

    repo = providers.Factory(
        Repo, adapter=adapters.db_adapter
    )

Can I make dependency-injector check the provided container for adapters and reject invalid types?

Activity

chbndrhnns

chbndrhnns commented on Oct 21, 2021

@chbndrhnns
Author

According to my experiences with the library, this seems not possible. Is it something you see as valuable to have, @rmk135?

self-assigned this
on Oct 21, 2021
rmk135

rmk135 commented on Nov 8, 2021

@rmk135
Member

Hi @chbndrhnns ,

Thanks for sharing the use case. Yes, I think you can be something like:

class Repositories(containers.DeclarativeContainer):
    adapters: Adapters = containers.DependenciesContainer(instanceof= Adapters)

    repo = providers.Factory(
        Repo, adapter=adapters.db_adapter
    )

I'll add it to the backlog.

chbndrhnns

chbndrhnns commented on Nov 8, 2021

@chbndrhnns
Author

Your example is not working for me as containers (line 2) does not seem to have a DependenciesContainer. In version 4.37, only providers has DependenciesContainer.

Also, when I try to run Repositories() from the snippet below, it fails:

class Repositories(containers.DeclarativeContainer):
    adapters: Adapters = providers.DependenciesContainer(instance_of=Adapters)

    repo = providers.Factory(Repo, adapter=adapters.db_adapter)

Error:

src/dependency_injector/containers.pyx:742: in dependency_injector.containers.DeclarativeContainer.__new__
    ???
src/dependency_injector/containers.pyx:392: in dependency_injector.containers.DynamicContainer.load_config
    ???
src/dependency_injector/containers.pyx:193: in traverse
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

>   ???
E   AttributeError: type object 'Adapters' has no attribute 'related'

src/dependency_injector/providers.pyx:4813: AttributeError

rowan-maclachlan

rowan-maclachlan commented on Mar 24, 2022

@rowan-maclachlan

Yeah, same issue for me.

chbndrhnns

chbndrhnns commented on Oct 18, 2022

@chbndrhnns
Author

@rmk135 i am coming back to this issue now and then. Do you have any idea what to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @rmk135@chbndrhnns@rowan-maclachlan

      Issue actions

        How to restrict DependenciesContainer type? · Issue #513 · ets-labs/python-dependency-injector