3

I have a fair bit of experience developing Angular-UIs, but one thing that's simply not computing for me is what the REAL gain of using a CoreModule is. I do get a SharedModule, it contains reuseable components (like an "are you sure?" -modal dialog) that are used in many other modules.

But the CoreModule just bugs me - Now I am supposed to put all "globally used services" in there - and of course that's what I did in recent projects. But given that it's imported exactly once (in the AppModule) - please explain to me what I really gain by doing that? I thought a lot (actually probably too much hehe) about it and I really don't see a good reason why I just couldn't forget the CoreModule and use the AppModule instead - especiall since now we can use providedIn: 'root' for global-services anyway.

I really feel like I must be overlooking something, because I don't get it..

1
  • I think this is a valid question and I would like to know the same thing. I disagree with the vote to close as this question isn't asking for opinions if read carefully, its asking what is gained. Commented Sep 27, 2020 at 14:20

1 Answer 1

4

Your question is valid and yes, it is a bit thought-provoking.

However, it is more of a commonly followed convention to place all the services with providedIn: 'root' in the CoreModule. The logic behind this is, it will definitely be imported once and it will be imported by the AppModule hence, it should be available globally to the application.

Now, let's take a step back and think about what made us use providedIn: 'root' for a service which was placed in CoreModule, it was done so that the service is an absolute singleton throughout the application and no matter where it is injected to, the same instance is injected. This obviously means it may be injected into multiple lazy loaded modules but keeping it in the CoreModule will make sure we conventionally keep aside all such services in a common module i.e. CoreModule and all such services' instances will be created when the application starts.

In a lazy-loaded module, you definitely would not want to place a Service with providedIn: 'root' because you would definitely want an instance of the Service to be created only when the lazy-loaded module is loaded.

There is no hard and fast rule that forces us to do so, as I have mentioned, it is a commonly followed convention. A new developer when he/she looks at your code for the first time will know "hey look! this is the CoreModule, all the globally used singletons should be present here!".

I have taken examples of only services, you may treat other singleton stuff similarly.

Some conventions are not exact, there may be differences in opinion, separating code into well-defined modules, and writing clean and maintainable code is all that matters. You can debate over the fact that where should a Navigation bar component/Toolbar be placed? It is used only once in the AppModule and the same is displayed throughout the application which makes it sound like a candidate for CoreModule but, CoreModule is not supposed to be having any components. Shall we place it in the SharedModule where components are allowed? It is only going to be used in one module, why place it in a module which is supposed to be accessed by multiple modules? Place it in the AppModule? Isn't AppModule supposed to contain as minimum components as possible?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.