WEBVTT

1
00:00.410 --> 00:07.490
Now let's understand what is the dependency injection or the shorthand will be the DI.

2
00:08.270 --> 00:15.920
If you click on fundamentals and click on Custom provider, now you can see dependency injection is

3
00:15.920 --> 00:19.850
a an inversion of control techniques.

4
00:20.000 --> 00:20.480
Okay.

5
00:20.510 --> 00:33.200
This is the techniques we will delegate the in instance of dependency into the something called IOC

6
00:33.200 --> 00:33.920
container.

7
00:33.950 --> 00:37.100
Now let me summarize what this means.

8
00:37.700 --> 00:40.910
The DI or dependency injection.

9
00:41.180 --> 00:45.050
It will manage the object creation.

10
00:45.290 --> 00:46.250
What is mean?

11
00:46.400 --> 00:50.540
Now let's go back to the example right here.

12
00:51.050 --> 01:00.550
Now right here you can see, in order to.

13
01:02.050 --> 01:13.600
In order to create a person, we must create the car and pass it into the parameter.

14
01:13.630 --> 01:14.380
Right.

15
01:17.710 --> 01:29.590
It means we as a developer we will responsible for creating the object by hand, right?

16
01:30.370 --> 01:43.090
And with dependency injection, we will leave the managing object creation for dependency

17
01:43.090 --> 01:45.130
injection or the framework.

18
01:45.160 --> 01:45.580
Okay.

19
01:46.300 --> 01:54.970
We will leave that for framework and framework in this case will be the NestJS.

20
01:55.150 --> 01:57.610
So I will leave that for Nest.js.

21
01:58.450 --> 01:59.160
It's mean.

22
01:59.670 --> 02:01.140
Instead of create.

23
02:03.750 --> 02:06.300
The class sorry.

24
02:06.300 --> 02:08.490
The object instance by hand.

25
02:08.940 --> 02:12.750
We will leave that for Nest.js.

26
02:13.530 --> 02:17.460
And that's it for dependency injection.

27
02:18.810 --> 02:24.750
And now let me show you clearly the idea.

28
02:25.260 --> 02:28.350
Now, for example, I have a controller.

29
02:28.680 --> 02:32.280
You can see I have a controller class.

30
02:34.710 --> 02:37.350
And I have a service.

31
02:37.440 --> 02:38.040
Right.

32
02:39.300 --> 02:44.700
In here we will use a syntax constructor(private service)

33
02:45.960 --> 02:47.370
To show it right here.

34
02:48.330 --> 02:48.900
Right.

35
02:50.250 --> 02:53.700
And in order to use the service.

36
02:55.710 --> 03:00.920
In order to use the controller, we must initialize the service, for example.

37
03:01.100 --> 03:01.670
Controller.

38
03:02.450 --> 03:03.200
New controller.

39
03:03.800 --> 03:05.060
Here is not work, right?

40
03:05.390 --> 03:08.420
We will create a service.

41
03:10.760 --> 03:13.250
And we will pass the service into here.

42
03:13.760 --> 03:14.360
Right.

43
03:14.390 --> 03:18.680
It very similar with this problem right now.

44
03:18.920 --> 03:22.400
You can see, this is do something like here.

45
03:24.140 --> 03:27.980
And we must pass the.

46
03:30.350 --> 03:37.490
The object of the service object creation of the service into the parameter of the controller right

47
03:37.490 --> 03:37.970
here.

48
03:38.450 --> 03:40.040
And how we can pass that.

49
03:40.670 --> 03:45.170
We don't have any ability to pass that, right?

50
03:45.320 --> 03:50.150
And so that reason we will use a dependency injection.

51
03:50.150 --> 03:53.300
Right now it means we will remove everything right here.

52
03:53.330 --> 03:54.980
We don't need this anymore.

53
03:54.980 --> 04:02.950
We just need to run the controller and the dependency injection will do the rest for me.

54
04:03.970 --> 04:05.290
Okay, that's the idea.

55
04:08.560 --> 04:09.760
Hopefully this makes sense.

56
04:10.510 --> 04:19.240
And let me show you how we can use the dependency injection inside the NestJS.

57
04:19.660 --> 04:20.140
Okay.

58
04:20.170 --> 04:21.790
Click on the provider here.

