google/dpy

Should have a best practice example for conditional injection.

Opened this issue · 3 comments

For something like:

@Singleton
def provideProdConfiguration():
    return something

@Singleton
def provideTestConfiguration():
    return something

def configure():
  if prod:
    Injectable(provideProdConfiguration)
  else:
    Injectable(provideTestConfiguration)

I like it.

Similarly:

# Module A
@Singleton
class DefaultClient(object): pass

def SetClient(client=DefaultClient):
  Injectable.named('my_client')(client)

# Module B
@Singleton
class OtherClient(object): pass

def main():
  module_a.SetClient(OtherClient)

I think something like this (shadowing with Scope) is still in the running:

@Injectable.named('my_client')
@Singleton
class DefaultClient(object): pass

# via injecting it:

@Injectable
@Singleton
class OtherClient(object): pass

@Scope
@Inject
def main(OtherClient=IN):
  Injectable.value(my_client=OtherClient)

# or without injecting it:

@Singleton
class OtherClient(object): pass

@Scope
def main():
  Injectable.named('my_client')(OtherClient)