Now let's understand what is the dependency injection or the shorthand will be the DI.
If you click on fundamentals and click on Custom provider, now you can see dependency injection is
a an inversion of control techniques.
Okay.
This is the techniques we will delegate the in instance of dependency into the something called IOC
container.
Now let me summarize what this means.
The DI or dependency injection.
It will manage the object creation.
What is mean?
Now let's go back to the example right here.
Now right here you can see, in order to.
In order to create a person, we must create the car and pass it into the parameter.
Right.
It means we as a developer we will responsible for creating the object by hand, right?
And with dependency injection, we will leave the managing object creation for dependency
injection or the framework.
Okay.
We will leave that for framework and framework in this case will be the NestJS.
So I will leave that for Nest.js.
It's mean.
Instead of create.
The class sorry.
The object instance by hand.
We will leave that for Nest.js.
And that's it for dependency injection.
And now let me show you clearly the idea.
Now, for example, I have a controller.
You can see I have a controller class.
And I have a service.
Right.
In here we will use a syntax constructor(private service)
To show it right here.
Right.
And in order to use the service.
In order to use the controller, we must initialize the service, for example.
Controller.
New controller.
Here is not work, right?
We will create a service.
And we will pass the service into here.
Right.
It very similar with this problem right now.
You can see, this is do something like here.
And we must pass the.
The object of the service object creation of the service into the parameter of the controller right
here.
And how we can pass that.
We don't have any ability to pass that, right?
And so that reason we will use a dependency injection.
Right now it means we will remove everything right here.
We don't need this anymore.
We just need to run the controller and the dependency injection will do the rest for me.
Okay, that's the idea.
Hopefully this makes sense.
And let me show you how we can use the dependency injection inside the NestJS.
Okay.
Click on the provider here.
Now we will scroll down, in order to make it.
Into the dependency injection we must use the decorator injectable from the NestJS.
Common
Now let me go to the test service
Sorry.
In injectable in the nestjs/common right here.
And that's the first step.
Now let's see it worked or not.
If I refresh, I will run the project again.
Good.
Click
Wait a bit
And now it's still not working.
The reason is we have an.
We haven't completed this step work.
This is just the first step.
Okay And now let's see the second step.
If I scroll down to here.
Go into here.
Now you can see inside the provider registration, we must pass the service into the provider of the modules.
Now let me go to the app module and inside the provider right here, I will pass the test service.
Now you can see this application successfully.
Start.
If I refresh it worked right.
And let me refactor the repository, in the service over here.
Instead of using this, I will comment this and I will here to private test repository.
And if I remove.
Okay now you will see it will crash application.
And in the fix that issue, we used the similar techniques, firstly mark it @Injectable.
Injectable
And the secondly. register it inside a module, the provider of the module.
Test repository.
Now application if I refresh it working right.
And now until now we already Understand what is the provider, right?
The provider is just the register of the dependency injection.
Okay.
In order to achieve the dependency injection inside the.
NestJS framework, we use the provider.
Okay.
And firsly, let me summarize.
Firstly we must use the decorator of the service or the repository okay.
With @Injectable.
Right.
Injectable.
And secondly we may add it inside the provider.
And only we complete two steps.
We tell the NestJS managing
The test service
And test repo for me and as the controller, it will auto go to the dependency injection and look at
the test service and the test service.
It will be auto go to the test repo inside the dependency injection okay.
And auto write it inside the controller.
It's already done by the framework.
We don't need to worry about this step.
And that's the idea about providers.
Hopefully this makes sense and I will see you in the next one.