@hynek I like what I see, can it layer contexts/disposal/dependency?
We have a little monster in pytest for managing fixture's
@ossronny yes, they’re much better.
The container is an `with closing(container)` context manage and the factories are pytest-style generators. MUCH more ergonomic in practice.
@hynek all of the frameworks model their needs as they grow, the results are molded by fate
I'd like to get to a place where collaboration gets us out of that trap
@ossronny I’ve been using this for years, so this is not a first attempt to solve a problem, it’s _my_ trade-off at comprehensibility vs functionality.
It always depends what you compare it to. I compare it to the boilerplatey get_x (Flask) and add_request_method (Pyramid) patterns. Got it working with FastAPI too, which gets super verbose with its own DI once you have more than 2 dependencies.
I think if the present audience is really that small, it's okay to let the package speak for itself for now. You will continue to extract value from it personally until the audience grows large enough for it to be worth proper thorough communication.
If the target audience is growing, you'll get many more chances at first impressions even a year down the road than today, and maybe the API will have hardened a bit by then and it'll also be easier to communicate.
@hynek ah, yes, so it appears I was familiar with the idea from my previous job, but not the name.
Let me rephrase to see if I've understood:
The term "dependency injection" is specifically about your code getting its dependency as input arguments. Service locator is a different mechanism, that shares a common goal.
Aside from that, hexagonal architecture cares more about where you draw your lines, what you call your components and interfaces.
@hynek so to put that together in a sentence:
My previous job was using a hexagonal architecture, with different components for the http views, the business logic, the database access, and the network call to other services.
All of this was tied together with dependency injection (implemented is scala as argument to classes, with the glue being one big main that instances everything in order).
Does that sound coherent with what you were explaining in the glossary?
@georges yep sounds mostly right to me! hexagonal is mostly about thinking more clearly about components and less about “layers” (each component can have arbitrary layers, too).
and the diff between injection and locator is literally just the acquisition mechanism with upsides and downsides on both side. it’s explained a bit deeper in https://svcs.hynek.me/en/latest/why.html#is-this-dependency-injection – I should probably transplant some of the text
@hynek thanks, things are now way more clear in my head than they were this morning :)
It also looks like on that topic I should resume reading Architecture Patterns with Python. (And wrap my head around how to use sqlalchemy properly in this context)
@hynek FWIW, as far as I can tell this is pretty cool. If I ever have to use Flask again, I'll insist on using this, and if I write something using another framework, I'll at least spend 30m thinking whether I should write and contribute an svcs.<framework> plugin.
I'm...a pretty easy sell? It's a little bit like the Pyramid registry (which I love) and a little bit like Twisted Componentized (also awesome), but palatable to others.
@hynek Dependency injection vs service locator
I didn’t have that distinction in mind
In Python i felt most Of the DI libraries were too complex, with type annotation inference and what not.
I ended up using punq, which is actually service locator if I follow these definitions.
It looks like svcs is essentially punq plus generator definition & cleanup functionality?
@hynek I was wondering if you'd be solving the worst part, but I think "The main downside is that it's impossible to verify whether all required dependencies have been configured without running the code." answers my question there.
That's always the tough thing; how to be sure your various environments provide the same necessary set of capabilities when not every code path is going to reach those service discovery steps. Even 100% test coverage doesn't save you :(
@tintvrtkovic @hynek honestly, after a decade or more of professional focus on availability, this is largely why I avoid late-binding dependencies.
That's not to say it's automatically bad; it is, as so many things are, a tradeoff between developer ergonomics, architecture, stability, startup time, etc, etc... this definitely fills a need, and I can see why someone would use it. I’d probably use it depending on what I was building.