News for hackers. Stuff that matters.
Re:You probably don't need a dependency injection framework by surgical_fire on Sunday May 25, 2025 @05:10AM (#44086573)
Dependency Injection has a fancy name that makes some developers uncomfortable, but it's really just all about making the code easier to test. Basically everything that the class depends upon has to be informed during construction.

This can be done manually, but becomes a chore super fast - and will be a very annoying thing to maintain as soon as you change the constructor of something widely used in your project to accept a new parameter.

Frameworks typically just streamline this process, and offers some flexibility at times - for example, when you happen to have different implementations of the same thing. I find it funny that people rally against those Frameworks so often.

To make things more concrete, let's say you have a method that gets the current date, and has some logic there (for example, it checks if today is EOM to do something). In Java, you could do `Instance.now()` to do this.

This will be a pain in the ass to test, you might need to test, for example a date when there's a DST change, or February 28th in a leap year, etc. With DI you can instead inject an `InstantSource` to your code, and on testing you can just mock the dependency to have a predictable date on each test.

Re:You probably don't need a dependency injection framework by ffsm8 on Sunday May 25, 2025 @05:25AM (#44086626)
You're talking from the perspective of Java, which has been designed from the ground up with dependency injection in mind.

Dependency injection is the inversion of control pattern at the heart, which is something like oxygen to a Java dev.

In other languages, these issues are solved differently. From my perspective as someone whoes day job has been roughly 60+% Java for over 10 yrs now... I think I agree with the central message of the article. Unless you're currently in Java world, you're probably better off without it.

These patterns work and will on paper reduce complexity - but if comes at the cost of a massively increased mental overhead if you actually need to address a bug that touches more then a miniscule amount of code.

/Edit: and I'd like to mention that the article actually only dislikes the frameworks, not the pattern itself

Re:You probably don't need a dependency injection framework by mattmanser on Sunday May 25, 2025 @05:36AM (#44086668)
DI wasn't around when Java (or .Net) came out. DI is a fairly new thing too, relatively speaking, like after ORMs and anonymous methods. Like after Java 7 I think. Or maybe 8? Not a Java person myself.

I know in .net, it was only really the switch to .net core where it became an integral part of the frameworks. In MVC 5 you had to add a third party DI container.

So how can it have been designed for it from the ground up?

In fact, if you're saying 10 years, that's roughly when DI became popular.

You're wrong about other languages not needing it, yes statically typed languagess need it for unit testing, but you don't seem to realize that from a practical perspective DI solves a lot of the problems around request lifetimes too. And from an architectural context it solves a lot of the problem of how to stop bad developers overly coupling their services.

Before DI people often used static methods, so you'd have a real mess of heavily interdependent services. It can still happen now but.its nowhere near as bad as the mess of programming in the 2000s.

DI helped reduce coupling and spaghetti code.

DI also forces you to 'declare' your dependencies, so it's easy to see when a class has got out of control.

Edit: I could keep on adding, but one final thing. Java and .Net are actually quite cumbersome to use DI, and Go is actually easier. Because Go has implicit interfaces, but older languages don't and it would really help reduce boiler plate DI code.

A lot of interfaces in Java/C# only exist to allow DI tow work, and are otherwise a pointless waste of time/code.

Re:You probably don't need a dependency injection framework by ollysb on Sunday May 25, 2025 @05:01PM (#44091082)
The Spring framework was released in 2003 when Java was at v1.4. From memory it wasn't the first DI framework either.
Re:You probably don't need a dependency injection framework by layer8 on Sunday May 25, 2025 @06:04PM (#44091622)
The term was coined in 2004: https://www.martinfowler.com/articles/injection.html

It’s not correct that Java was designed for it, unless you want to call class loading dependency injection. It’s merely that Java’s reflection mechanism happened to enable DI frameworks. The earlier Java concept was service locaters (also discussed in the article linked above).

Re:You probably don't need a dependency injection framework by thiht on Sunday May 25, 2025 @05:58AM (#44086758)
Or don’t use a DI framework, and DI just becomes a fancy name for "creating instances" and "passing parameters". That’s what we do in Go and there’s no way I would EVER use a DI framework again. I’d rather be unemployed than work with Spring.
Re:You probably don't need a dependency injection framework by surgical_fire on Sunday May 25, 2025 @06:26AM (#44086838)
> DI just becomes a fancy name for "creating instances" and "passing parameters".

