DEV Community

Raounak Sharma
Raounak Sharma

Posted on • Updated on

Service object | Shots #1


What is it?

Let's talk in simple words, services are just a thing which serves a purpose. Now before saying that it is rubbish just hold on with me.

Serves the purpose in making software means that it do something for a class that the class itself is not meant to do (following single responsibility principle). Now that class can be anything in any framework for example model class in Rails and component class in Angular.

Why we need them?

Actually, we only need them in case of:

  • If the same logic is used in more than one place. And our ability to modify the same thing over multiple places is not too good.
  • If the app is ever going be changed or modified. I think it will be.

So if our code is ever going to be modified/expanded and we do want to follow DRY and SR principles, then I guess we need to do it.

How we do them?

A service is nothing, but just another class. Also doing something around a single responsibility.
Example: A class handling calling to Twitter API.

Now make an object of this class in whatever class you required and use it to do its things while the class in which you called it will only use the output of this service object.

Now, how you can actually do them in different languages/framework is your next search, I just gave you a reason to search.

Discussion (0)