Section 10 of 17

The facade, and when to skip it

Contents
feature-work-ordersdata-accessfeaturecomponentfacadeopen · filterBy · assignstorereducerselectorseffectsHTTP APIeventsignalsucceeded · failed

A facade is a thin object between features and state management. It exposes state (signals in Angular, values in React, refs in Vue) and intentions (methods). It contains no logic: each method dispatches exactly one event, and each property is exactly one selector.

libs/maintenance/data-access/src/lib/work-orders.facade.ts

import { inject, Service } from '@angular/core';
import { Store } from '@ngrx/store';
import type { WorkOrder, WorkOrderFilter } from '@fleetline/maintenance/model';
import { workOrdersPage } from './work-orders.events';
import { workOrdersFeature } from './work-orders.feature';

@Service({ autoProvided: false })
export class WorkOrdersFacade {
  private readonly store = inject(Store);

  readonly visible = this.store.selectSignal(workOrdersFeature.selectVisible);
  readonly overdueCount = this.store.selectSignal(workOrdersFeature.selectOverdueCount);
  readonly filter = this.store.selectSignal(workOrdersFeature.selectFilter);
  readonly loading = this.store.selectSignal(workOrdersFeature.selectLoading);
  readonly error = this.store.selectSignal(workOrdersFeature.selectError);

  open(depotId: string): void {
    this.store.dispatch(workOrdersPage.opened({ depotId }));
  }

  filterBy(filter: Partial<Omit<WorkOrderFilter, 'depotId'>>): void {
    this.store.dispatch(workOrdersPage.filterChanged({ filter }));
  }

  assign(order: WorkOrder, assigneeId: string): void {
    this.store.dispatch(
      workOrdersPage.assigneeChosen({ id: order.id, assigneeId, previousAssigneeId: order.assigneeId }),
    );
  }
}

autoProvided: false turns off providing in root. The facade is provided by provideWorkOrders(), so it exists exactly where its store feature exists.

10.1. Why a facade

  • Replaceable state. The day work orders move from the NgRx Store to a Signal Store or to a server cache, the facade changes and no component does. Fleetline did exactly this for fleet (section 7.1).
  • A small surface. A feature sees six members instead of fourteen actions and nine selectors.
  • Easy tests. A feature test provides a fake object with the same shape (section 15).
  • The same shape in every framework. The class here, useWorkOrders() in React and Vue: the feature code reads almost the same in all three (section 14).

10.2. Why the facade is optional

The NgRx community has a good argument against facades: actions should describe events ("assignee chosen on the work orders page"), and a facade with a method assign() invites thinking in commands. A facade that dispatches three actions in a row, or that holds logic, hides the event log that makes NgRx debuggable.

So in our variant the facade is a rule with conditions:

Use a facade when Skip it when
more than one feature uses the same state one feature owns one small store feature
the state may change implementation (Store ↔ Signal Store ↔ server cache) the state is local to a component: use a Signal Store or plain signals in the component
the data is offered to another domain through api the team prefers dispatching events directly and the domain is small
the code base is shared with React or Vue teams

Two rules when you do use one:

  1. Method names keep the event. assign() dispatches assigneeChosen from the Work Orders Page source. If two pages can assign, each page gets its own event; the facade gets two methods or a source parameter, never one generic update().
  2. No logic. No if, no loop, no call to HTTP. If it needs a decision, the decision belongs in a reducer, a selector or an effect.

Without a facade, the index.ts of data-access exports the page events and the selectors instead, and the component calls store.dispatch() and store.selectSignal() itself. Both shapes pass the boundary rules; pick one per domain and write it in the domain's README.