I literally addressed this in the post you are replying to. I think your problem is reading comprehension, not dependency injection or framework usage.

Then again, understanding Design Patterns will be difficult if you can't even parse you way through a handful of paragraphs written in plain English.

I'll bother to repeat myself:

>> This can be done manually, but becomes a chore super fast - and will be a very annoying thing to maintain as soon as you change the constructor of something widely used in your project to accept a new parameter.

>> Frameworks typically just streamline this process, and offers some flexibility at times - for example, when you happen to have different implementations of the same thing. I find it funny that people rally against those Frameworks so often.

There. Maybe stripped of the surrounding context will make it easier.

> I’d rather be unemployed than work with Spring.

That's really a you problem.

Re:You probably don't need a dependency injection framework by thiht on Sunday May 25, 2025 @07:00AM (#44086966)
No need to be aggressive, I just disagree that DI frameworks streamline anything, they just make things more opaque and hard to trace.

> will be a very annoying thing to maintain as soon as you change the constructor of something widely used in your project to accept a new parameter.

That for example is just not true. You add a new parameter to inject and it breaks the injection points? Yeah that’s expected, and suitable. I want to know where my changes have any impact, that’s the point of typing things.

A lot of things deemed "more maintainable" really aren’t. Never has a DI framework made anything simpler.

Re:You probably don't need a dependency injection framework by surgical_fire on Sunday May 25, 2025 @08:08AM (#44087232)
> That for example is just not true. You add a new parameter to inject and it breaks the injection points?

Perhaps you never worked in a sufficiently large codebase?

It is very annoying when you need to add a dependency and suddenly you have to touch 50+ injection points because that thing is widely used. Been there, done that, and by God I wished I had Dagger or Spring or anything really to lend me a hand.

DI frameworks are a tool like any other. When properly used in the correct context they can be helpful.

Re:You probably don't need a dependency injection framework by culturedsystems on Sunday May 25, 2025 @05:22PM (#44091256)
"It is very annoying when you need to add a dependency and suddenly you have to touch 50+ injection points because that thing is widely used"

You don't have to update the injection points, because the injection points don't know the concrete details of what's being injected. That's literally the whole point of dependency injection.

Edited to add: Say you have a class A, and this is a dependency of classes B, C, etc. Using dependency injection, classes B and C are passed instances of A, they don't construct it themselves. So if you add a dependency to A, you have to change the place that constructs A, of course, but you don't have to change B and C, because they have nothing to do with the construction of A.

Re:You probably don't need a dependency injection framework by therealdrag0 on Sunday May 25, 2025 @07:41PM (#44092369)
Ya very confused by this. Either the change is to the constructor of the object being injected, in which case there is no difference either way, or the change is to the constructor receiving the injection, in which case there’s no difference either way.
Re:You probably don't need a dependency injection framework by cornel_io on Sunday May 25, 2025 @07:15PM (#44092186)
I think you're being downvoted because you're agreeing with the post you're quoting, but arguing as if they're wrong: the example in question was there to show how DI can be useful, so there's nothing to argue against.
Re:You probably don't need a dependency injection framework by layer8 on Sunday May 25, 2025 @05:53PM (#44091539)
This is the only reason why DI frameworks exist. However, this issue can be largely avoided by working with configuration objects (parameter objects [0]) from the start, that get passed around. Then you only need to apply the change in a small number of parameter-object classes, if not just a single one.

[0] https://wiki.c2.com/?ParameterObject

