Search code examples
angularangular-libraryangular11

How To Generate Circular Library In Angular


I have an issue to generate a circular library.

For an example: I have 2 library, the first one is @core/entity and the second one is @core/model.

In my entity, I have 2 entity, that is UserEntity and OrganizationEntity

UserEntity:

import { OrganizationModel } from '@core/model';

export class UserEntity {
    organization: OrganizationModel;
}

OrganizationEntity:

export class OrganizationEntity {
   code: string;
   name: string;
}

And in my model, I have 2 model, that is UserModel and OrganizationModel

UserModel

import { UserEntity } from '@core/entity';

export class UserModel extends UserEntity { }

OrganizationModel

import { OrganizationEntity } from '@core/entity';

export class OrganizationModel extends OrganizationEntity { }

My problem is, when I build my entity library, angular will remove all of them in my dist directory and it will make the UserEntity will be error, it's because UserEntity lookup the OrganizationModel and then OrganizationModel lookup the OrganizationEntity ("Organization Entity is doesn't exists anymore, because it's temporarily re-generated").

I need your help, how to fix this code in this situation, thanks in advance.


Solution

  • Fast way to break a circular references is a third library that both point to it.

    also maybe you can try to replace OrganizationModel -> OrganizationEntity in your UserEntity so no longer needs the other library.

    And if your entity is only holding data, use interface instead of class.