Replies: 3 comments 5 replies
-
Nice article! What do you think about using this feature with lower version of Angular?. I saw that, since the v9, there is an I don't think the implementation has changed, you could do something like this import {ɵɵdirectiveInject} from '@angular/core';
export const inject = ɵɵdirectiveInject; |
Beta Was this translation helpful? Give feedback.
-
Hi @yjaaidi ! Thanks for the article :) Good that we have this space to discuss! So, this feature clearly has advantages and drawbacks. I'm a consultant so I'm very used to imagine what devs will do with a feature like this (or better, how they could misuse or abuse it). These are my thoughts. Naming conventionsFirst of all: I'm not a fan of naming conventions :) I already know that won't be consistent across devs and teams and I expect I'll find different conventions and different names, I'll just have to deal with that. For example: prefixing the functions with "inject" (eg. state = injectState(1); How is this function injecting a state? Where is it provided? It'd be easier to say that this is creating a state, and injecting hold = injectHolder();
ngOnInit() {
this.hold(interval(1000), value => console.log(value));
} Same thing! I wouldn't know what this thing does without looking at the source, and then I'd say "well that's a strange name"! It'd be easier to reason about if it wasn't called that way. So, not a fan of prefixing with "inject" every function that uses The name itselfThis is kind of an off-topic but I'll put this here anyway: as you know we have discussed on Twitter about the name I thought about it for a while and imho the only place I (personally) find it a bit problematic is: inside The Type Inference Use CaseAgree, if I'm using The Abstract Base Class Use CaseAgree, and I agree with you on the "inheritance should be avoided but can come in handy" reasoning. Anti-patternsThis is just an advice in regards to the form of your article: when I first read it, I expected that the "anti-patterns" you'd talk about would be the ones caused by the Either way, the Anti-Pattern #3 doesn't even contain a "before" version (without the RecommendationsI agree with most of the recommendations! Especially the one regarding React's Hooks. I've seen plenty of examples on Twitter where people think about it that way ( The one recommendation I disagree with is in fact the one about the naming convention: I don't think that's actually useful, as I said. I don't see particular problems with the "constructor time" caveat, what's the worse that can happen if the dev doesn't know about it? The function will break inevitably, that's it. Right? Or am I missing something? Making it clear on the docs is mandatory imho, but other than that, I see no problems. SummaryFor the moment, I'll keep on suggesting to use constructor-based DI for most of the things. I'm not a fan of the "less code is better" argument that's usually made in these cases by some people, I never felt that was Angular's main philosophy and I still don't think it is. I value transparent code more than anything and I see how this |
Beta Was this translation helpful? Give feedback.
-
Let's talk about https://marmicode.io/blog/angular-inject-and-injection-functions
Beta Was this translation helpful? Give feedback.
All reactions