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

Is it an antipattern to have multiple instances of Injector (because of missing Generics support)? #218

Closed
hf-kklein opened this issue Jun 2, 2023 · 2 comments

Comments

@hf-kklein
Copy link

hf-kklein commented Jun 2, 2023

I have an application. It has multiple purposes:

  1. Doing Foo
  2. Doing Bar
  3. ... many more

There are helper classes which I'll just call A, B, ...

For doing Foo or Bar I'm having a generic super() type:

class ThingDoer(Generic[X]):
    @inject
    def __init__(a_instance: A[X], b_instance: B[X])
       ...

As of now Generics are not supported (see #175). So as a workaround I do the following:

class ThingDoer(Generic[X]):
    @inject
    def __init__(a_instance: A, b_instance: B) # <-- workaround for no generic args to A and B
       ...
 
class AFoo(A[Foo]):
    """this class has no body"""
 
class BFoo(B[Foo]):
    """this class has no body"""
 
class ABar(A[Bar]):
    """this class has no body"""
 
class BBar(B[Bar]):
    """this class has no body"""
 
def configure_foo(binder):
    binder.bind(A, AFoo)
    binder.bind(B, BFoo)
injector_foo = Injector([configure_foo])
 
 
def configure_bar(binder):
    binder.bind(A, ABar)
    binder.bind(B, BBar)
injector_bar = Injector([configure_bar])
 
foo_doer = injector_foo.get(ThingDoer)
bar_doer = injector_bar.get(ThingDoer)

Question is: Is it an antipattern to have multiple instances of Injector?
Should I instead use the inject decorator on the child classes directly?

class FooDoer(ThingDoer[Foo]):
    @inject
    def __init__(a_instance: AFoo, b_instance: BFoo)
       ...
 
class BarDoer(ThingDoer[Bar]):
    @inject
    def __init__(a_instance: ABar, b_instance: BBar)
       ...
 
my_global_injector = Injector()
 
foo_doer = inject.get(FooDoer)
bar_doer = inject.get(BarDoer)
@hf-kklein hf-kklein changed the title Is it an antipattern to have multiple instances of Injector? Is it an antipattern to have multiple instances of Injector (because of missing Generics support)? Jun 2, 2023
@davidparsson
Copy link
Collaborator

I'd say that there's no simple answer to your question, but rather that it depends on your situation. Do whatever suits you best. I've successfully used parent/child injectors for simliar cases, but there are probably cases where either of your options are better.

@jstasiak
Copy link
Collaborator

I'd say that there's no simple answer to your question, but rather that it depends on your situation. Do whatever suits you best. I've successfully used parent/child injectors for simliar cases, but there are probably cases where either of your options are better.

I second this

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

No branches or pull requests

3 participants