I am reading the book "Learning TypeScript" by Remo Jansen. In one section the author describes how to create a very simple proof-of-concept MVC framework including how to create the Model
class and says the following:
A model needs to be provided with the URL of the web service that it consumes. We are going to use a class decorator named ModelSettings to set the URL of the service to be consumed. We could inject the service URL via its constructor, but it is considered a bad practice to inject data (as opposed to a behavior) via a class constructor.
I don't understand that last sentence. In particular, I don't understand what it means to "inject data". It seems to me that in almost all introductions to JavaScript classes using over-simplified examples, data is introduced ("injected"?) into the constructor via its parameters. For example:
class Person {
constructor(name) {
this.name = name;
}
}
I certainly think of name
as data, not as behaviour, and it is universally included in this sort of example as a constructor parameter, and there is never any mention that this is bad practice. I thus assume I'm misunderstanding something in the above quote, either what is meant by "data" or by "inject" or something else.
Your answers could include explanations of when, where, how and why to use decorators in JavaScript/TypeScript, as I strongly suspect that concept is intimately connected to the understanding I seek. However, more importantly, I want to understand more generally what is meant by injecting data via a class constructor and why that's bad.
To give more context to the above quote, this is the situation: A Model
class is created which, in this example, will be used to create stock exchange models, one for NASDAQ and one for NYSE. Each model requires the path of the web service or static data file that will provide the raw data. The book states that a decorator should be used for this information, rather than a constructor parameter, leading to the following:
@ModelSettings("./data/nasdaq.json")
class NasdaqModel extends Model implements IModel {
constructor(metiator : IMediator) {
super(metiator);
}
...
}
I just haven't been understanding why I should add the service url via the decorator rather than simply as a parameter for the constructor, e.g.
constructor(metiator : IMediator, serviceUrl : string) {...