Re:You probably don't need a dependency injection framework by jcelerier on Monday May 26, 2025 @09:14AM (#44097143)
Eh, having built a whole codebase around these configuration objects I really regret not going for more traditional DI IoC container. It's thousands over thousands of additional parameters passed all over the place when creating objects just for the sake of saving five minutes of explanation to newcomers.
Re:You probably don't need a dependency injection framework by baq on Sunday May 25, 2025 @04:34PM (#44090861)
The one thing DI frameworks unarguably and decisively solve by design (if accidentally, but it doesn’t matter) is control over static initialization. I’d say you haven’t truly lived if your C++ didn’t crash before calling main(), but it helps in large JS and Python projects just the same.
Re:You probably don't need a dependency injection framework by layer8 on Sunday May 25, 2025 @05:48PM (#44091502)
How do they solve that? If constructors require certain parameters, someone has to pass them. If it’s not a top-level object, the instantiating code will pass it and have a corresponding constructor or method parameter itself. At the top level, main() or equivalent will pass the parameter. Where is the problem?
Re:You probably don't need a dependency injection framework by baq on Monday May 26, 2025 @05:48AM (#44095711)
Exactly, there is no problem when you do it this way and DI frameworks force you to.

The problem when you don't do it this way is when you depend on order of initialization in a way you are not aware of until it breaks, and it breaks in all kinds of interesting ways.

Re:You probably don't need a dependency injection framework by babyent on Sunday May 25, 2025 @05:04PM (#44091114)
Chill dawg
Re:You probably don't need a dependency injection framework by tomhow on Monday May 26, 2025 @10:29AM (#44097764)
Be kind. Don't be snarky. Converse curiously; don't cross-examine. Edit out swipes.

When disagreeing, please reply to the argument instead of calling names. "That is idiotic; 1 + 1 is 2, not 3" can be shortened to "1 + 1 is 2, not 3."

Please don't fulminate. Please don't sneer, including at the rest of the community.

https://news.ycombinator.com/newsguidelines.html

Re:You probably don't need a dependency injection framework by Gibbon1 on Sunday May 25, 2025 @09:14PM (#44092952)
I'm a small brained primate but when I get down to what dependency injection is doing it's like my firmware written in C setting a couple of function pointers in a radio handler's struct to tell it which SPI bus and DIO's to use. Which seems trivially okay.
Re:You probably don't need a dependency injection framework by jen20 on Monday May 26, 2025 @02:42AM (#44094634)
Maybe you could even use a different handler in tests!? Shocking that you can do this without using an IoC container and a thousand lines of YAML!

/sarcasm (in case anyone doubted it).

Re:You probably don't need a dependency injection framework by IshKebab on Sunday May 25, 2025 @11:23AM (#44088472)
> Dependency Injection has a fancy name that makes some developers uncomfortable, but it's really just all about making the code easier to test.

It's not just a fancy name. I'd argue it's a confusing name. The "$25 name for a 5c concept" quote is brilliant. The name makes it sound like it's some super complicated thing that is difficult to learn, which makes it harder to understand. I would say "dynamic programming" suffers the same problem. Maybe "monads".

How about we rename it? "Generic dependencies" or "Non hard-coded dependencies" or even "dependency parameters"?

Re:You probably don't need a dependency injection framework by kevmo314 on Sunday May 25, 2025 @04:24PM (#44090772)
“Global variables that can pass code review”
Re:You probably don't need a dependency injection framework by layer8 on Sunday May 25, 2025 @05:59PM (#44091588)
The name is confusing because originally [0] it was exclusively about dynamic injection by frameworks. Somehow it morphed into just meaning suitable constructor parameters, at least in some language communities.

I like “dependency parameters”. Dependencies in that sense are usually what is called service objects, so “service parameters” might be even clearer.

[0] https://www.martinfowler.com/articles/injection.html#FormsOf...

Re:You probably don't need a dependency injection framework by akoboldfrying on Sunday May 25, 2025 @10:01PM (#44093253)
I third "dependency parameters". It concisely describes what the actual thing is, and isn't intimidating.

And yes, even though some languages/frameworks allow deps to be "passed in" via mechanisms that aren't technically parameters (like member variables that are just somehow magically initialised due to annotations), doing that only obfuscates control flow and should be avoided IMHO.

Re:You probably don't need a dependency injection framework by ulrikrasmussen on Monday May 26, 2025 @05:32AM (#44095613)
I think the "injection" is referring to a particular style of frameworks which rely on breaking language abstractions and modifying otherwise private fields to provide dependencies at runtime. I really, really, REALLY dislike that approach and therefore also dislike the name "dependency injection".

