4 ms·
DI containers don't stop circular dependencies. It's not a massive advantage.
by gonzo41 5y ago
DI containers don't stop circular dependencies. It's not a massive advantage.
- mayank 5y agoYes, but modern DI (like Dagger for Java) can detect cyclic dependencies at compile time and break the build.
- gonzo41 5y agoI know, but just thinking though what you're doing and building a program for right now, rather than building for a future that may never happen is probably going to deliver more success. I like constructor based dependency injection personally. It's simple and I don't really see a stack of value obsessing about the possibility of code reuse.
- contravariant 5y agoI can detect a cyclic reference before I even finish writing the code, what's the point of letting the compiler figure it out?
- mayank 5y ago> I can detect a cyclic reference before I even finish writing the code, what's the point of letting the compiler figure it out? The compiler generally has better attention to detail and the ability to deal with larger object graphs than the typical human.
- contravariant 5y agoSure, but how are you going to write code that uses a class with a circular dependency?
- KronisLV 5y agoCuriously i've actually seen people use @Lazy in Spring projects to allow for circular dependencies, to deal with situations where the services or their dependencies aren't structured like a neat tree with leafs, but rather some interdependent graph with cycles. Honestly, in practice it worked and wasn't too bad to work with, which was interesting to behold - in practice everyone talks about how circular dependencies are bad for a variety or reasons (trying to print or process data and ending up with endless loops comes to mind), but then there just was that system that was chugging along without a care in the world.
- teh_klev 5y agoYes they can and do. The DI container in .NET core does this upfront and will thrown an exception if it detects circular references.