From the course: ASP.NET Core in .NET 6: Dependency Injection
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Circular dependency - ASP.NET Core Tutorial
From the course: ASP.NET Core in .NET 6: Dependency Injection
Circular dependency
- [Instructor] Circular dependency is when one class relies on another class and vice versa. For example, we have a product service, and we wish to inject the category service to get all a categories that a product belongs to. However, with the category service, we wish to inject the product service to get all the products that belong to a particular category. This is an example of circular dependency as the category service relies on the product service, and the product service relies on the category service. Circular dependency is a bad idea as it can involve tight coupling between classes, something that dependency injection is designed to reduce. In addition, it can make the code confusing as it goes against the single responsibility principle. If both services rely on each other, they both have a responsibility for each other. In ASP.NET Core, it's forbidden to use circular dependency to inject services for a class's…