Hi everyone.
In this video let's talk about one important topic.
I will call it a circular dependency.
So what exactly is a circular dependency.
So you can see the documentation.
Say for example the A need class B okay.
And class B also needs class A.
This will create for you something called circular dependency.
Let I write for you.
For example we have a service A okay in the service A we inject the service B okay.
And in the service B we also inject the service A.
now you can see right here and right here it will create something called circular dependency.
So Now, let me demonstrate this for you.
Maybe in future we will.
You will meet that.
For example, I have a post and a user, right?
For example.
User.
The user.
Here I will inject the postService and inside the post right here I will also inject the the userService
service
Like that
Now if I run the project.
Actually I must write a private over here.
Okay, now let's wait a bit.
I think I will pause the video a little bit.
Now we have something error.
Like that
Okay.
If you can scroll down.
And this error is not relate anything to the circular dependency.
I just talk about that okay.
This just we forgot to import the user module inside the post module right here.
Okay.
We must import the user module.
Like that
And inside the user service let me see sorry.
Inside the user module we must export the UserService
like that
And actually if you not import it is already show the error right.
But anyway let me remove and you can see it will say the error, something like circular dependency.
Okay, now we can use the forwardRef to avoid the circular dependency.
Let me show you the documentation now right here.
For example, we have a cat service and we also have a common service.
And now you can see in the common service, it use sorry.
In the CatService, it's use the CommonService
And also in the CommonService, it use the CatService
Okay.
This will create something called a circular dependency.
And we can avoid that due to something called forwardRef with the inject annotation right here.
Okay.
You can test that I will not test right here also.
And actually in here we have something called common module.
We can use that-- right here, but.
Let me tell you about a little bit in.
We must avoid the circular dependency as we can.
Okay.
And in any programming language and any framework, we must avoid the circular dependency.
Okay.
So for that reason, if you have a circular dependency in our project and in your future project.
Now let's think about us a little bit.
Okay.
Let's stop and think about that and try to avoid a circular dependency.
So that's it. That's my advice and that's for the video.
So I will see you in the next one.