WEBVTT

1
00:00.560 --> 00:02.090
Hi everyone.

2
00:02.090 --> 00:07.550
In this video let's talk about one important topic.

3
00:08.390 --> 00:11.840
I will call it a circular dependency.

4
00:13.550 --> 00:16.340
So what exactly is a circular dependency.

5
00:16.610 --> 00:19.160
So you can see the documentation.

6
00:19.190 --> 00:25.100
Say for example the A need class B okay.

7
00:25.130 --> 00:27.890
And class B also needs class A.

8
00:28.640 --> 00:33.290
This will create for you something called circular dependency.

9
00:33.380 --> 00:36.620
Let I write for you.

10
00:36.800 --> 00:45.410
For example we have a service A okay in the service A we inject the service B okay.

11
00:45.650 --> 00:49.550
And in the service B we also inject the service A.

12
00:49.580 --> 00:58.070
now you can see right here and right here it will create something called circular dependency.

13
00:58.910 --> 01:03.670
So Now, let me demonstrate this for you.

14
01:04.480 --> 01:06.610
Maybe in future we will.

15
01:06.640 --> 01:07.720
You will meet that.

16
01:08.650 --> 01:11.140
For example, I have a post and a user, right?

17
01:11.500 --> 01:11.980
For example.

18
01:12.010 --> 01:12.250
User.

19
01:12.250 --> 01:12.850
The user.

20
01:12.880 --> 01:25.900
Here I will inject the postService and inside the post right here I will also inject the the userService

21
01:26.020 --> 01:26.320
service

22
01:32.650 --> 01:33.220
Like that

23
01:34.240 --> 01:36.520
Now if I run the project.

24
01:41.770 --> 01:45.220
Actually I must write a private over here.

25
01:46.750 --> 01:48.700
Okay, now let's wait a bit.

26
02:02.240 --> 02:04.610
I think I will pause the video a little bit.

27
02:05.660 --> 02:07.490
Now we have something error.

28
02:07.670 --> 02:08.480
Like that

29
02:09.170 --> 02:09.530
Okay.

30
02:09.560 --> 02:11.180
If you can scroll down.

31
02:11.420 --> 02:18.020
And this error is not relate anything to the circular dependency.

32
02:18.050 --> 02:20.390
I just talk about that okay.

33
02:20.780 --> 02:26.840
This just we forgot to import the user module inside the post module right here.

34
02:27.110 --> 02:27.470
Okay.

35
02:27.470 --> 02:32.030
We must import the user module.

36
02:32.360 --> 02:32.780
Like that

37
02:33.110 --> 02:36.950
And inside the user service let me see sorry.

38
02:36.980 --> 02:43.820
Inside the user module we must export the UserService

39
02:45.080 --> 02:45.620
like that

40
02:49.820 --> 02:54.590
And actually if you not import it is already show the error right.

41
02:54.740 --> 03:05.320
But anyway let me remove and you can see it will say the error, something like circular dependency.

42
03:05.350 --> 03:12.850
Okay, now we can use the forwardRef to avoid the circular dependency.

43
03:12.880 --> 03:16.840
Let me show you the documentation now right here.

44
03:16.870 --> 03:22.900
For example, we have a cat service and we also have a common service.

45
03:22.960 --> 03:27.730
And now you can see in the common service, it use sorry.

46
03:27.730 --> 03:29.830
In the CatService, it's use the CommonService

47
03:29.860 --> 03:33.070
And also in the CommonService, it use the CatService

48
03:33.100 --> 03:33.460
Okay.

49
03:33.490 --> 03:38.140
This will create something called a circular dependency.

50
03:38.290 --> 03:46.180
And we can avoid that due to something called forwardRef with the inject annotation right here.

51
03:46.390 --> 03:46.870
Okay.

52
03:46.870 --> 03:51.640
You can test that I will not test right here also.

53
03:51.910 --> 03:57.910
And actually in here we have something called common module.

54
03:58.270 --> 04:02.060
We can use that-- right here, but.

55
04:02.090 --> 04:06.980
Let me tell you about a little bit in.

56
04:07.670 --> 04:13.070
We must avoid the circular dependency as we can.

57
04:13.700 --> 04:14.090
Okay.

58
04:15.380 --> 04:22.400
And in any programming language and any framework, we must avoid the circular dependency.

59
04:22.910 --> 04:23.540
Okay.

60
04:24.170 --> 04:35.030
So for that reason, if you have a circular dependency in our project and in your future project.

61
04:35.810 --> 04:39.470
Now let's think about us a little bit.

62
04:39.500 --> 04:40.130
Okay.

63
04:40.190 --> 04:49.580
Let's stop and think about that and try to avoid a circular dependency.

64
04:49.610 --> 04:56.120
So that's it. That's my advice and that's for the video.

65
04:56.120 --> 04:58.370
So I will see you in the next one.