59
04:21.820 --> 04:26.860
Now we will scroll down, in order to make it.

60
04:29.980 --> 04:38.770
Into the dependency injection we must use the decorator injectable from the NestJS.

61
04:39.010 --> 04:39.370
Common

62
04:39.850 --> 04:40.300
Common

63
04:41.650 --> 04:45.370
Now let me go to the test service

64
04:45.940 --> 04:46.300
Sorry.

65
04:47.020 --> 04:52.840
In injectable in the nestjs/common right here.

66
04:54.520 --> 04:55.810
And that's the first step.

67
04:55.840 --> 04:58.720
Now let's see it worked or not.

68
04:59.410 --> 05:03.070
If I refresh, I will run the project again.

69
05:17.740 --> 05:17.830
Good.

70
05:20.170 --> 05:20.620
Click

71
05:20.650 --> 05:21.190
Wait a bit

72
05:36.070 --> 05:40.090
And now it's still not working.

73
05:42.760 --> 05:45.010
The reason is we have an.

74
05:45.220 --> 05:48.130
We haven't completed this step work.

75
05:48.880 --> 05:52.510
This is just the first step.

76
05:53.140 --> 05:57.120
Okay And now let's see the second step.

77
05:58.320 --> 06:00.690
If I scroll down to here.

78
06:07.080 --> 06:09.570
Go into here.

79
06:09.600 --> 06:21.960
Now you can see inside the provider registration, we must pass the service into the provider of the modules.

80
06:23.430 --> 06:33.690
Now let me go to the app module and inside the provider right here, I will pass the test service.

81
06:36.210 --> 06:39.930
Now you can see this application successfully.

82
06:39.930 --> 06:40.410
Start.

83
06:40.620 --> 06:42.990
If I refresh it worked right.

84
06:43.170 --> 06:48.990
And let me refactor the repository, in the service over here.

85
06:49.080 --> 07:01.490
Instead of using this, I will comment this and I will here to private test repository.

86
07:03.050 --> 07:04.190
And if I remove.

87
07:06.260 --> 07:10.940
Okay now you will see it will crash application.

88
07:10.940 --> 07:21.350
And in the fix that issue, we used the similar techniques, firstly mark it @Injectable.

89
07:24.890 --> 07:26.660
Injectable

90
07:29.510 --> 07:37.970
And the secondly. register it inside a module, the provider of the module.

91
07:40.190 --> 07:41.660
Test repository.

92
07:43.490 --> 07:48.440
Now application if I refresh it working right.

93
07:48.860 --> 07:55.000
And now until now we already Understand what is the provider, right?

94
07:55.450 --> 08:01.240
The provider is just the register of the dependency injection.

95
08:01.750 --> 08:02.320
Okay.

96
08:02.650 --> 08:06.880
In order to achieve the dependency injection inside the.

97
08:06.880 --> 08:10.240
NestJS framework, we use the provider.

98
08:11.260 --> 08:11.680
Okay.

99
08:12.070 --> 08:14.560
And firsly, let me summarize.

100
08:15.220 --> 08:24.700
Firstly we must use the decorator of the service or the repository okay.

101
08:24.850 --> 08:28.720
With @Injectable.

102
08:31.030 --> 08:31.510
Right.

103
08:32.260 --> 08:33.070
Injectable.

104
08:36.280 --> 08:41.230
And secondly we may add it inside the provider.

105
08:46.510 --> 08:49.630
And only we complete two steps.

106
08:49.900 --> 08:55.560
We tell the NestJS managing

107
08:58.200 --> 09:00.000
The test service

108
09:02.940 --> 09:17.040
And test repo for me and as the controller, it will auto go to the dependency injection and look at

109
09:17.040 --> 09:19.470
the test service and the test service.

110
09:19.500 --> 09:29.700
It will be auto go to the test repo inside the dependency injection okay.

111
09:29.790 --> 09:33.960
And auto write it inside the controller.

112
09:33.990 --> 09:37.050
It's already done by the framework.

113
09:37.080 --> 09:39.990
We don't need to worry about this step.

114
09:40.920 --> 09:44.220
And that's the idea about providers.

115
09:46.530 --> 09:50.880
Hopefully this makes sense and I will see you in the next one.