Advertisement
vamsiampolu

DI First Impression

Apr 26th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. Dependency Injection:
  2.  
  3. An object acts as a manifest for dependencies which register themselves.
  4.  
  5. Once the dependencies have registered themselves,an `injector` compiles the code,looking for resolve requests in the source and:
  6.  
  7. 1) it loads a pre-defined array of dependencies and maps them in the order they were registered
  8. 2)uses fancy regex to convert get the arguments from a string version of each function/method and resolves them either to a dependency or an argument.
  9.  
  10. The first one seems easier to read,you see the list of dependencies with the function definition and it seems easier to run because it has a list of dependencies thus making it much easier to know which one is a dependency and which one is an argument.It is more code and the order has to be correct.
  11.  
  12. The second one seems a little more problematic,it seems to involve a lot of guess work,assume that someone gives me a file in their project which has DI,the scenario here for a poor sap like me is that the code uses arguments to functions that remain unknown(cannot be found in the file) unless I have access to the rest of the project.What if the coder defines a variable with the same name as the dependency,if dependencies are resolved first it becomes a dependency and if arguments are resolved first it becomes an argument.This can be rather confusing,in order to not have this,you will need very strict naming conventions which can be quite tedious to write and make the code look like a court/meeting record.
  13.  
  14. Also,consider this,for each dependency there are two names,the file it was placed in and the dependency name for it,this means that one has to keep track of two names(where the code is and what it's dependency name is ---> cool FB game idea here).I think it encourages people to write more code in fewer files to mitigate this.This will mean that a module can have many things in it although it is a commonjs style module.
  15.  
  16. Aren't module loaders like commonjs and amd superior to dependency management,they can combine with a package manager to resolve dependencies with multi version support while providing interoperability for other formats.DI seemsimplementation based.It feels like module loading for languages where module loading is not possible.Also,minification breaks stuff in the second case causing people a lot of grief.
  17.  
  18. Also,the article mentions adding a parameter without having to worry about where it came from,but isnt usually better to be a little more explicit???(seems to be a pro)
  19.  
  20. Also,when I was an Android dev,I heard somewhere that using a lot of regex is not recommended because it might block the message queue on the UI thread
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement