Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC - Field level injection via added get function to the inject #1742

Closed
brandonseydel opened this issue Apr 17, 2023 · 3 comments 路 Fixed by #1748
Closed

RFC - Field level injection via added get function to the inject #1742

brandonseydel opened this issue Apr 17, 2023 · 3 comments 路 Fixed by #1748
Labels
RFC Request For Comment - Potential enhancement that needs discussion and/or requires experimentation Topic: DI

Comments

@brandonseydel
Copy link
Member

brandonseydel commented Apr 17, 2023

馃挰 RFC

While injecting via constructor is great, there is a problem when base classing or trying to use in generic functions to construct a result.

馃敠 Context

Ctor is getting messy when injecting lots of parameters, hard to get an instance from the container in a generic function, and not having to call super with the parent injections would be beneficial for lib auths.

馃捇 Examples

class AbcNew {
  a = inject.get(A);
  b = inject.get(B);
}

class AbcOld {
  constructor(private readonly a:A, private readonly b:B,}{}
}


// new
class Def extends AbcNew {
 c = inject.get(C);
}

// old
class Def extends AbcOld {
  constructor(a:A, b:B, private readonly c:C}{
  super(a,b);
  }
}

class CrazyStuff {
  a = doSomethingCrazy();
}


doSomethingCrazy(){
const service= inject.get(Service);
const result = service.findAKey('sdfsd');
return inject.get(result);
}

@zewa666 @bigopon

@brandonseydel brandonseydel changed the title Field level injection RFC - Field level injection via added get function to the inject Apr 17, 2023
@bigopon
Copy link
Member

bigopon commented Apr 17, 2023

I think we need to mention where we have seen this approach and express thanks to their ideas/exploration. Particularly Angular and Vue.

Personally, I think this is not bad, and works well with native class field to avoid having to have constructor as well. One point of discussion could be the function call to inject. Should it be a separate export or a property on the inject export itself:

class Abc {
  a = injected(A)
  // vs
  a = inject.get(A)
  // vs
  a = fromDI(A)
  // vs
  a = di(A)
  // ... etc names
}

@brandonseydel
Copy link
Member Author

brandonseydel commented Apr 17, 2023

You can see Angular implementation
https://angular.io/api/core/inject

While this idea is very old they helped flush out the use cases and make it more standard. :) You can see community contribution for au1 here (deco based which is an option to combine with a func)
https://github.com/devaur/aurelia-property-injection

@bigopon bigopon added the RFC Request For Comment - Potential enhancement that needs discussion and/or requires experimentation label Apr 17, 2023
@zewa666
Copy link
Member

zewa666 commented Apr 18, 2023

while at it we could improve the dx via array destructuring to inject multiple keys at once

const [aa, bb, cc] = inject(a, b, c)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Request For Comment - Potential enhancement that needs discussion and/or requires experimentation Topic: DI
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants