Open blaix opened 6 years ago
https://github.com/alecthomas/injector provides functionality very similar to what I'm describing, but I'm worried that it does too much, and I don't like the decorators. I want to emphasize that these are just normal parameters. In the case of a normal request, woma passes the parameters in, but any other time (e.g. in tests) you must pass them. I don't want to focus on injectors and providers and implementers and containers at all.
worth looking at how pyramid-services works. Instead of literally instantiating the type in the hint, you could register objects, or factories that return objects, for a given interface, and the injector can give you the instance for the interface used as a type hint (or by passing it to some method on a container object, ala request.find_service
in pyramid-services).
If you call the handler yourself (e.g. in tests), you are responsible for passing all arguments. When woma calls your handler (to handle a real request), it will instantiate an instance of the action's type and pass that. The action(s) can be any number of kwargs, named anything other than request and response.
To do this, I should create a new library for doing dependency injection this way. It should look something like this:
Internally, woma would use
injector
to instantiate actions. Also, kwargs should be injected arbitrarily deep. For example:This means actions can themselves depend on other actions, boundaries, etc. that can also depend on others, etc. and they will all be instantiated by the injector. The fact that they must be instantiated manually outside of a woma-handled request (e.g. in tests) will discourage a nesting level that is too deep.
Things I do not like about this approach: