question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use FactoryAggregate in a container - aka Selector provider

See original GitHub issue

Can I use FactoryAggregate in a DeclarativeContainer, where I choose the proper factory via config? The following code does not work

from dependency_injector import containers, providers


class A(object):
    name = "A"


class B:
    name = "B"


class C:
    def __init__(self, a_or_b):
        print(a_or_b.name)


class IoC(containers.DeclarativeContainer):
    config = providers.Configuration('config')
    aggregate_factory = providers.FactoryAggregate(a=providers.Factory(A), b=providers.Factory(B))
    # c_factory = providers.Factory(C, aggregate_factory('a')) # this works, but ignores the config
    c_factory = providers.Factory(C, aggregate_factory(config.a_or_b)) # this fails


def main():
    container = IoC(config={'a_or_b': 'a'})
    c = container.c_factory()


main()

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rmk135commented, Jun 29, 2020

@githubkusi

The time has come.

I’ve added new Selector provider. It covers the case from this issue. It also supports Singleton or any other provider.

Code from the issue now can look like:

from dependency_injector import containers, providers


class IoC(containers.DeclarativeContainer):
    config = providers.Configuration('config')
    factory_selector = providers.Selector(config.a_or_b, a=providers.Singleton(A), b=providers.Singleton(B))
    c_factory = providers.Factory(C, factory_selector)


def main():
    container = IoC(config={'a_or_b': 'a'})
    c = container.c_factory()


if __name__ == '__main__':
    main()
  • Selector provider is available since version 3.19. It’s already on PyPI.
  • Here is a doc page for - Selector provider.

Thank you for bringing up the issue.

Roman

0reactions
rmk135commented, Feb 6, 2020

That happens because FactoryAggregate provider can only accept Factory providers. This is a really dirty hack, but that’s how this could work if you need singletons:

class IoC(containers.DeclarativeContainer):
    config = providers.Configuration('config')
    aggregate_factory = providers.FactoryAggregate(
        a=providers.Factory(providers.Singleton(A)),
        b=providers.Factory(providers.Singleton(B)),
    )
    c_factory = providers.Factory(C, FactoryAggregateSelector(aggregate_factory, config.a_or_b))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Aggregate provider - Dependency Injector
Aggregate provider aggregates other providers. This page demonstrates how to implement the polymorphism and increase the flexibility of your application ...
Read more >
Flutter - The use of Selector in Provider package - Samderlust
This is done by the selector function will compare the previous value and the new value whenever the provider notifies something. If the ......
Read more >
DepenDency InjectIon wIth UnIty - Microsoft
Factories, Service Locators, and Dependency Injection ... Adding Unity to Your Application ... Configuring the Unity Container to Support Interception.
Read more >
Hibernate ORM 6.0.2.Final User Guide - Red Hat on GitHub
As a Jakarta Persistence provider, Hibernate implements the Java ... Hibernate supports defining the storage to use for time zone information for individual ......
Read more >
Spring for Apache Kafka
The aggregate of partitions currently assigned to this container's child ... V2 (aka BETA ), the only supported mode, it is no longer...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found