Angular 2: where to store data

I have been studying angular 2 for a couple of weeks. I am a little confused by one. Please compare how data is stored / shared here:

https://github.com/Apress/pro-angular-2ed/blob/master/Angular%202.0/08%20-%20SportsStore%20-%20Orders%20and%20Checkout/SportsStore/app/model/product.repository. ts

and here:

https://github.com/gothinkster/angular2-realworld-example-app/blob/master/src/app/shared/services/comments.service.ts

The first link shows how this is done in Adam Freeman’s book called Pro Angular. We see that there is a service called ProductRepository, and that is where all the products are stored. This service has a constructor that initializes its data from another service called StaticDataSource (later in the book it was changed to take data from the rest of the api). So to summarize: we have a component that receives an injection of a service called ProductRepository. He then uses the getProducts () method from this service to retrieve all the products (which are actually just stored in an array in this service).

Now look at the second link:

Here we have a comment service. This time the data is not stored in this service. We just have a method called getComments (), which in turn executes another method from the api service. So to summarize: we have a component (ArticleComponent), and it gets an CommentService injection. It then calls getComments () on this service, which actually sends an HTTP request to the server each time it is called.


Now my question is about the difference between these approaches and the consequences. As I understand it, in the first case, all data is taken from the server only once (when the application is loaded), and then all of it is stored in services called SomethingRepository (ProductRepository, etc.). In the second link, however, every time we use the service (in any component), we receive data directly from the server.

? , , , , " " , - , - , , . , .

, , - , , , . .

+4
2

, -. .

, , , . , . , , . - , , , .

Sooooo, , , , . - , , . , websockets

+3

, : " ".

, , - , . , , , , .. , , (, , ) - .

, , . , , , . , , , .

PS. , , . : http://softwareengineering.stackexchange.com

+2

Source: https://habr.com/ru/post/1674753/


All Articles