question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Keep CoreModule as preferred location for app-wide single-use components

See original GitHub issue

πŸ“š Docs or angular.io bug report

Description

The CoreModule guidelines have recently been removed from the style guide. While the CoreModule is indeed not relevant for providing app-wide singleton services now that we have the providedIn option in the @Injectable() decorator, the style guide also gave it another purpose. The CoreModule was the place to go for declaring app-wide single-use components, such as a navigation bar or menu.

I think this recommendation could still be relevant in the style guide.

πŸ”¬ Minimal Reproduction

What’s the affected URL?**

https://angular.io/guide/styleguide

Expected vs Actual Behavior**

The CoreModule guidelines have been completely removed from the style guide.

I think that the CoreModule is still a great place to declare app-wide single-use components, along with their dependencies, which would otherwise add some unnecessary cluttering to the AppModule. The SharedModule would not be a good location for these components, since they are not β€œactually” re-usable. While it is a pretty opinionated choice, I find it useful to have a common place amongst all projects for these specific components, as it used to be the case for app-wide singleton services (I still tend to keep these in a services sub-folder of the core folder, because I find it kind of weird to have a services folder directly under app, alongside feature modules folders).

Feel free to weigh in on this proposal, I’ll be very happy to discuss it further ! 😸

Anything else relevant?

This issue refers to PR #28434 by @kapunahelewong

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:14
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

19reactions
johnpapacommented, Apr 15, 2019

This is all great feedback. Now that services can be provided in the root using providedIn it does make it easier to put services where they are needed.

However, the CoreModule was never intended to be a place to put services that should only be defined and used in a sub-ngmodule. So here are my thoughts on this, with some questions to consider.

My domain here is to consider app-wide services. Not ones used only in a sub-ngmodule. What about services used app-wide by needing to be defined in a sub module? Let’s hold off on that for a moment.

First - app-wide stuff in the root …

  1. When you have a dozen or so services that are used by the entire app, would you really want to declare them all over the place or in a single place?
  2. If the answer to #1 is β€œyes” then would that place be AppModule? If so, are you comfortable with having a dozen or so of them defined there? Possibly.
  3. Would these app wide services be useful in other apps? If so, a Core module can make it easier to test and find these. CoreModule helps here
  4. Would these app wide services be good candidates to other apps via an npm library? If so, I might suggest a npm module and not an specific module (via the Angular CLI)

I do think there is some value for a CoreModule for organizational purposes. (see above)

But … what about a service that a sub module needs and some other one might need. Could you define that in a sub module? Well, you could … let’s walk through that. Assume we have a Customer ngmodule and we define a CustomerService that gets and puts customer data and it uses providedIn to the root. Now another module needs Customer data. You could still use it in that other ngmodule, let’s say it handles Orders. So the ngOrders module now uses a service that is provided in the root, but exists in the Customer Module. This works. But is is clear? Is it readable?

New questions based on this new scenario … How does lazy loading of OrdersModule work now that it depends on a service in the CustomersModule (which is provided in the root)? Is it confusing? What happens if you re-use OrdersModule in another app? It has a dependency that you now need to track down?

IMO - any app-wide β€œthings” should be easily findable. Thus the concept (not the name though) of a CoreModule is still valid and I still use it.

Hope this helps πŸ˜ƒ

2reactions
kapunahelewongcommented, May 29, 2020

Status: docs team needs to partner with engineering to determine next steps. Great context info above!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 6 Services: providedIn: 'root' vs CoreModule
Do gather application-wide, single use components in the CoreModule. Import it once (in the AppModule) when the app starts and never import itΒ ......
Read more >
Choosing The Right File Structure for Angular in 2020 and ...
Do structure the app such that you can Locate code quickly, Identify the code at a glance, keep the Flattest structure you can,...
Read more >
Style Guide - ts - Angular
This style guide presents preferred conventions and, as importantly, explains why. Contents. Single responsibility; Naming; Coding conventions; App structureΒ ...
Read more >
Reactor 3 Reference Guide
It offers composable asynchronous sequence APIs β€” Flux (for [N] elements) and Mono (for [0|1] elements) β€” and extensively implements theΒ ...
Read more >
Core Technologies - Spring
Arguments to the static factory method are supplied by <constructor-arg/> elements, exactly the same as if a constructor had actually been used.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found