Third layer - domain layer
Model
Well, a model is a class that represents a business entity that we face on the frontend.
Example models: user.model.ts, cart.model.ts, file-item.model.ts.
ℹ️
Take a look that methods are mostly POST/PUT calls.
class Model {
data: number
update(data: number, repo: ModelRepositoryInterface): Observable<Model> {
return repo.updateModel(data).pipe(
tap(response => this.data = data) // update only on succes response
)
}
}Store
Store represents data in views.
ℹ️
Stores are reactive source of data which makes (GET) requests and reacts to actions.
type Data = { data: Model | null }
const DEFAULT: Data = { data: null }
class Store {
data$ = new BehaviorSubject<Data>(DEFAULT)
actions = {
fetchDataForID: new Subject<number>()
}
constructor(repo: ModelRepositoryInterface) {
this.repo = repo
this.actions
.fetchDataForID.subscribe(id => this._fetchFor(id))
}
private _fetchFor(id: number) {
this.repo.findOne(id)
.subscribe(response => this.data$.next(response)) // update data
}
}Service
ℹ️
Services is useful when we need to glue up two or more models/services/etc together.
🏗️
QUAK is working on it.