Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

455
Views
Angular 2 service: where to keep the data

I have been learning angular 2 for a couple weeks. I am a bit confused with one thing. Please compare how the 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 it is done in Adam Freeman's book called "Pro Angular". We can see that there is a service called ProductRepository, and thats where all the products are stored. This service has a constructor which initializes its data from another service, called StaticDataSource (later in the book its changed to take the data from the rest api). So to sum up: We have a component, that gets injected a service called ProductRepository. Then it uses getProducts() method from this service in order to receive all the products (which in reality are just storied in an array in that service).

Now lets look at the second link:

In here we have a CommentsService. This time the data is not stored in this service. We just have the method called getComments() which in turn executes another method from an api service. So to sum up: We have a component (ArticleComponent) and it gets injected the CommentsService. Then it calls getComments() on that service, which in reality sends a http.get request to the server each time it is called.


Now my question is about the difference between these approaches and the consequences. From what I understand is that in the first case all the data is taken from the server only once (when the app loads) and then its all stored in services called SomethingRepository (ProductRepository etc). In the second link however, every single time we use a service (in any component), we receive the data straight from the server.

What is the best practice about it? I am just worried that if we use the approach that is presented in the book, then we wont always get the 'freshest' possible data, because if another client changed something in the meantime, then we still deal with data that was downloaded when our app was loading. On the other hand, the second approach may influence our possibility of sharing data between the components.

I am really confused with this and I am not sure if I actually should keep the whole model in my app and have some kind of repositories, or maybe the second approach is better. Thank you for any help.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

A collection of products is very unlikely to change in the time you spend on that website. Which in this case makes it far more relevant to actually cache the obtained data.

A collection of comments, for instance in an active message board, can change multiple times during a single page visit. This is probably why they choose to always call from the service when the collection is obtained. Even though in my opinion, this is not the right way to do it. Better would be to use a websocket connection, and update the collection from the server, instead of obtaining the collection every time, with the high probability that nothing has changed.

Sooooo, to sum it up, it depends on case to case, collection to collection, which kind of caching you would like to use. But my recommendation is to call the server only once, if it's static data like a product array. And when it's dynamic data, you should use websockets to maintain the collection

about 4 years ago · Santiago Trujillo Report

0

Unfortunately, as most of software development goes, the answer to which approach is better is: "it depends".

As you've clearly identified, the risk with the approach of storing the data client-side is that if another user changes something, you run the risk of having a local cache of stale data. Whether that matters or not depends on the app and the type of data being cached and how often that data changes etc. But, you do get better performance from the point of view of not having to go to the server every time to get the list. Some types of data are much safer to cache this way (eg the list of US states, or even a product catalog that's not too large and doesn't change by the minute) - for other apps, you just can't afford to have stale data.

So, my takeaway would be to use the approach best suited for the data at hand. Many times, the added complexity of dealing with invalidating a cache isn't worth the performance benefits it brings, but that's never a blanket statement that you could use in all situations. Learn the different approaches and the benefits and tradeoffs so you know when to apply which technique.

PS. This question is likely to be closed as being opinion-based. It might be better suited for: http://softwareengineering.stackexchange.com

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!