Why not call it "dependency resolution"? The only problem frameworks solve is to connect a provider of thing X with all users of thing X, possibly repeating this process until all dependencies have been resolved. It also makes it more clear that this process is not about initialization and lifecycling, it is only about connecting interfaces to implementations and instantiating them.

Edit: The only DI framework I have used and actually kind of like is Dagger 2, which resolves all dependencies at compile time. It also allows a style of use where implementation code does not have to know about the framework at all - all dependency resolution modules can be written separately.

All other runtime DI frameworks I have used I have hated with a passion because they add so much cognitive overhead by imposing complex lifecycle constraints. Your objects are not initialized when the constructor has finished running because you have to somehow wait for the DI framework to diddle the bits, and good luck debugging when this doesn't work as expected.

Re:You probably don't need a dependency injection framework by kazinator on Sunday May 25, 2025 @11:36AM (#44088581)
It's not just about testing. When any code constructs its own object, and that object is actually an abstraction of which we have many implementations, that code becomes stupidly inflexible.

For instance, some code which prints stuff, but doesn't take the output stream as a parameter, instead hard-coding to a standard output stream.

That leaves fewer options for testing also, as a secondary problem.

Re:You probably don't need a dependency injection framework by layer8 on Sunday May 25, 2025 @06:02PM (#44091611)
That’s just parameterization, though. It’s overkill to call every parameter (or even most parameters) a “dependency”.
Re:You probably don't need a dependency injection framework by kazinator on Monday May 26, 2025 @02:10PM (#44099993)
So even "dependency parametrization" would be better than "injection".

Since what we want to parametrize are objects held by composition, maybe "composition parametrization".

"To promote better flexibility and code reuse and to make testing easier, parametrize object composition."

Re:You probably don't need a dependency injection framework by loevborg on Sunday May 25, 2025 @04:54PM (#44091014)
Why is it a pain to inject dependencies manually? I think this is because people assume for some reason that a class isn't allowed to instantiate its own dependencies.

If you lift that assumption, the problem kind of goes away. James Shore calls this Parameterless Instantiation: https://www.jamesshore.com/v2/projects/nullables/testing-wit...

It means that in most cases, you just call a static factory method like create() rather than the constructor. This method will default to using Instance.now() but also gives you a way to provide your own now() for tests (or other non-standard situations).

At the top of the call stack, you call App.create() and boom - you have a whole tree of dependencies.

Re:You probably don't need a dependency injection framework by bluGill on Sunday May 25, 2025 @08:05PM (#44092513)
many of the things I dependeon are shared services where there should only be one instance. Singleton means a global variable. a di framework lets me have on without a global - meaning if I need a second service I can do it with just a change to how the di works.

there is no right answer of course. Time should be a global so that all timers/clocks advance in lock step. I hav a complex fake time system that allows my tests to advance minutes at a time without waiting on the wall clock. (If you deal with relativity this may not work - for everyone else I encourage it)

Re:You probably don't need a dependency injection framework by pdpi on Sunday May 25, 2025 @05:17PM (#44091220)
If the class instantiates its own dependencies then, by definition, you're not injecting those dependencies, so you're not doing dependency injection at all!
Re:You probably don't need a dependency injection framework by loevborg on Sunday May 25, 2025 @05:31PM (#44091352)
That seems too dogmatic. Does the fact that Foo has a static function that knows how to create its dependencies disqualify the class from being a case of DI?

    class Foo {
      private nowFn: () => Date;

      constructor(nowFn: () => Date) {
        this.nowFn = nowFn;
      }
    
      doSomething() {
        console.log(this.nowFn());
      }

      static create(opts: { nowFn?: () => Date } = {}) {
        return new Foo(opts.nowFn ?? (() => new Date()));
      }
    }
Re:You probably don't need a dependency injection framework by physPop on Sunday May 25, 2025 @06:15PM (#44091704)
Yes because injecting nowFn is trivial and not a case for DI. Consider a database handle, or network socket, or http response payload. Clearly each class shouldn't be making its own version of those.
Re:You probably don't need a dependency injection framework by cyanydeez on Sunday May 25, 2025 @07:59PM (#44092481)
You're nitpicking for no good reason. You can create global handles to each of those items, and let them instantiate with the class or override them with a create function.

