Angular: NGRX clean architecture with multiple stores (Part 1)

Angular: NGRX clean architecture with multiple stores

by Kate Sky

The objective of this article is to provide a technical implementation of the NGRX for an application with a complexity that could benefit from adding feature store(s) in addition to the root store.

Finally, I am going to provide links to my GitHub repository with the example code.

In part 2, I will show how to unit test components that use a feature store and later we will go into the unit testing of the NGRX store itself.

This is the list of topics that we are going to discuss in this article:

  1. A high-level use case for state management.
  2. Getting started with a root store
  3. Adding feature store to the application
  4. Organizing your store folders
  5. Adding stores to your components
  6. Conclusion
  7. Links
  8. Part 2 ( to be continued)

1. High-level use case for state management.

When a decision has been made to manage the state of your complex application by adding NGRX, it is important to understand that you will not be able to convert the whole application to use the store right from the start.

It will be a gradual process that I will try to provide a technical guide for. In the example, you will see a RootStoreModule and a Store1Module.

The root store is where you will start. This store will be the highest level of user/system actions that can be performed in the application. For example, a dashboard app will have links to other areas, so the root store will load the available links and user access and privileges needed for the app to run. Root store will manage the state of the highest level of the application or lowest since it is a root (pun attended).

The feature store is an abstraction for parts of the application that are feature-based. For example, the user will click on a feature of the application from a dashboard, that allows him to manage orders, sales, employees, etc.  This is where the feature will load or change data using the feature store. It will manage the state of this feature along.

First, you must install NGRX by following the installation steps from https://ngrx.io/guide/store/install

2. Getting started with a root store

Second, you will create a folder where you will hold all files related to the root store.

Inside the folder, you will have files needed for the store: actions, effects, reducers and an index file for exports. One of the necessary imports is CommonModule and reducers and effects.

If you use the CLI’s “ng add @ngrx/store” the structure is flatter. I like to organize it in a more 2D way.

root-store.module.ts

Second, you will add the RootStoreModule to the app.module( CLI will add it for you!)

app.module.ts

3. Adding Feature store to the application

When adding a feature store module and adding the imports one major difference from the root is the forFeature method.

feature store in store1.module.ts

Ones all the files are created you can add feature module to the app.module just like the RootStoreModule

4. Organizing your store folders

In this simple example, you can see a separate folder for the feature store on the same level as the root store, but in a real application, I recommend adding a feature store folder on the same level as the feature folder.

5. Adding stores to your components

Since this example uses both stores in the app component, it’s pretty clear how easy it is to use the store and create a truly reactive application using an NGRX store.

In AppComponent you can find root store and feature store usage.

Stores are injected into the constructor and actions are dispatched to load the data:

app.component.ts

You can see that the template is using the async pipe to load the data, there are no subscribe events. Observables are assigned the selectors from the store:

6. Conclusion

In this article, I tried to deliver a technical guide on how to organize your application NGRX stores and provide you with examples of how they can be organized.

We started by setting up the root and feature stores and registering them in the app module. We then added the stores to a component and saw how data is displayed on the screen.

7. Links

8. Part 2 (To be continued…)

Where I will show how to test a component that uses a feature store. Some mocking will take place.

I kept this project as simple as possible, so you can even take the source code and add it to your application as a template. I know I will in the future.

I hope this article helps you out with adding NGRX to your application.