picobox
picobox copied to clipboard
Add `picobox.ext.enterprise` extension
One thing I regret about picobox
is that it uses non-conventional names for methods (e.g. pass_()
, not inject
) and classes (e.g. Box
, not Container
). I'm afraid it might be a too late to change the names.
That's why it's worth considering introducing some picobox.ext.enterprise
extension that can expose better names.
How can it look like?
import picobox.ext.enterprise as di
import requests
@di.inject("conf")
@di.inject("requests", as_="session")
def get_resource(uri, session, conf):
return session.get(conf["base_uri"] + uri)
container = di.Container()
container.add("conf", {"base_uri": "http://example.com"})
container.add("requests", factory=requests.Session, scope=picobox.threadlocal)
with di.use(container):
get_resource("/resource", requests.Session(), {})
get_resource("/resource", requests.Session())
get_resource("/resource")
Alternatively, the new interface can be developed in-house while deprecating the old one, which will delegate its calls to the new interface. :thinking: This approach will result in much more readable application code, though the project name will eventually lose its meaning.