In OOP I was taught that your objects should only expose their members via getters and should never allow direct mutation.
Looking at some Angular2 apps I always see this concept violated.
export class TodoService {
todos: Array<TodoData> = [];
addTodo(todo: string) {
this.todos.push({
text: todo
});
}
}
In the code above there's no guarantee that a todo won't be added in ways other than the addTodo
method (e.g. calling todoService.todos.push({text: ''})
or even by overwriting the whole todos array).
I always try to eliminate these violation possibilities, which makes my services end up looking similar to this:
export class MyService {
private _myPrimitive: boolean;
get myPrimitive(): { return this._myPrimitive; }
private _myArray: Array<number>;
get myArray(): Array<number> { return this._myArray.splice(0); }
private _myObject: MyObject;
getMyObject(): MyObject { return JSON.parse(JSON.stringify(this._myObject)); }
private _onMyEvent = new Subject<Event>();
get onMyEvent: Observable<Event> { return this._onMyEvent.asObservable(); }
}
I don't see this style of coding in any Angular2 applications. It's not just ugly, it's also slow when it comes to change detection (firing JSON.parse(JSON.stringify(object)
after every 'mousemove' event is very costly) so obviously I'm doing something wrong.
My question is:
Are there more elegant ways to achieve read-only behaivour from outside
MyService
.Why is there no such focus on hiding unnecessary logic in Angular2 than in other OOP languages such as Java/C#/PHP.