A circular dependency issue because of imports

85 views Asked by At

What do I have now:

  • an abstract class (for ex. AbstractEntity);
  • a factory that produces Entities;
  • many different types of entities, that extends Entity;

and everything was good enough but was added:

  • an entity that can contain a list of entities;

In my code it looks like this:

// abstract/entity.ts
export abstract class AbstractEntity {
  readonly id: IdKey; //
}
// entities/concrete-entity.ts
export class ConcreteEntity extends Entity {
  readonly type = 'concrete type';
}

// entities/concrete-entity2.ts
export class ConcreteEntity2 extends Entity {
  readonly type = 'concrete type2';
}
// abstract/factory.ts
export abstract class EntityFactory {
  static create(creationCase): Entity | null {
    if (someCase === creationCase) { return new ConcreteEntity(); }
    if (otherCase === creationCase) { return new ConcreteEntity2(); }
    if (containerCase === creationCase) { return new ContainerEntity(); }
    
    return null;
  }

  static createFromArray(creationCases): Entity[] {
    return creationCases.map(this.create);
  }
}

and my splinter:

// entities/container-entity.ts
export class ContainerEntity extends Entity {
  readonly type = 'container';
  readonly entities: Entity[] = EntityFactory.createFromArray(creationCases);
}

and right here I have a warning about a circular dependency because my

  • factory imports ContainerEntity
  • my ContainerEntity imports factory
0

There are 0 answers