How to work with classes, services and pages with Ionic3 and Angular4

0

I launch this question so that those who have more experience tell me if my approach is correct or if it is nonsense.

Suppose we are going to develop an application with Ionic3 / Angular where we have a CRUD of Clients. The correct thing, as I have been reading and seeing examples, is to have:

  • Client Model: class where you define the attributes that the object must have.
  • Service / Provider Client: In charge of working with the database, obtaining, manipulating and storing the data.
  • Page: Implementation of the view to show and work with this data.

In all the examples that I see, they usually present it in the following way:

  • Instance the model on the page
  • I inject the service on the page

To load the data:

  • From the page I upload the data through the service and assign it to the model.

To save the data:

  • From the page assign the changes to the attributes of the model and use the service to save them in the bd.

My question is this, would not it be better to implement the access and save data also in the model ?. I have done it in some small projects but when you do not see anyone else doing it like this you have the doubt of whether it is right or not. What I mean is to have the following methods in the client class

static load(cs:ClienteService,id):Cliente{
//funcion a la que paso el provider que accede a los datos y el id que voy buscando
}

guardar(cs:ClienteService):boolean{
//funcion que guarda el objeto 
}

The Service Client would be injected into the page and would be passed as an argument to the Client object to perform the corresponding operations with the data. In this way you can implement all the logic, data checking, etc on the model.

Well, I hope that people with more experience can advise me about it.

Thank you very much.

    
asked by Goltra 30.09.2018 в 10:54
source

0 answers