1

I'm working on some Angular JS SPA .

I'm trying to understand if from architecture perspective it is correct to use angular services for holding\sharing data between views or should I use some caching mechanism.

My need is to hold and share any REST response and fetch again by demand.

Unfortunately I couldn't get clear answer if services is the correct place for holding the data.

Please advice based on your experience.

Thanks.

Amir_Af
  • 576
  • 6
  • 22
  • Are you sure you want to write your own service? Why not using something like Angular models if you already having REST at your SPA? https://github.com/mgonto/restangular Without experience it's really hard to create architecture that will suit you - with relations, caching, etc. If you just need to store data during life time of app - yes, the easiest way is to use `$cacheFactory` to create your own `applicationCache` 'storage'. – Appeiron Jul 18 '16 at 13:56
  • Not sure I understand . I'm creating several services which each one will do different http call. I want to keep the response data in the service and to inject the service in the relevant view. any view which will do manipulation on the data will do it on the service so all other view will see the reflected result. why writing my own services is wrong ? – Amir_Af Jul 18 '16 at 14:15
  • You have REST API implemented in SPA, so you gonna have **relations** between different data objects, am I right? – Appeiron Jul 18 '16 at 14:19
  • Let me give you simple example. Lets say I will have service for fetching (http call) students list and service for fetching customers list. For each list i will have some business logic like selected student , delete , add , selected customer.... so the idea is that each service will do the http call, will hold the results and will provide some functions (add, remove , setSelected.....) then inject any service i need in any controller i need. why in that case i should maintain caching mechanism ? – Amir_Af Jul 18 '16 at 14:55
  • -> why in that case i should maintain caching mechanism ? To lower number of requests to server about big set of data. Actually it's much more simple to use `Factory` and don't bother with `$get` etc. things. – Appeiron Jul 18 '16 at 15:05
  • The service will call only for first time. once data is returned Its getting hold by service "this.results = response" and the service expose function to get the results , add , remove and refresh function to fetch again the data on demand.... this is common approach but trying to understand if correct one. – Amir_Af Jul 18 '16 at 15:08
  • 1
    Sure, you can store data right under your `Factory` or `Service`, but imagine that you need to clear all cache - you will need to write `clear`. If you need to look how many results - `info` etc. It is just already included in `$cacheFactory`. – Appeiron Jul 18 '16 at 15:11
  • Thanks Appeiron. That's the answer I was looking for . In my case "clear" is not required but it definitely something I should think about. Thanks again. – Amir_Af Jul 18 '16 at 15:15
  • Will move to answer after structure consolidated one. – Appeiron Jul 18 '16 at 15:24

2 Answers2

2

AngularJS has a $cacheFactory service you can use to manage data. In the example I linked to, they create a 'service' using the $cacheFactory. I've done something similar in an app I've created where I made a 'caching service' that has methods that I expose to add/remove items from a $cacheFactory cache (data I retrieved over $http calls).

You didn't give a lot of detail to go on, but I would say that generally speaking, it's a good practice to use a service to hold the cached data.

EDIT

You can also look into using $resource, which has built in support for caching. Not sure what your app specifically requires, but if you feel you need to maintain the cache yourself, use a service. Having each service maintain it's own caching logic is more likely to be an issue later if something changes.

Matt M
  • 3,699
  • 5
  • 48
  • 76
  • The reason why I'm thinking using services as the data holder is becuase most of business logic will be there so in case I need to work on this data I just work with the service and not with the cache. – Amir_Af Jul 18 '16 at 14:06
  • Not sure if this is what you mean or not, but I would suggest creating a service devoted specifically to caching and then inject that into your other services and/or controllers. – Matt M Jul 18 '16 at 14:10
  • The question is why ? If each service has it own business logic , then its not better to keep the relevant information in that service ? why to move it to dedicated service ? – Amir_Af Jul 18 '16 at 14:16
-2

Yes Service is best for holding your data or sharing data between controllers.

Thanks

Akl
  • 1
  • 2