r/javascript • • Apr 07 '17

Opinionated Comparison of React, Angular2, and Aurelia

https://github.com/stickfigure/blog/wiki/Opinionated-Comparison-of-React%2C-Angular2%2C-and-Aurelia
59 Upvotes

55 comments sorted by

View all comments

Show parent comments

3

u/[deleted] Apr 07 '17

You don't seem to understand why I'm asking you to tell me the difference between global variables (and locators) and injection.

It's not because I'm not aware of it. It's because you're listing benefits which are common to all those patterns, and dismissing benefits which are you unique to injection.

Dependency Injection is a better pattern than a Service Locator because it makes dependencies explicit, hopefully resolving a lot of runtime errors.

This doesn't even make sense because all errors in JavaScript are "runtime errors". Even if you use a type system like TypeScript, using autowiring containers would results in more runtime errors, not less, compared to alternatives.

See here

I'm very well aware of what this article says. It's somewhat bizarre you're linking to it, but you don't understand its substance. Especially in the context of JavaScript.

Also neither Angular2 nor Aurelia adhere to the Service Locator pattern as they ask for explicit dependencies in the constructor.

You're falling for style over substance. Aside from syntax, tell me what's the difference between calling a locator, and putting parameters in a constructor which are automatically mapped to equivalent locator calls.

Injection doesn't have any of this automatic resolution magic going on, and it results in a very different architectural picture as a result. But you don't seem to understand why.

1

u/tme321 Apr 08 '17

The mappings are only automatic if you don't override them. That is how angular generally works. If you want to seamlessly override a particular DI'd object to a particular component you do so by specifying which object you want to inject instead of the one the component thinks it is asking for.

But if you don't do any overriding that is when the automatic wiring up of dependencies happens.

This is pretty basic DI stuff and how pretty much all of them work. The whole point of it is to enable inversion of control as mentioned. Just because it's possible to abuse the system and inject global state objects everywhere doesn't invalidate the idea it just means you need a better class of developer who doesn't abuse the pattern. That statement holds true for basically any design pattern not just DI.

1

u/[deleted] Apr 08 '17

Let me ask you a simple question then. Let's say we have a wizard class developer, so that we can get that "your developers aren't good enough" straw man argument out.

We have 90 components, and some very basic DI stuff to do:

  • 30 of them should see object foo1, when they ask for Foo.

  • 30 of them should see object foo2, when they ask for Foo.

  • 30 of them should not be able to ask for Foo.

You can organize the project however you like, tell me how the "default automatic mapping" makes sense in this basic scenario. Or would you start overriding the DI on every single one of them? That means at least 60 overrides, and DRY be damned, I guess.

Because that's what a real project structure looks like. If you grow it like a monolith, where the default is to resolve singletons by interface/name throughout the project, at some point you'll have a mess, even your developers are all master class hacker magicians.

1

u/tme321 Apr 08 '17

You can override the type used for an instance of di across entire subtrees of the app. You don't have to specify manually for each one. As per my other comment if you want to see how to actually do this go read the angular documentation.

→ More replies (0)