Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Greetings,

I have some nested containers for a web server in the script below. I want to override the AppServices.*_baseclass object providers from within my main script, but I'm having trouble. It took me several hours to understand the behavior of these providers, and now I don't know where to turn.

`
class AppDatabase(containers.DeclarativeContainer):
pass

class AppServices(containers.DeclarativeContainer):
    database = providers.DependenciesContainer()
    log = providers.Callable()
    err = providers.Callable()

    # Base class providers that I eventually want to override
    user_baseclass = providers.Object(UserService)
    doc_baseclass = providers.Object(DocService)
    auth_baseclass = providers.Object(AuthService)
    cleanup_baseclass = providers.Object(CleanupService)

    user = providers.Singleton(
        user_baseclass.provided,
        db_factory = database.internal.provider,
        log = log,
        err = err
    )

    doc = providers.Singleton(
        doc_baseclass.provided,
        db_factory = database.internal.provider,
        log = log,
        err = err
    )

    auth = providers.Singleton(
        auth_baseclass.provided,
       db_factory = database.internal.provider,
       log = log,
        err = err
    )

    cleanup = providers.Singleton(
        cleanup_baseclass(),
        db_factory = database.internal.provider,
        log = log,
        err = err
    )

class AppHandler(containers.DeclarativeContainer):
    database = providers.DependenciesContainer()
    log = providers.Callable()
    err = providers.Callable()

    # Base handler class
    handler_baseclass = providers.Object(Handler)

    services = providers.Container(
        AppServices,
        database = database,
        log = log,
        err = err
    )

    # Handler with injected objects
    wired_class = providers.Singleton(
        handler_factory,
        handlerclass = handler_baseclass,
        log = log,
        err = err,
        userservice = services.user,
        docservice = services.doc,
        authservice = services.auth
    )


class App(containers.DeclarativeContainer):
    log = providers.Callable(log)
    err = providers.Callable(err)

    database = providers.Container(
        AppDatabase
    )

    handler = providers.Container(
        AppHandler,
        database = database,
        log = log.provider,
        err = err.provider
    )

`

Here's what I've done so far.

At first, I was inserting the baseclasses into the service singletons like how you see for the Cleanup service: by calling the provider which then returns the class. I was doing this for all the services. This worked because it correctly gives a type to the singleton provider.

With this setup, though, my overrides are completely ignored no matter how I write my main script. Maybe someone could explain this to me?

Anyway, I changed the user/auth/doc service singletons to use baseclass .provided and my overrides finally work, but now the singletons are not instances, but classes, and thus I run into immediate issues when I want to reference self. How can I get these service singletons to give me an instance of whatever the baseclass provider provides?

Hope this makes sense. Thanks for any help!!!!

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.