0
0
Angularframework~10 mins

Facade service pattern in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Facade service pattern
Component calls Facade Service
Facade Service receives call
Calls Data Service
Data fetched
Facade returns combined data/state
Component updates view
The component talks only to the Facade Service, which handles calls to data and UI state services, then returns combined results back to the component.
Execution Sample
Angular
export class UserFacade {
  constructor(private userService: UserService, private uiStateService: UiStateService) {}

  loadUser() {
    this.uiStateService.setLoading(true);
    return this.userService.getUser().pipe(
      tap(() => this.uiStateService.setLoading(false))
    );
  }
}
This facade method sets loading state, calls user data service, then clears loading state after data arrives.
Execution Table
StepActionState BeforeState AfterOutput/Effect
1Component calls facade.loadUser()uiState.loading = falseuiState.loading = trueLoading indicator shown
2Facade calls userService.getUser()user data = nulluser data = fetchingData request sent
3UserService returns user datauser data = fetchinguser data = {name: 'Alice'}User data received
4Facade calls uiStateService.setLoading(false)uiState.loading = trueuiState.loading = falseLoading indicator hidden
5Facade returns user data observableN/AN/AComponent receives user data
6Component updates view with user dataView emptyView shows user name 'Alice'User info displayed
💡 Facade completes data fetch and UI state update, component finishes rendering user info.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
uiState.loadingfalsetruetruetruefalsefalse
user datanullnullfetching{name: 'Alice'}{name: 'Alice'}{name: 'Alice'}
component viewemptyemptyemptyemptyemptyshows 'Alice'
Key Moments - 3 Insights
Why does the facade set loading to true before calling the data service?
Setting loading to true before fetching data shows the user a loading indicator early, as seen in step 1 of the execution_table.
How does the facade keep the component simple?
The facade hides calls to multiple services and manages UI state, so the component only calls one method and gets combined results, shown in steps 1 and 5.
When does the loading indicator get hidden?
After user data arrives, the facade sets loading to false in step 4, hiding the indicator before the component updates the view.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of uiState.loading after step 2?
Atrue
Bfalse
Cnull
Dundefined
💡 Hint
Check the 'State After' column for uiState.loading in step 2.
At which step does the facade receive the user data from the service?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look for when 'UserService returns user data' in the Action column.
If the facade did not set loading to false after data arrives, what would happen?
AUser data would not load
BComponent would crash
CLoading indicator would stay visible
DNothing changes
💡 Hint
Refer to step 4 where loading is set to false to hide the indicator.
Concept Snapshot
Facade Service Pattern in Angular:
- Facade acts as a single interface for components.
- It calls multiple services internally (data, UI state).
- Manages UI states like loading indicators.
- Simplifies component code by hiding complexity.
- Returns combined data/state to component.
Full Transcript
The Facade Service Pattern in Angular helps components by providing a simple interface to complex service calls. The component calls the facade, which sets UI loading state, fetches data from a data service, then clears the loading state. This keeps the component clean and focused on displaying data. The execution trace shows the loading state changing before and after data fetch, and the component updating the view once data arrives. This pattern improves code organization and user experience by managing UI states centrally.