Decoupling services from components

In 8- Dependency Injection | Code with Mosh at around 3:45, decoupling the service from the component is discussed. It is mentioned that by decoupling, we can later test this service with a fake backend without making any changes in the component. Why can’t you test with a fake backend while also using new CoursesService()? Any more explanation about this decoupling would be really appreciated. Thank you.

@jordankittle Good Question!
Computer Science (Separation of Concerns):
-Components (user-facing) are the first tier
-Services are the second tier
-Calls to/from data is the third tier

Please continue with the course and you will understand why.
If not, then please inquire.

1 Like

Hi @JerryC, thank you for the reply. I understand why you want to separate the concerns, what I am not understanding is how instantiating a new Service in the component is different from injecting it. I understand the goal and the reasoning (why) but I am unclear on the mechanics (how) I have read Angular and I understand that using dependency injection from a provider at the root level creates a single instance of the service that can be used by any component and module. I understand that registering the service at the module level would provide the same instance of the service to all components in that module, and that registering the service at the component level would create a new service instance each time that component was used. Would registering the service at the component level (in the Component decorator) be the same as declaring new Service()? I am just trying to wrap my head around the mechanics.