Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Should have a best practice example for conditional injection. #24

Open
yozo1984 opened this issue Aug 12, 2013 · 3 comments
Open

Should have a best practice example for conditional injection. #24

yozo1984 opened this issue Aug 12, 2013 · 3 comments

Comments

@yozo1984
Copy link
Contributor

For something like:

@Singleton
def provideProdConfiguration():
    return something

@Singleton
def provideTestConfiguration():
    return something

def configure():
  if prod:
    Injectable(provideProdConfiguration)
  else:
    Injectable(provideTestConfiguration)
@wesalvaro
Copy link
Contributor

I like it.

@wesalvaro
Copy link
Contributor

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)

@wesalvaro
Copy link
Contributor

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)

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

No branches or pull requests

2 participants