Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Services depending on each other

Tags:

angular

In my Angular 2 app, I have two services that depend on each other (service A calling methods from service B and vice versa).

Here are the relevant code:

app.component.ts:

import {Component} from 'angular2/core'; import {TempService} from '../services/tmp'; import {Temp2Service} from '../services/tmp2';  @Component({     selector: 'my-app',     templateUrl: 'app/app/app.component.html',     providers: [TempService, Temp2Service] }) export class AppComponent { (...) } 

Service 1:

import {Injectable} from 'angular2/core'; import {Temp2Service} from './tmp2';  @Injectable() export class TempService {   constructor (private _sessionService: Temp2Service) {} } 

Service 2:

import {Injectable} from 'angular2/core'; import {TempService} from './tmp';  @Injectable() export class Temp2Service {   constructor (private _sessionService: TempService) {} } 

Running the app leads to the following error:

EXCEPTION: Cannot resolve all parameters for 'Temp2Service'(undefined). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Temp2Service' is decorated with Injectable

When commenting the constructor in one of the services, the app runs fine. So my guess is that the "cross-reference" of the two services is causing the problem.

Do you have an idea what is going wrong here? Or is my approach already wrong?

like image 424
Daniel Avatar asked Apr 02 '16 21:04

Daniel


People also ask

What causes circular dependency?

A circular dependency occurs when two classes depend on each other. For example, class A needs class B, and class B also needs class A. Circular dependencies can arise in Nest between modules and between providers. While circular dependencies should be avoided where possible, you can't always do so.

What is circular dependency in angular?

A cyclic dependency exists when a dependency of a service directly or indirectly depends on the service itself. For example, if UserService depends on EmployeeService , which also depends on UserService . Angular will have to instantiate EmployeeService to create UserService , which depends on UserService , itself.

How do you solve circular dependencies?

There are a couple of options to get rid of circular dependencies. For a longer chain, A -> B -> C -> D -> A , if one of the references is removed (for instance, the D -> A reference), the cyclic reference pattern is broken, as well. For simpler patterns, such as A -> B -> A , refactoring may be necessary.

How do I resolve circular dependency in nest JS?

Avoiding circular dependencies by refactoring The NestJS documentation advises that circular dependencies be avoided where possible. Circular dependencies create tight couplings between the classes or modules involved, which means both classes or modules have to be recompiled every time either of them is changed.


1 Answers

This is a called circular dependency. It is not an issue with Angular2 itself. It is not allowed in any language I am aware of.

You will need to refactor your code to remove this circular dependency. Likely you will need to breakup one of these services into new service.

If you follow the single responsibility principle you will find you won't get into circular dependency trap.

like image 115
Martin Avatar answered Oct 14 '22 07:10

Martin