Skip to content

How can I register an interface with multiple interfaces in a container? #507

@satodaiki

Description

@satodaiki

hi.

Sorry for the confusing question.
I want to register only the interface in the Container without having an implementation.

Specifically, it is as follows:

import abc

from dependency_injector import containers, providers

# It is an implementation of Repository that executes user registration/update.
# It is implemented separately for Postgres implementation and InMemory implementation.

class InsertRepository(metaclass=abc.ABCMeta):
    def insert(entity):
        pass

class UpdateRepository(metaclass=abc.ABCMeta):
    def update(entity):
        pass

class DeleteRepository(metaclass=abc.ABCMeta):
    def delete(entity):
        pass

class PostgresInsertRepository(InsertRepository):
    def insert(entity):
        # awesome code

class PostgresUpdateRepository(UpdateRepository):
    def update(entity):
        # awesome code

class PostgresDeleteRepository(DeleteRepository):
    def delete(entity):
        # awesome code

class InMemoryInsertRepository(InsertRepository):
    def insert(entity):
        # awesome code

class InMemoryUpdateRepository(UpdateRepository):
    def update(entity):
        # awesome code

class InMemoryDeleteRepository(DeleteRepository):
    def delete(entity):
        # awesome code

class UserRepository(
    InsertRepository,
    UpdateRepository,
    metaclass=abc.ABCMeta
):
    pass

# I want UserRepository to have PostgresInsertRepository and PostgresUpdateRepository implementations 
# and register them in Container.

# I thought about creating a PostgresUpdateRepository, but I want to avoid it because I just call it
# example:
class PostgresUserRepository(
    UserRepository
):
    def __init__(
        self,
        postgres_insert_repository: PostgresInsertRepository,
        postgres_update_repository: PostgresUpdateRepository
    ):
        self.postgres_insert_repository = postgres_insert_repository
        self.postgres_update_repository = postgres_update_repository

    def insert(entity):
        self.postgres_insert_repository.insert(entity)

    def update(entity):
        self.postgres_update_repository.insert(entity)

class Container(containers.DeclarativeContainer):

    insert_repository = providers.Factory(
        PostgresInsertRepository
    )

    update_repository = providers.Factory(
        PostgresUpdateRepository
    )

    user_repository = providers.Factory(
        PostgresUserRepository,
        postgres_insert_repository=insert_repository,
        postgres_update_repository=update_repository,
    )

(Of course, the above code normally receives DB related values, but it is a simplified code because it deviates a little from this question.)

Do you have any good ideas?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions