Base solution for your next web application
Open Closed

AppCommonModule - Angular 2 App #3682


User avatar
0
bilalhaidar created

Hello,

I noticed in the AppCommonModule that you use the forRoot() method to provide 2 services. Also, in the providers array of the module you provide 2 other services.

My question is why not move them all to forRoot()? Why separating them?

Because AppCommonModule would be imported in every new Feature Module, so Angular would create more than one instance of the DateTimeService and AppLocalizationService. In a time, they could be singleton also and added with forRoot()?

Thanks


7 Answer(s)
  • User Avatar
    0
    bilalhaidar created

    Any idea?

  • User Avatar
    0
    alper created
    Support Team

    hi,

    Our development team decides architecture design.

  • User Avatar
    0
    bilalhaidar created

    I already know that and that's why I am asking to gain some knowledge.

    Maybe someone from the development team should answer this thread.

    Thanks

  • User Avatar
    0
    bilalhaidar created

    In fact, this might cause problems having multiple instances of the same service, in different levels of the injector (when using lazy loading).

    if possible to ask someone from dev team to comment on this here.

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @bilalhaidar,

    As I remember, it was offered by ngx-bootstrap team. Probably for using same (singleton) service for entire app.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    Well true, it is a convention to use forRoot and forChild. But my question why keep 2 services out of the forRoot? There will be many instances at different levels (Modules and lazy loaded modules). Why not put them all in forRoot to maintain a singleton service?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @bilalhaidar,

    Singleton instances are not good, especially for SPAs. It is hard to manage singleton instances. I don't know why ngx-bootsrap offers this but it is better to use transient services if possible.

    If you need more information on this topic, I suggest you to search it on the web :).

    Thanks.