Practices - Do you use a Service to share reusable logic?

Updated by TiagoAraujo 6 years ago. See history

123
<introEmbed body={<> 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. </>} />

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.

Image

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

Image

✅ Figure: Good Example - Reusable code is placed in a Service and the component calls the Service

Acknowledgements

Barry Sanders
Brendan Richards
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star