Dependency injection boils down the question of whether or not you can dynamically change a dependency at runtime.

Re:You probably don't need a dependency injection framework by loevborg on Monday May 26, 2025 @05:16AM (#44095515)
You're right, stateful dependencies like DB handles need to be passed in manually, and that's a bit of extra legwork you need to do.
Re:You probably don't need a dependency injection framework by Charon77 on Wednesday May 28, 2025 @11:40AM (#44117208)
Just use a static variable to the DB instance / pool, maybe using singletons, so everyone needing it have access.
Re:You probably don't need a dependency injection framework by ptx on Sunday May 25, 2025 @06:17PM (#44091717)
> James Shore calls this Parameterless Instantiation

Mark Seemann calls it the Constrained Construction anti-pattern: https://blog.ploeh.dk/2011/04/27/Providerisnotapattern/#4c7b...

Re:You probably don't need a dependency injection framework by loevborg on Sunday May 25, 2025 @05:22PM (#44091260)
In other words, despite all the noise to the contrary, hard-coded dependencies are fine.

James explains this a lot better than I can: https://www.jamesshore.com/v2/blog/2023/the-problem-with-dep...

Re:You probably don't need a dependency injection framework by almostdeadguy on Sunday May 25, 2025 @06:43PM (#44091927)
It often is not enough. Singletons frequently need to be provided to things (a connection pool, etc.).
Re:You probably don't need a dependency injection framework by lgas on Sunday May 25, 2025 @08:24PM (#44092652)
Maybe I'm misunderstanding what you're saying but a connection pool seems like almost a canonical example of something that shouldn't be a singleton. You might want connection pools that connect to different databases or to the same database in read-only vs. read/write mode, etc.
Re:You probably don't need a dependency injection framework by stickfigure on Sunday May 25, 2025 @10:54PM (#44093527)
Then these different pools can be separate singletons. You still don't want to instantiate multiple identical pools.

You can use the type system to your advantage. Cut a new type and inject a ReadOnlyDataSource or a SecondDatabaseDataSource or whatnot. Figure out what should only have one instance in your app, wrap a type around it, put it in the singleton scope, and inject that.

Re:You probably don't need a dependency injection framework by MrJohz on Monday May 26, 2025 @12:18AM (#44093932)
I think the point is that you can already do all of that with hand-wired dependency injection. I wrote about an example of that a couple of years ago here: https://jonathan-frere.com/posts/how-i-do-dependency-injecti...

This has the advantage that you don't need an extra framework/dependency to handle DI, and it means that dependencies are usually much easier to trace (because you've literally got all the code in your project, no metaprogramming or reflection required). There are limits to this style of DI, but in practice I've not reached those limits yet, and I suspect if you do reach those limits, your DI is just too complicated in the first place.

Re:You probably don't need a dependency injection framework by almostdeadguy on Monday May 26, 2025 @09:26AM (#44097237)
I think most people using these frameworks are aware that DI is just automated instantiation. If your program has a limited number of ways of composing instantiations, it may not be useful to you. The amount of ceremony reduced may not be worth the overhead.
Re:You probably don't need a dependency injection framework by stickfigure on Monday May 26, 2025 @10:51AM (#44097994)
This conversation repeats itself ad infinitum around DI, ORMs, caching, security, logging, validation, etc, etc, etc... no, you don't need a framework. You can write your own. There are three common outcomes of this:

* Your framework gets so complicated that someone rips it out and replaces it with one of the standard frameworks.

* Your framework gets so complicated that it turns into a popular project in its own right.

* Your app dies and your custom framework dies with it.

The third one is the most common.

Re:You probably don't need a dependency injection framework by MrJohz on Monday May 26, 2025 @03:48PM (#44100966)
I'm not suggesting a custom framework here, I'm suggesting no DI framework at all. No reflection, no extra configuration, no nothing, just composing classes manually using the normal structure of the language.

