nest icon indicating copy to clipboard operation
nest copied to clipboard

Inject on custom providers with useClass.

Open cristianmartinez opened this issue 9 years ago • 10 comments

Hi @kamilmysliwiec, Is there any reason why I can't inject when creating a custom provider with useClass?.

I want to keep my domain model classes agnostic to the framework, that's why I want to define my classes without any framework intervention. Looking into the code the only way to inject when creating custom providers is using useFactory but is not clear to me why I can't do it with useClass.

Thanks in advance for the clarification.

cristianmartinez avatar Oct 30 '17 04:10 cristianmartinez

Hi @cristianmartinez, useClass should work. Could show your class?

kamilmysliwiec avatar Oct 30 '17 06:10 kamilmysliwiec

Any news ?

adrien2p avatar Oct 31 '17 06:10 adrien2p

Hey @kamilmysliwiec, Sorry for the delay. (I'm not working full time on this project).

My original codes it's a little bit large, so here is a repo of what I tried to do in my original code: https://github.com/cristianmartinez/nest-playground

UserService and UserRepository classes are part of my domain model, which a want to keep them agnostic to any framework feature. But those classes are registered into the shared module through custom provider using the useClass provider. Initially, I was looking into the nest code and I found that the useClass provider method doesn't let you use the inject option (the same you can use for useFactory provider).

This is the module.ts code which registers with a custom provider using useClass

https://github.com/nestjs/nest/blob/d2574415f4afb2767a51c4202e7b91037eaee449/src/core/injector/module.ts#L159

    public addCustomClass(component: CustomClass, collection: Map<string, any>) {
        const { provide, name, useClass } = component;
        collection.set(name, {
            name,
            metatype: useClass,
            instance: null,
            isResolved: false,
            // Doesn't take inject values
        });
    }

As you know it never takes the ´inject´ option. different to ´useFactory´:

https://github.com/nestjs/nest/blob/d2574415f4afb2767a51c4202e7b91037eaee449/src/core/injector/module.ts#L181

    public addCustomFactory(component: CustomFactory, collection: Map<string, any>) {
        const { provide, name, useFactory: factory, inject } = component;
        collection.set(name, {
            name,
            metatype: factory as any,
            instance: null,
            isResolved: false,
            inject: inject || [], // Take inject values
            isNotMetatype: true,
        });
    }

I'm pretty sure I can use useFactory and solve the issue, but I think the right way is using useClass with inject options. please let me know your thoughts. Thank you so much for your help on this.

cristianmartinez avatar Nov 01 '17 03:11 cristianmartinez

Hi @cristianmartinez, You don't need to use inject together with useClass. I totally understand that you wanna have both UsersService and UsersRepository agnostic to any framework feature, but Nest cannot resolve your class dependencies because the constructor metadata is undefined. Typescript doesn't emit any types metadata when the class doesn't have any decorator. You need to use @Component() there or create a custom decorator and use it then 🙂

kamilmysliwiec avatar Nov 01 '17 12:11 kamilmysliwiec

Any updates here 🙂 ?

kamilmysliwiec avatar Nov 07 '17 23:11 kamilmysliwiec

Hey @kamilmysliwiec,

Sorry again for the delay and thank you for your reply.

I understand the idea of what are you saying. so I just started moving into the decorator way, the reason of that is because I don't see decorators will affect our domain model classes in a deep way. Thank you so much for your help, hope someday I can help you with this brilliant project. :)

cristianmartinez avatar Nov 08 '17 01:11 cristianmartinez

Great! I'm happy to help 😃

kamilmysliwiec avatar Nov 08 '17 15:11 kamilmysliwiec

Hi @kamilmysliwiec , I have the same problem, but I don't know how to implement the custom decorator solution. Could you tell me how I should implement it?

Thanks in advance

pedroalexisruiz avatar Aug 12 '19 14:08 pedroalexisruiz

Hey @pedroalexisruiz,

If you want to implement your own decorators, you can just create a high order function that returns the original nest implementation, just like this:

export function MyOwnDecorator<T = any>(token?: T) {
  return Inject(token);
}

I must recommend you first understand how decorators work in Typescript. This is a good article about it.

https://codeburst.io/decorate-your-code-with-typescript-decorators-5be4a4ffecb4

If you already have experience with decorators, then I think you can just jump into the nest Inject decorator implementation in case you are looking for a better understanding:

https://github.com/nestjs/nest/blob/e4121d6eb4ccb7969ecc5e89f40a30f8606cd2d2/packages/common/decorators/core/inject.decorator.ts#L11

cristianmartinez avatar Aug 12 '19 17:08 cristianmartinez

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Nov 10 '19 17:11 lock[bot]