reagento / dishka

Cute DI framework with agreeable API and everything you need
https://dishka.readthedocs.io
Apache License 2.0
432 stars 47 forks source link

dynamic component provider - per component consistent providing of attributes #257

Closed RonnyPfannschmidt closed 1 month ago

RonnyPfannschmidt commented 1 month ago

in a Application framework im working on i want to migrate towards using dishka to provide data of plugins

however instead of having something like

...

class PluginProvider(Provider):
   ...

plugin_providers = [PluginProvider(component=plugin_name) for plugin_name in get_availiable_plugins()]

i want to use something more like


...

class PluginProvider(DynamicComponenProvider):
  ...

plugin_provider = PluginProvider()

if necessary i can extract a more complete example to demonstrate the point

Tishka17 commented 1 month ago

You can inherit from BaseProvider and make your own login on filling lists inside it

RonnyPfannschmidt commented 1 month ago

I saw how providerwrapper is implemented and off hand its not clear to me how to propperly map the component of Depend objects

As looks to me like component is taken from the Provider unless i manually create All components in the dependencies

Using providerwrapper may be the most prachical solutikn for now

Tishka17 commented 1 month ago

Component from factory is used when it is not set explicitly in dependency. But, if you are creating new BaseProvider from existing Provider you can use with_component method on each factory to fill it. It is a bit more complicated on decorators and aliases, but not very much. I guess we can extend their API adding same method.

RonnyPfannschmidt commented 1 month ago

for my immediate needs, im under the impression, using ProviderWrapper is a more sensible approach as starting point

as there is a need to replicate all providers/dependency keys its much better to make instances per component