4

I have my own applicaton, written in Angular2, consist of several modules. To keep order in code, I found that I should isolate all services (for communication with API) to separate module.

I wonder if it's good idea? Can it have an impact on loading time or size my app? Could this be other negative effects? If so, why? And what are the best practices to dividing application on modules.

Thanks.

Jaroslaw K.
  • 5,016
  • 1
  • 31
  • 56

2 Answers2

4

Your services should be stored in the same module that is using them.

In case of reusing it in several modules, you might want to have them in the main component, the one which will generally store its model as well.

zurfyx
  • 23,843
  • 15
  • 103
  • 130
  • Thanks. :) But I still don't know what the consequences of isolation all services to one module. Can they seriously harm the application? If so, why? – Jaroslaw K. Jan 12 '17 at 14:17
  • 1
    @JaroslawK. harm? not at all. Harder to find? most likely. Harder to read? If you have them all in one file, for sure, if you split them, it'll be the same. Harder to import? Yes. Not to say that according to the style guides Aravind has pointed you before, most developers will be expecting to find each module services on the same folder. – zurfyx Jan 12 '17 at 14:27
1

Storing services in the same modules is a good practice, but sometimes, there are services that are used by different modules, so CoreModule is the best way for storing them.

From angular 2 styleguide, STYLE 04-11:

Do put a singleton service whose instance wil be shared throughout the application in the CoreModule (e.g. ExceptionService and LoggerService).

After all recommendations, there's a tree view and example of angular app with CoreModule (wich stores services) implemented.

im.pankratov
  • 1,380
  • 1
  • 12
  • 15