Create multiple instances of an angular service

I have an odd requirement. Our’s is a single-spa application. In which, the container app would give us a navigation menu on the left (Tree View), and we would create a new instance of the module and component and load it basing on the navigation item the user selects.

We are introducing a new feature called multi-site, wherein, users could connect to multiple databases using a single UI. So, now the left nav would show the available sites and when he expands a site, the list of navigation items would load. When user clicks on a item, we would create a instance of the component and present it to the user. For components, creating multiple instances one for each site is done by the Angular DI itself since we would open each component in tabs. The siteId would be injected into the components while creating the instance.

Whereas the problem is with the Services. As we could place data and business logic in them in observables, etc., I would need to create different instances of the same service for each site.

Is there a way, I could create multiple instances one for each site and use then in the components?

Currently, I have created a simple ServiceManger for each service which would contain a getInstance method, which would create a instance for a service if it doesn’t exist and send it back.

@Injectable({ providedIn: 'root' })
// Maintains instances of family service
export class TestServiceManager {
  private readonly api = inject(TestApi);
  private readonly stateInstances = new Map<string, TestServiceManager>();

  /**
   * Gets an instance stored with instanceName param
   * @param instanceName
   */
  getInstance(instanceName: string): TestService {
    if (!this.stateInstances.has(instanceName)) {
      this.stateInstances.set(instanceName, new TestService(this.api));
    }

    return this.stateInstances.get(instanceName) as TestService;
  }
}

In the components, I would inject the TestServiceManager instead of the TestService, and I would access the service as below.

this.testServiceManager.getInstance(this.siteId)

I do not like this for a couple of reasons.

  1. I do not like it that I have to create a service manager for each service.
  2. If one service is being used in the other service, and it also maintains data in it, I would have to create new instance for that as well.
  3. I would have to explain to the new people on the approach we follow everytime and review that they would adhere to it.
  4. Deep down I would have a feeling that I’m not using the default Angular’s dependency Injection and thus not folloing the set pattern.

That being said,

Is there a better way of achieving this? I would like to see if I could let angular take care of creating different instances of services for each site basing on some custom provider or something.

If not, could we improve this and create a generic ServiceManager class using which I would not have a need to create a new ServiceManager for each service.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật