4 ms·
I don't get dependency injection. I come from languages that I am told don't have it. But when I read the Wikipedia page for DI, I get: "a dependent consumer
by nailer 13y ago
I don't get dependency injection. I come from languages that I am told don't have it. But when I read the Wikipedia page for DI, I get:
"a dependent consumer
a declaration of a component's dependencies, defined as interface contracts
an injector (sometimes referred to as a provider or container) that creates instances of classes that implement a given dependency interface on request."
Wait, I still have to declare dependencies, just like the import statement in python, require.js in JS, etc. which are apparently not 'dependency injection' systems? And this just adds, er more code?
That same wikipedia page mentions dynamically loaded dependencies while also mentioning 'a declaration of a components dependencies' which sure doesn't sound dynamic to me.
Does anyone care to provide a better explanation of what dependency injection gives me over the existing common dependency mechanisms?
- arethuza 13y agoIn a stackoverflow thread I found a reference to this classic: '"Dependency Injection" is a 25-dollar term for a 5-cent concept.' http://www.jamesshore.com/Blog/Dependency-Injection-Demystified.html http://www.jamesshore.com/Blog/Dependency-Injection-Demystif... I do think it is a useful concept - but I sometimes think it gets a bit blown out of proportion.
- JanezStupar 13y agoPython itself has DI built in throughout the stack. Building Java like DI mechanisms is cargo cultism. The whole point of DI in Java is to work around its static typedness, so one can perform runtime binding of components in a dynamic manner. Three quarters of cool Java and .NET "design patterns" are just band aids around semantic limitations of these languages.
- altcognito 13y ago> The whole point of DI in Java is to work around its static typedness. So, coding the configuration of your application is an ideal way to work. Interesting.
- JanezStupar 13y agoYes! http://thedailywtf.com/Articles/Soft_Coding.aspx http://thedailywtf.com/Articles/Soft_Coding.aspx If Java guys wouldn't have stuck their heads up their asses so hard, they could use something like scripting language, with the whole debugging and library stack to configure their applications instead of building byzantine XML based DSL's. Frankly my python (code!) based config files work a lot better than similar XML based config files in Android. Except mine are debuggable and readable. The whole point of software is that it gets changed on demand. That's why it is called SOFTware. Edit: Or perhaps you are going to enlighten me why XML config files are not code? Then you might be able to explain where configurations cross the border of "code". Is configuring Sharepoint coding or configuring?
- altcognito 13y agoReadable to who? Machine readable? Which is easier to change or debug: dependencies which are in predictable locations OR buried within your source code? Could you say the same thing about arbitrary python code? Now I have to learn the way you decided to implement what's already been done dozens of times before I even get to reading the actual logic of the application. Also, for the record, if you're debugging the infrastructure of how objects are created, you're doing it wrong. You probably don't understand the framework you're working in.
- JanezStupar 13y agoYou are missing the point. I am not saying that one should make mess of his code. I am merely saying that configuration files are code too. They need to be tested, debugged and refactored. Writing code in XML seems a retarded way to go about it. If my language would prevent me from writing configurations in itself in a sane manner, I would rather use another language with a solid toolchain for this part of the job. Than go and build my own DSL in XML of all things. If you are of an open mind, go check how Django is configured and extended and compare it to your run of the mill enterprise framework. And let me tell you that I did my fair share of enterprisey "configurable and extendable" framework shit. And its all a lie. You cannot make "simplified" languages that will enable you to be more "flexible" or to let amateurs to program system behavior. Always these kinds of interfaces gobble up way more developer hours than they save in the long run. Often programmers resort to writing programs that offer a sane API for writing these kinds of configs. One should simply build a quality API and document it well.
- manojlds 13y agoThank you for treating .NET as a language and ignoring languages like F#
- JanezStupar 13y agoI was aware of this dichotomy when writing the post. And I find .NET stack much better than JVM stack. In fact while I couldn't stomach working in Java I would be much more willing working with C#, not to mention other .NET citizens. But in general as a spectrum, the toolkits and libraries of .NET and Java share architectural and design patterns. However the .NET stack does have built in languages (Javacript and F#) that can be used to glue together the rest of the system in a sane manner. The capability is there, however standard operating procedure is sadly still using shitload of XML's.
- camus 13y agoAt its core DI is basically constructing instances of objects with the help of other objects. So you have constructor injection , setter injection , property injection ,etc ... Now that you got these huge object graphs, you need a way to wire them without pain. That's where service locators and IOC containers are handy. A service locator is basically a registery that holds the dependencies and you instanciate objects that way container.createDefinition("MyClass").injectProperty("prop1","MyOtherClass").injectConstructor("MyThirdClass","MyLastClass") then myInstance = container.create("MyClass") or whatever The question is while dependency injection is good practice in general , do Pythonists need IOC containers or Service Locators? etc ... AMD in javascript has nothing to do with dependency injection. AMD is not about instanciating objects but resolving file dependencies.
- gizzlon 13y ago"At its core DI is basically constructing instances of objects with the help of other objects." Why would you wan t that in python? Doesn't python have higher order functions?
- kyllo 13y agoExactly, in a dynamic language with first-class functions you don't need to create object factories just to pass around static methods between scopes.