meadsteve / lagom

📦 Autowiring dependency injection container for python 3
https://lagom-di.readthedocs.io/en/latest/
MIT License
286 stars 15 forks source link

Autowire interface with one implementation #249

Open sidux opened 12 months ago

sidux commented 12 months ago

One feature that can reduce some boilerplate, is that we create automatically a service for each interface and point to the implementation automatically when there is only one.

For example :

class UserProvider(ABC)
class DatabaseUserProvider(UserProvider)

Don't have to write

container[UserProvider] = DatabaseUserProvider

What do you think ?

GH-maggio commented 12 months ago

Sounds like a great idea, any chance you have a rough idea how to implement this?

meadsteve commented 11 months ago

Whilst this is potentially a nice feature I'm hesitant on two fronts:

  1. It's less explicit. There's now an implicit behaviour about what gets loaded. Currently in lagom all the loading is done explicitly so this is a fairly big change.

  2. This may actually be surprisingly hard to implement in a thorough way. Python classes have a __subclasses__ property which would contain the "one implementation". However __subclasses__ only contains classes that have been loaded. So it's possible that a second subclass exists but it's not yet been loaded. This means that lagom would likely need to implement some directory scanning to find all possible classes.