Share
Embed
Scalable Application Architecture
Modern web applications have constantly growing requirements and their complexity grows exponentially. Some of the biggest challenges in front of us are state management, testability and flexibility. Given the dynamic business environment we need to make sure our projects' design uses well known patterns most developers are familiar with. In this talk I’ll introduce a scalable Angular 2 application architecture, which answers the following requirements: testability, predictable state management, scalable communication layer and modular and robust design.
Minko Gechev
December 09, 2016
Transcript
110 slides
-
Disclaimer • High-level architecture, doesn’t define interfaces • Suggestions, not
rules • Works for me, may not work for you -
Data layer • RESTful API • WebSocket application service •
WebRTC data-channel • Various data packages • Bert-RPC • JSON • etc. -
abstraction |əbˈstrakʃ(ə)n| noun [ mass noun ] … 4 the
process of considering something independently of its associations or attributes: the question cannot be considered in abstraction from the historical context in which it was raised. -
Consistent state • Domain objects should: • Have a single
instance • Explicit update -
Sample Tech Stack • Angular 2 • RxJS • ngrx
• TypeScript • ImmutableJS -
UI components Façade (Active Record-like) (provides simplified interface to the
components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers -
UI components Façade (Active Record-like) (provides simplified interface to the
components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers -
UI components Façade (Active Record-like) (provides simplified interface to the
components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers -
UI components Façade (Active Record-like) (provides simplified interface to the
components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers -
UI components Façade (Active Record-like) (provides simplified interface to the
components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers -
UI components Façade (Active Record-like) (provides simplified interface to the
components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers -
game.component.ts @Component({ selector: 'sd-game', template: `...`, styles: [...] }) export
class GameComponent { constructor(private _model: GameModel) {} changeHandler(data: string) { this._model.onProgress(data); } } -
game.component.ts @Component({ selector: 'sd-game', template: `...`, styles: [...] }) export
class GameComponent { constructor(private _model: GameModel) {} changeHandler(data: string) { this._model.onProgress(data); } } -
game.component.ts @Component({ selector: 'sd-game', template: `...`, styles: [...] }) export
class GameComponent { constructor(private _model: GameModel) {} changeHandler(data: string) { this._model.onProgress(data); } } -
UI components • Simple presentational logic • As stateless as
possible • Delegate business logic to models -
UI components Façade (Active Record-like) (provides simplified interface to the
components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers -
class GameModel extends Model { game$: Observable<Game>; constructor(protected _store: Store<any>,
@Inject(AsyncService) _services: AsyncService[]) { super(_services || []); this.game$ = this._store.select('game'); } completeGame(time: number, text: string) { const action = GameActions.completeGame(time, text); this.performAsyncAction(action) .subscribe(() => this._store.dispatch(action)); } } game.model.ts -
game.model.ts class GameModel extends Model { game$: Observable<Game>; constructor(protected _store:
Store<any>, @Inject(AsyncService) _services: AsyncService[]) { super(_services || []); this.game$ = this._store.select('game'); } completeGame(time: number, text: string) { const action = GameActions.completeGame(time, text); this.performAsyncAction(action) .subscribe(() => this._store.dispatch(action)); } } -
class GameModel extends Model { game$: Observable<Game>; constructor(protected _store: Store<any>,
@Inject(AsyncService) _services: AsyncService[]) { super(_services || []); this.game$ = this._store.select('game'); } completeGame(time: number, text: string) { const action = GameActions.completeGame(time, text); this.performAsyncAction(action) .subscribe(() => this._store.dispatch(action)); } } game.model.ts -
BaseModel Model B Model A State tree Component tree state
stream (game$) references (store.select(…)) -
class GameModel extends Model { game$: Observable<Game>; constructor(protected _store: Store<any>,
@Inject(AsyncService) _services: AsyncService[]) { super(_services || []); this.game$ = this._store.select('game'); } completeGame(time: number, text: string) { const action = GameActions.completeGame(time, text); this.performAsyncAction(action) .subscribe(() => this._store.dispatch(action)); } } game.model.ts -
class GameModel extends Model { game$: Observable<Game>; constructor(protected _store: Store<any>,
@Inject(AsyncService) _services: AsyncService[]) { super(_services || []); this.game$ = this._store.select('game'); } completeGame(time: number, text: string) { const action = GameActions.completeGame(time, text); this.performAsyncAction(action) .subscribe(() => this._store.dispatch(action)); } } game.model.ts -
class GameModel extends Model { game$: Observable<Game>; constructor(protected _store: Store<any>,
@Inject(AsyncService) _services: AsyncService[]) { super(_services || []); this.game$ = this._store.select('game'); } completeGame(time: number, text: string) { const action = GameActions.completeGame(time, text); this.performAsyncAction(action) .subscribe(() => this._store.dispatch(action)); } } game.model.ts -
Models (Façade) • Map the state stream • Delegate business
logic to reducers • Delegate data-access logic to async services -
UI components Façade (Active Record-like) (provides simplified interface to the
components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers -
game.reducer.ts export const gameReducer = (state: any = initialState.get('game'), action:
Action) => { switch (action.type) { case START_GAME: state = fromJS({}); break; case GAME_PROGRESS: state = state.set('currentText', action.payload.text); break; } return state; }; -
game.reducer.ts export const gameReducer = (state: any = initialState.get('game'), action:
Action) => { switch (action.type) { case START_GAME: state = fromJS({}); break; case GAME_PROGRESS: state = state.set('currentText', action.payload.text); break; } return state; }; -
game.reducer.ts export const gameReducer = (state: any = initialState.get('game'), action:
Action) => { switch (action.type) { case START_GAME: state = fromJS({}); break; case GAME_PROGRESS: state = state.set('currentText', action.payload.text); break; } return state; }; -
game.reducer.ts export const gameReducer = (state: any = initialState.get('game'), action:
Action) => { switch (action.type) { case START_GAME: state = fromJS({}); break; case GAME_PROGRESS: state = state.set('currentText', action.payload.text); break; } return state; }; -
game.component.ts … get currentText$() { return this._model.game$ .map((game: Game) =>
game.currentText); } … -
game.component.ts … get currentText$() { return this._model.game$ .map((game: Game) =>
game.currentText); } … -
game.component.ts … get currentText$() { return this._model.game$ .map((game: Game) =>
game.currentText); } … -
Reducers • Pure functions of the state and an action
• Implement the business logic -
UI components Façade (Active Record-like) (provides simplified interface to the
components) State management Async services Gateways (HTTP, WS, WebRTC) Commands (RESTful, RPC) Payloads (BERT, JSON) Store Reducers -
export abstract class AsyncService { abstract process(data: Action): Observable<any>; }
base.async-service.ts -
export class GameP2PService extends AsyncService { constructor(private _rtcGateway: WebRTCGateway, private
_store: Store) { _rtcGateway.dataStream .map((data: any) => JSON.parse(data.toString())) .subscribe((command: any) => { command.method === PROGRESS && _store.dispatch(P2PActions.progress(command.payload.text)); }); } process(action: Action) { const commandBuilder = buildP2PCommand(action); if (commandBuilder) return commandBuilder().invoke(); } } game-p2p.async-service.ts -
export class GameP2PService extends AsyncService { constructor(private _rtcGateway: WebRTCGateway, private
_store: Store) { _rtcGateway.dataStream .map((data: any) => JSON.parse(data.toString())) .subscribe((command: any) => { command.method === PROGRESS && _store.dispatch(P2PActions.progress(command.payload.text)); }); } process(action: Action) { const commandBuilder = buildP2PCommand(action); if (commandBuilder) return commandBuilder().invoke(); } } game-p2p.async-service.ts -
export class GameP2PService extends AsyncService { constructor(private _rtcGateway: WebRTCGateway, private
_store: Store) { _rtcGateway.dataStream .map((data: any) => JSON.parse(data.toString())) .subscribe((command: any) => { command.method === PROGRESS && _store.dispatch(P2PActions.progress(command.payload.text)); }); } process(action: Action) { const commandBuilder = buildP2PCommand(action); if (commandBuilder) return commandBuilder().invoke(); } } game-p2p.async-service.ts -
Async services • Map actions to commands • Map responses/messages
to actions • Use an abstract gateway -
Async services Business facade Business logic Communication logic Immutable app
state Component tree root cmp sign-up form user UserModel User Action Creator signup(data) signup(data) RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name uses as user$ -
Async services Business facade Business logic Communication logic Immutable app
state Component tree root cmp sign-up form user UserModel User Action Creator signup(data) RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) uses as user$ -
Async services Business facade Business logic Communication logic Immutable app
state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$ -
Async services Business facade Business logic Communication logic Immutable app
state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$ -
Async services Business facade Business logic Communication logic Immutable app
state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$ -
Async services Business facade Business logic Communication logic Immutable app
state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$ -
Async services Business facade Business logic Communication logic Immutable app
state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$ -
Async services Business facade Business logic Communication logic Immutable app
state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$ -
Async services Business facade Business logic Communication logic Immutable app
state Component tree root cmp sign-up form user UserModel User Action Creator RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) signup(data) uses as user$ -
Async services Business facade Business logic Communication logic Immutable app
state Component tree root cmp sign-up form user UserModel User Action Creator signup(data) RESTful Async Service process(action) userReducer register() RESTful CommandBuilder build(action) Restful Command Restful Gateway invoke() send() creates() uses as user$ uses user$ Stream Dependency Action (manipulation/method call) User registration user.email = email user.name = name signup(data) -
Properties… • Predictable state management • Testable (easy to mock
services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events -
Properties… • Predictable state management • Testable (easy to mock
services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events -
Properties… • Predictable state management • Testable (easy to mock
services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events -
Properties… • Predictable state management • Testable (easy to mock
services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events -
Properties… • Predictable state management • Testable (easy to mock
services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events -
Properties… • Predictable state management • Testable (easy to mock
services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events -
Properties… • Predictable state management • Testable (easy to mock
services thanks to DI) • Not coupled to any remote service • Serializable state usable offline • Model can use different services based on context • Easy management of async events