Angular: NGRX multiple store architecture (Part 1)

Kate Gable (former Sky)
4 min readJan 17, 2020

--

Angular: NGRX multiple store architecture

The objective of this article is to provide a technical implementation of the ngrx for an application with a complexity which 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. 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 decision has been made to manage 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 which 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 a 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, from the dashboard user will click on a feature of the application 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 are 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.

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

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.

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 real application I recommend adding 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:

You can see that 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 provided you with examples of how they can be organized.

We started with 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.

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 really hope this article helps you out with adding ngrx to your application.

Originally published at https://www.katesky.com on January 17, 2020.

--

--

Kate Gable (former Sky)
Kate Gable (former Sky)

Written by Kate Gable (former Sky)

sharing knowledge, encouraging to learn, promoting passion for coding, supporting mothers who code

No responses yet