[Angular] Extract Implementation Details of ngrx from an Angular Application with the Facade Pattern
Extracting away the implementation details of ngrx from your components using the facade pattern creates some interesting possibilities in terms of iteratively migrating an application to ngrx. By decoupling your components from ngrx completely, this also makes testing and collaboration a lot easier. In this lesson, we will finalize our application by creating a facade to sit in between our component and ngrx and hide all of the ngrx implementation details away from our component. This has an added benefit of reducing the number of pieces that we need to export in our state module's barrel roll by reducing our dependency down to a single facade.
Current we have the component code like this.. we want to extract implementation detail into facade partten.
component:
import { Component, Input, OnInit, ChangeDetectionStrategy } from '@angular/core'; import { Observable } from 'rxjs'; import {Role} from '../stores/models'; import { Store, select } from '@ngrx/store'; import { DashbaordState, selectAllRoles, LoadRoles } from '../stores'; @Component({ selector: 'dashboard', templateUrl: './dashboard.component.html', styleUrls: ['./dashboard.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush }) export class DashboardComponent implements OnInit { roles$: Observable<Role[]>; constructor( private store: Store<DashbaordState> ) { this.roles$ = this.store.pipe( select(selectAllRoles) ); } ngOnInit() { this.store.dispatch(new LoadRoles()); } }
Create a facade.ts file:
import { Injectable } from "@angular/core"; import { Store, select } from '@ngrx/store'; import { DashbaordState } from '../reducers'; import { Observable } from 'rxjs'; import { Role } from '../models'; import { selectAllRoles } from '../selectors'; import { LoadRoles } from '../actions'; @Injectable({ providedIn: 'root' }) export class DashboardFacade { roles$: Observable<Role[]>; constructor( private store: Store<DashbaordState> ) { this.roles$ = store.pipe( select(selectAllRoles) ); } getRoles () { this.store.dispatch(new LoadRoles()); } }
Basiclly just move all the Store related code to the facede service.
Update the component:
import { Component, Input, OnInit, ChangeDetectionStrategy } from '@angular/core'; import { Observable } from 'rxjs'; import {Role} from '../stores/models'; import { Store, select } from '@ngrx/store'; import { DashbaordState, selectAllRoles, LoadRoles } from '../stores'; import { DashboardFacade } from '../stores/facades/dashboard.facade'; @Component({ selector: 'dashboard', templateUrl: './dashboard.component.html', styleUrls: ['./dashboard.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush }) export class DashboardComponent implements OnInit { roles$: Observable<Role[]>; constructor( private facade: DashboardFacade ) { this.roles$ = this.facade.roles$; } ngOnInit() { this.facade.getRoles(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2018-01-31 [React] Refactor a Stateful List Component to a Functional Component with React PowerPlug
2018-01-31 [MST] Use Volatile State and Lifecycle Methods to Manage Private State
2017-01-31 [React] Test friendly approach
2017-01-31 [React] Modify file structure
2017-01-31 [Javascript] Format console.log with CSS and String Template Tags
2017-01-31 [Angular] ChangeDetection -- onPush
2016-01-31 [Javascript] Redirect the browser using JavaScript