Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add abstract injection without importing concrete impl #19

Open
hkuszynski opened this issue Mar 22, 2024 · 3 comments
Open

Add abstract injection without importing concrete impl #19

hkuszynski opened this issue Mar 22, 2024 · 3 comments
Labels
question Further information is requested

Comments

@hkuszynski
Copy link

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.

@maldoinc
Copy link
Owner

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 maldoinc added the question Further information is requested label Mar 22, 2024
@hkuszynski
Copy link
Author

@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.

@maldoinc
Copy link
Owner

maldoinc commented Mar 26, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants