lagom icon indicating copy to clipboard operation
lagom copied to clipboard

Autowire interface with one implementation

Open sidux opened this issue 2 years ago • 2 comments

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 ?

sidux avatar Nov 08 '23 15:11 sidux

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

GH-maggio avatar Nov 10 '23 18:11 GH-maggio

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.

meadsteve avatar Nov 13 '23 08:11 meadsteve