I would like know if it good or bad to initialize my model by a request to a webservices or is it better to use an another public method called after the constructor
For example:
class Model {
ModelData data;
Model(Integer model_id) {
data = Request.getDataFromWebServices(model_id);
}
}
It is generally a good aproach to use as constructor args parameters that are required for the class to be functional (instead of using setters).
So in your case if
model_idis mandatory forModelto work, it is correct you have it there.Now you use
model_idto do a remote method call.Remote method calls can take more time to execute, making
Modeltaking more time to initialize and could fail e.g. due to network reasons.If the api covers any exception either coming from network layer or from the actual processing and returning a good value to initialize the
Modelthen IMHO it should be ok as it is.Just document the class as taking more time to initialize due to network access