At some point this stops working, I agree — this isn't necessarily an infinitely scalable solution. At that point, switching to (1) is usually a fairly simple endeavour because you're already using DI, you just need to wire it together differently. But I've been surprised at the number of cases where just going without a framework altogether has been completely sufficient, and has been successful for far longer than I'd have originally expected.

Re:You probably don't need a dependency injection framework by almostdeadguy on Monday May 26, 2025 @09:05AM (#44097088)
I meant "singleton" in the sense of a single value for a type shared by anything that requires one, i.e. a Guice singleton ( https://github.com/google/guice/wiki/scopes#singleton ) not a value in global scope. Or maybe a single value by type with an annotation, the salient point is that there are values in a program that must be shared for correctness. Parameterless constructors prohibit you from using these (unless you have global variables).
Re:You probably don't need a dependency injection framework by hayst4ck on Sunday May 25, 2025 @09:40PM (#44093116)
If you construct an object once, then what is the difference between that and a singleton?

The answer is scope. Singletons exist explicitly for the purpose of violating scoping for the convenience of not having to thread that dependency through constructors.

https://testing.googleblog.com/2008/08/root-cause-of-singlet...

Re:You probably don't need a dependency injection framework by almostdeadguy on Monday May 26, 2025 @09:00AM (#44097060)
Calling these "singletons" maybe created confusion here, I'm more talking about singleton in the sense of a value that is created once and shared (i.e. injecting a value as a "Singleton" via something like Guice). I'm not arguing that you need to have values in global scope, I'm arguing that parameterless constructors prevent you from using shared values (actually _unless_ you have singletons in global scope).
Re:You probably don't need a dependency injection framework by layer8 on Sunday May 25, 2025 @05:42PM (#44091456)
> Frameworks typically just streamline this process, and offers some flexibility at times - for example, when you happen to have different implementations of the same thing.

The very purpose of DI is to allow using a different implementation of the same thing in the first place. You shouldn’t need a framework to achieve that. And my personal experience happens to match that.

Re:You probably don't need a dependency injection framework by shadowgovt on Sunday May 25, 2025 @05:51PM (#44091521)
I frequently find DI pattern to show up in Java... But I also frequently find that Java gives me all the handcuffs of systems languages with few of the benefits of more flexible duck-typing languages.

If you can't monkey-patch the getDate function with a mock in a testing context because your language won't allow it, that's a language smell, not a pattern smell.

Re:You probably don't need a dependency injection framework by akoboldfrying on Sunday May 25, 2025 @10:15PM (#44093330)
> If you can't monkey-patch the getDate function with a mock in a testing context because your language won't allow it, that's a language smell, not a pattern smell.

Not so fast. Constraints like "no monkeypatching allowed" are part of what make it possible to reason about code at an abstract level, i.e., without having to understand in detail every control path that could possibly have run before. Allowing monkeypatching at the language level means discarding that useful reasoning tool.

I'm not saying that "no monkeypatching allowed" is always ideal, but it is a tradeoff.

(Consider why so many languages have something like a "const" modifier, which purely restricts what you can do with the object in question. The restriction reduces what you can do with it, but increases what you know about it.)

Re:You probably don't need a dependency injection framework by shadowgovt on Monday May 26, 2025 @12:22AM (#44093945)
In practice, we accomplish that by not monkeypatching the production code.

But for unit testing? Go nuts.

Re:You probably don't need a dependency injection framework by akoboldfrying on Monday May 26, 2025 @01:57AM (#44094393)
Sure, a policy like that helps, but relying on programmer discipline like this only scales so far. In a large enough code base, if something can be done, someone will have done it somewhere.

(If you have linter rules or static analysis that can detect monkeypatching and runs on every commit (or push to main, or whatever), you're good.)

Re:You probably don't need a dependency injection framework by arjvik on Monday May 26, 2025 @12:20AM (#44093940)
You CAN in fact monkeypatch getDate - look at a Mockito add-on known as PowerMockito! While it's impossible to mock it out in the normal JVM "happy path," the JVM is powerful enough to let you mess with classloading and edit the bytecode at load-time to mock out even system classes.

(Disclaimer: have not used PowerMockito in ages, am not confident it works with the new module system.)

Re:You probably don't need a dependency injection framework by surgical_fire on Monday May 26, 2025 @04:30AM (#44095262)
> If you can't monkey-patch the getDate function with a mock in a testing context because your language won't allow it, that's a language smell, not a pattern smell.

Of course you can do it in Java. But it is widely considered poor practice, for good reason, and is generally avoided.

Re:You probably don't need a dependency injection framework by shadowgovt on Tuesday May 27, 2025 @12:42PM (#44108561)
Interesting. Why is it considered poor practice to monkey-patch getDate for testing purposes in Java?
Re:You probably don't need a dependency injection framework by zzo38computer on Monday May 26, 2025 @12:31AM (#44093986)
> but it's really just all about making the code easier to test. Basically everything that the class depends upon has to be informed during construction.

It is useful for more than testing (although, depending on the kind of tests being made, it might not always be useful for all kind of tests). It also allows you to avoid a program having too many dependencies that you might not need (although this can also cause a problem, it could perhaps be avoided by providing optional dependencies, and macros (or whatever other facility is appropriate in the programming language you are using) to use them), and allows more easily for the caller to specify customized methods for some things (which is useful in many programs, e.g. if you want customized X.509 certificate validation in a program, or customized handling of displaying/requesting/manipulation of text, or use of a virtual file system).

In a C program, you can use a FILE object for I/O. Instead of using fopen or standard I/O, a library could accept a FILE object that you had previously provided, which might or might not be an actual file, so it does not need to deal with file names.

> This will be a pain in the ass to test, you might need to test, for example a date when there's a DST change, or February 28th in a leap year, etc.

I think that better operating system design with capability-based security would help with this and other problems, although having dependency injection can also be helpful for other purposes too.

Capability-based security is useful for many things. Not only it helps with testing, but also helps to work around a problem if a program does not work properly on a leap year, you can tell that specific program that the current date is actually a different date, and it can also be used for security, etc. (With my idea, it also allows a program to execute in a deterministic way, which also helps with testing and other things, including resist fingerprinting.)

Re:You probably don't need a dependency injection framework by jen20 on Monday May 26, 2025 @02:38AM (#44094613)
If it becomes a chore to instantiate your dependency tree with `new` or whatever in the root of your app, it's a good indication that the dependency tree _is too complex_. You _should_ feel the pain of that, to align incentives for simplification.

Using an IoC container is endemic in the Java ecosystem, but unheard of in the Go ecosystem - and it's not hard to see which of them favours simplicity!

Re:You probably don't need a dependency injection framework by surgical_fire on Monday May 26, 2025 @04:23AM (#44095229)
> instantiate your dependency tree with `new` or whatever in the root of your app

Then you are doing dependency injection, just replacing the benefits of a proper framework by instantiating everything at once the root of your app.

Whatever floats your boat, I guess. Thankfully I don't need to work on your code.

Re:You probably don't need a dependency injection framework by jen20 on Monday May 26, 2025 @07:22AM (#44096290)
Indeed, you get the benefits of dependency injection, without the complexity and shit show of a truth-obscuring framework.

I think we can both agree that it’s good we don’t need to work with each other though.

Re:You probably don't need a dependency injection framework by surgical_fire on Monday May 26, 2025 @11:15AM (#44098267)
> truth-obscuring framework.

That you think that a DI framework obscures anything is just a developer smell.

Having worked with multiple of those, it was always pretty clear what was being instantiated and how.

Re:You probably don't need a dependency injection framework by jen20 on Monday May 26, 2025 @11:55AM (#44098595)
Well in that case, every Java DI framework I’ve encountered has that smell. Spring, Guice, all of them. Same in C#. Perhaps it’s an enterprise (in the pejorative) thing.

It is interesting to note that these frameworks are effectively non-existent in Go, Rust, Python - even Typescript, and no one complains you can’t build “real things” in any of those languages.

Re:You probably don't need a dependency injection framework by JyB on Monday May 26, 2025 @05:33PM (#44101883)
Regarding 'frameworks'. Golang already ships with a framework natively because of the design of the language. Therefore the point is moot in that specific context. Hence the post.