Building An Enterprise-Grade Angular Project Structure - Frontend Consulting - Typescript, Angular, NGRX and Rxjs PDF
Building An Enterprise-Grade Angular Project Structure - Frontend Consulting - Typescript, Angular, NGRX and Rxjs PDF
Building an enterprise-grade
Angular project structure
25.04.2019 — 7 Min Read — In angular
While some think of this as a simple and secondary aspect — it often hides
more complexities than it seems. Even though most times there is no perfect
solution — we can explore some of the industry best practices, and some of
the things that in my experience made the most sense.
https://frontend.consulting/building-an-enterprise-grade-angular-project-structure 1/11
05/05/2020 Building an enterprise-grade Angular project structure | Frontend Consulting | Typescript, Angular, NGRX and Rxjs
de ning the entities that make up our project at each level of the stack
Angular entities
The rst thing I often do when setting up a new codebase is to think and
de ne the programming entities that make up my stack. As Angular
one of these entities we will su x the lename with the name of the entity.
Feature Modules
As Angular apps are made of modules that can import other modules, they
naturally become the root folders that make up an Angular project. Each
module will contain all other Angular entities contained in their own folders.
https://frontend.consulting/building-an-enterprise-grade-angular-project-structure 2/11
05/05/2020 Building an enterprise-grade Angular project structure | Frontend Consulting | Typescript, Angular, NGRX and Rxjs
shopping cart feature module, this is what its structure could look like:
Shared Modules
In this case, we create a shared module SharedModule that will host all
di erent modules within a project — but aren’t normally needed outside of it.
di erent teams and projects, and that ideally don’t change very often, we
https://frontend.consulting/building-an-enterprise-grade-angular-project-structure 3/11
05/05/2020 Building an enterprise-grade Angular project structure | Frontend Consulting | Typescript, Angular, NGRX and Rxjs
💡 For a detailed overview of all the di erent module types, you can
check that out on Angular’s o cial website.
Thanks to the powerful CLI, we can easily generate Angular libraries that will
https://frontend.consulting/building-an-enterprise-grade-angular-project-structure 4/11
05/05/2020 Building an enterprise-grade Angular project structure | Frontend Consulting | Typescript, Angular, NGRX and Rxjs
we can easily publish and share these libraries with other teams/projects
you’d need to link your library to your main project and rebuild it for every
change
if this is distributed via NPM and built outside your main project, you’d
need to keep syncing your project with the latest version of the library
Example: Let’s say BigCompany uses a messaging system all teams use — we
may want to share our abstraction to avoid many libraries essentially doing
company/messaging.
https://frontend.consulting/building-an-enterprise-grade-angular-project-structure 5/11
05/05/2020 Building an enterprise-grade Angular project structure | Frontend Consulting | Typescript, Angular, NGRX and Rxjs
This would probably need a larger article, but we can’t talk about enterprise-
codebases so that all code can be reused, where essentially all the
codebase lives in the same repository. All projects will always use the
latest version of the code
composed. For example, your login page could be written in Vue and the
rest of the app in Angular and React. It is worth mentioning that Nx
massive technology companies, where many teams and projects are separate
and far away from each other.
If all developers of a company are set to work on the same main project, no
Typescript entities
https://frontend.consulting/building-an-enterprise-grade-angular-project-structure 6/11
05/05/2020 Building an enterprise-grade Angular project structure | Frontend Consulting | Typescript, Angular, NGRX and Rxjs
If you are using Angular with Typescript — and I assume you are, you also
have to take into account Typescript’s own powerful entities that we can
Here is a list of Typescript entities that you’ll be using the most in your
project:
classes
enums
I like to group these entities in their own folder within a module, which I
reluctantly call core, but this is very much up to you and your team to decide.
This includes enums, DTOs (for both requests and responses), and data
classes.
https://frontend.consulting/building-an-enterprise-grade-angular-project-structure 7/11
05/05/2020 Building an enterprise-grade Angular project structure | Frontend Consulting | Typescript, Angular, NGRX and Rxjs
locally.
State Management
Whatever state management library you’re planning on using, one thing I’d
if we import the module from a lazy loaded route — this will be imported
only when the route is loaded. Sometimes, you may need multiple feature
modules in a speci c route — in which case you may be forced to import
https://frontend.consulting/building-an-enterprise-grade-angular-project-structure 8/11
05/05/2020 Building an enterprise-grade Angular project structure | Frontend Consulting | Typescript, Angular, NGRX and Rxjs
I like to keep the state separate from feature modules, which is a practice
particularly popular but that still keeps the Angular community fairly
divided:
let’s say we have a routed module called Dashboard at root level that
contained all its UI components
again at root level — we have a folder called store that contains all the state
service modules that will handle the state
reducers
actions
selectors
Let’s look at a brief example in the image below using NGRX, which I will be
explaining in detail in a separate article.
https://frontend.consulting/building-an-enterprise-grade-angular-project-structure 9/11
05/05/2020 Building an enterprise-grade Angular project structure | Frontend Consulting | Typescript, Angular, NGRX and Rxjs
Takeaways ⭐
One of the rst things to think about when setting up a new project,
regardless of which stack you’re using, is to think about the programming
entities you are going to use
https://frontend.consulting/building-an-enterprise-grade-angular-project-structure 10/11
05/05/2020 Building an enterprise-grade Angular project structure | Frontend Consulting | Typescript, Angular, NGRX and Rxjs
PREVIOUS NEXT
https://frontend.consulting/building-an-enterprise-grade-angular-project-structure 11/11