3 ms·
DI to swap out an implementation at runtime is an extreme edge case, though "runtime" may mean "the app can determine at startup which implementation makes sens
by Xylakant 14y ago
DI to swap out an implementation at runtime is an extreme edge case, though "runtime" may mean "the app can determine at startup which implementation makes sense". If you want to have a look at a nice usage of DI, peek into the source of Elasticsearch. You can swap out pretty much any relevant part of the implementation. You can for example change the HTTP acceptor to provide authentication [1] or add SSL [2] So there are cases where changing the implementation makes sense.
[1] https://github.com/Asquera/elasticsearch-http-basic https://github.com/Asquera/elasticsearch-http-basic
[2] https://github.com/Asquera/elasticsearch-ssl-transport-plugin https://github.com/Asquera/elasticsearch-ssl-transport-plugi...
- wyuenho 14y agoFor which any simple configuration mechanism will do, and as long as you stick with coding to the interface, the problem is solved. So while technically this is called DI in Java, it's simply code taken as granted in many dynamic languages because of duck-typing. So yes, this is totally valid use case, but one with no need for a DI container in dynamic languages to solve.
- thelawal 14y agoFirst a DI container is not a necessary to do DI in any of the discussed languages. This is shown by the fact that you describe using duck-typing as method of doing DI in Ruby.