Practices - Do you use a Service to share reusable logic?
Rules to Better Angular|ebef3f5e-e218-4cd3-845b-6507fff9eb61
v2.0
Posted at
2/11/2018 8:36 AM by
Tiago Araujo
Rule Intro
A Service is a singleton object with a lifetime scope of the application. It is instantiated only once during the lifetime of an application. When combined with Angular’s Dependency Injection, a service can be injected into any component in an application via Constructor Injection. This makes a Service perfect for sharing reusable functionality throughout an application.
Page Content
A common example of when you’d use a Service is when you want to retrieve data from a WebApi using the HttpClient. There may be several places in your application where you need to retrieve the same list of data. By placing this functionality in a Service it gets rid of the duplicated code in the components that make the WebApi call.

- Figure: Bad Example - Code that is reusable should be placed in a Service

- Figure: Good Example - Reusable code is placed in a Service and the component calls the Service
{F9F41249-C8C4-4D11-A75A-79D397810FF1}
Do you feel this rule needs an update?