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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any useContainer(Container); method like routing-controller? #11

Open
sirfak opened this issue Aug 5, 2019 · 6 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@sirfak
Copy link

sirfak commented Aug 5, 2019

Hi
I was suggested to use koa-ts-controller as i was using routing controller before.

How do we inject Container from typedi like routing-controller package
useContainer(Container);

Thanks
Ajay

@georgyfarniev
Copy link
Contributor

georgyfarniev commented Aug 5, 2019

Cast @iyobo , I think it good idea to support basic DI, same as type-graphql does. I suggest it as option for bootstrapMiddleware instead of function call

@iyobo iyobo self-assigned this Aug 5, 2019
@iyobo iyobo added the enhancement New feature or request label Aug 5, 2019
@iyobo
Copy link
Owner

iyobo commented Dec 12, 2019

This will be added soon.
For now to get an index of controller instances:

import {getControllers} from 'koa-ts-controllers`
const indexOfControllerInstances = getControllers();

@georgyfarniev
Copy link
Contributor

georgyfarniev commented Dec 12, 2019

Hi @iyobo, thank you for reply. Honestly I decided to use koa with router directly and avoid any abstractions, but hope this can be useful for other people. So for case of koa, dependency injection is just like this: app.context = myDb and then use in controllers as ctx.db.findOne().

Functional programming rules, and I highly inspired by way choosen by Vue 3, they decided to use functional style api versus classes with decorators.

We should follow the nature, since it doesn’t have unnecessary abstraction and learn from it.

Cheers.

@iyobo
Copy link
Owner

iyobo commented Feb 14, 2021

@georgyfarniev
That example is within the jurisdiction of the developer to inject.
You could do:

koa.use(ctx => {
  ctx.db = yourDB;
});

The dependency injection concerns of koa-ts-controllers as of right now revolve around ways to get things out of ctx e.g

@Put('/:userId/profile')
  async deleteUserSubreddit(@Params() userId: number, @Ctx('db') db: YourDB) {
       const one  = await db.findOne()
  }

@iyobo
Copy link
Owner

iyobo commented Feb 14, 2021

I am still considering the useContainer paradigm. More to come soon!

@georgyfarniev
Copy link
Contributor

Thanks for update, @iyobo!

Repository owner deleted a comment from synchrovision Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants