wireup icon indicating copy to clipboard operation
wireup copied to clipboard

Add abstract injection without importing concrete impl

Open hkuszynski opened this issue 1 year ago • 3 comments

Currently from my experience, when we are using Annotated[AbstractClass, Wire(qualifier=Any)] - we still need to import redundant Concrete Impl to scope, where @autowire is performed. If such import is not performed, user will experience wireup.errors.UnknownQualifiedServiceRequestedError

It would be nice for this library to have such feature, or at least, have an docs for this cause, to not import redundant concrete classes, but have a workaround.

hkuszynski avatar Mar 22 '24 08:03 hkuszynski

I assume your interface and implementations are on different files/modules and they're decorated with register/abstract.

The issue that might come up is that if that module is never imported, the container.register decorator call is never triggered so the container does not know about that object.

To fix this do one of the following on app startup:

Call wireup.warmup_container and pass it the list of modules containing your services. If they're all in one module simply pass the top level one.

You can also just bring them into scope by using wireup.import_util.load_module.

What framework are you using this with if you don't mind me asking?

maldoinc avatar Mar 22 '24 10:03 maldoinc

@maldoinc

Correct, i have located abstract class, and concrete impls, on different packages (nested catalogs) and decorated them with abstract and register(qualifier) accordingly.

So basically speaking, i need to call wireup.warmup_container before any factory/setup method in my code (with inject usage) is launched? Can you show an example how it would look like in case with nested subfolders?

I've managed to do some hack by naming the qualifiers as the classes, and invoke them via Wire(qualifier=Class.__name__) 😆

I am using a lot of there, but to specify a few: aio_pika, fastapi, pydantic.. and so on.

Thank you for the response and guidance.

hkuszynski avatar Mar 24 '24 16:03 hkuszynski

Yes, you'd call that on your application's entrypoint right after everything is set up.

  • What it will do is recursively import that module and everything under it so that the container.register/abstract takes place
  • Create copies of your services ahead of time (otherwise they are created on first use)

Something like this:

from wireup import container, warmup_container
from some_app import services

def cerate_app():
    app = ...
    # perform rest of set up
    # ...
    warmup_container(container, service_modules=[services])
    
    return app

Take a look at the Flask and FastAPI integration docs. You can replace the integration method call with warmup container(Integration does warmup + a few extra things depending on framework).


Additionally

You can import all your services and call container.register/abstract on each if you want to avoid the decorator altogether. See Service docs; Interface docs

from services import A, B, C
from wireup import container

container.abstract(A)
container.register(B)
container.register(C, qualifier="foo")

I do recommend doing the warmup if you have a long-lasting process such as a web api.

Let me know if that answers your question.

maldoinc avatar Mar 26 '24 13:03 maldoinc