How to use FactoryAggregate in a container - aka Selector provider
See original GitHub issueCan 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:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@githubkusi
The time has come.
I’ve added new
Selector
provider. It covers the case from this issue. It also supportsSingleton
or any other provider.Code from the issue now can look like:
Selector
provider is available since version3.19
. It’s already on PyPI.Selector
provider.Thank you for bringing up the issue.
Roman
That happens because
FactoryAggregate
provider can only acceptFactory
providers. This is a really dirty hack, but that’s how this could work if you need singletons: