What is the difference between service and utility classes?

Asked 1 years ago, Updated 1 years ago, 296 views

I read Hidenori GOTO's answer to the following questions.It was explained that the service was like this.

What is the role of the service in the MVC model

However, if you look at this much, you can't tell the difference from the utility class.

Wikipedia's Utility said it meant things like "useful," "useful," "utility," and "public interest."What is the difference between a class (utility class) that brings together useful things in a certain way and a class (service class) that brings together common processing from a certain perspective?

  • Use the common actions described in the controller to summarize them from some point of view
  • Use to summarize the common actions described in the Model from some perspective
  • Use to summarize common actions needed for templates in some way

mvc

2022-09-30 21:58

2 Answers

This is a very difficult question to answer.

The context you are speaking, the framework you are dealing with, and the speaker will change your intentions.

It's been more than 10 years since I first heard the word service in a relatively timely manner, because I was also very confused at work.

First of all, the service that I understood is the scene that was introduced in the following article (it was only when I started to understand it somehow).

Kingdom of Nouns - Innocent Story

As a naming convention for most object-oriented,
class name is the noun and method name is the verb
This is a common understanding among programmers.

However, depending on the situation, you may want the verb to be the class name as shown in this article (there is something you would like to do).It says that, but I have hardly thought so myself, and I have experienced many projects written in that school, and I feel that there are many projects like that.However, it seems that there are many advantages such as making mock objects easier.)

In such situations, the class name (verb + object + service or verb + service verb + service) such as GetHogeService (classes that get Hoge) PostHogeService or (code>).(The article is an example of using verbs in the class name and having execute in the method name. This article is not a naming convention of verb + Service, so this is just a reference for that situation.)

By the way, there are cases where a manager class called HogeManager is introduced in similar situations.In my sense, it is rare for me to use the service within the project when I introduce a manager class.A manager class often has multiple associated methods.

There is a concept called architecture.
There are many different ways to view the architecture.
MVC is one of the architectures, but there are other things like DDD, IDDD, PofEAA, and clean architecture.

It's hard to say in a word what it's like to think about adding these architectures to MVC, or to think of them as a different architecture and create a system.

By the way, there were many stories about MVC and MVC2 in the past.
For example, when I searched now, the following article became a hit.

DDD or IDDD or PofEAA

In DDD, service generally refers to domain service.
Now, what is Domain Services, but I'm afraid I'll have to explain the Domain Layer of DDD first, so I won't be able to write much because I'm afraid I'll leave the answer to this question.
However, all I can say is that this domain service is still
in the project I've experienced. It was named verb + Service and had only one method
inside. (So far, I've written only one method, but this is just what I've experienced, not necessarily only one method).

By the way, I also experienced that DDD has only one method in the class name Verb + UseCase.This is about the use case layer, so everything doesn't seem to be a verb + Service naming convention just because you want the class name to be a verb.

In addition, DDD is also known as IDDD.

Domain-Driven Design: Summarized the Module configuration of the sample code that appears in IDDD

as shown in the . https://github.com/VaughnVernon/IDDD_Samples
There is a sample code in , so you may want to see how it is called a service here.This is an example of DDD, but there should be a way to incorporate it into the MVC and use it.

As part of the DDD conversation, project participants may be talking about PofEAA services, as they may be building an architecture based on PofEAA services.Some references included the following:About the concept of confusing services

Rails

Rails (which is also considered the framework of MVC), and so on, what is referred to as Service is called Rails (which means Active Record), and so on, CreateHogeServie services.(It is often said in the context of the design to make it that way so that the refactoring context or model does not become bloated, but I think that just the place of writing has changed in the first place is not fundamentally different from the bloated model.)

Also, if this is the case, the Rails example may not be enough, so the famous
https://github.com/gitlabhq/gitlabhq
There is also a class named Service in the code, so that might be helpful (I only read this code a little bit, so I can't write too many of them because I might be wrong enough to explain where the service is.I don't think it's just because the inside of the model is bloated...)
compare_service.rb
or
cohorts_service.rb
You have the execute method.

ban_service.rb
You don't seem to have anything, but the parent class is
banned_user_base_service.rb
Therefore, you may have the execute method.

In addition, After reading and examining DDD and PofEAA for the Service Class at dev.to, you may find examples of Rails in relation to DDD and PofEAA.

There was also an article like this:I'm sorry.I admit my mistake honestly, so please stop making class of service

Spring

The Spring Framework in Java is the framework of MVC, but it has the attribute @Service and refers to this attribute or class using it ().It is often made of >, so depending on the context, speakers may be talking about domain services as mentioned above.)
To make matters more complicated,
What are the differences between @Component, @Service, @Repository, and @Controller?-Qiita

as described in the following paragraphs

Spring annotations @Component, @Service, @Repository, @Controller are basically the same as the movement

Components, Services, Repositories, and Controllers are basically the same for the attribute of the Spring Framework, and are spoken among Spring Framework speakers in the context of how components, services, repositories, and controllers differ and use differently.
(By the way, when I use Spring, I use the service as a substitute for use cases in other contexts, but it may be just my own way, and there may be others who do the same.)

CakePHP

I have no experience with CakePHP, but one of the answer that you are referring to is

To simply answer the question, CakePHP has Component in the Service layer.

I came across the following entry written by CakePHP core developer Jose when I had the same question.

or

Although there is no definition that clearly separates MVC model from service layer, CakePHP makes implementation as Component refreshing.

So, maybe it's a little different from the school I know (as I mentioned, in the Spring framework, components and services are basically the same, but I can also think of them as different).Or rather, it should be easier to read and write code.) (My understanding may be wrong because I have no experience with CakePHP.)
This way, context and frameworks change the way you view them.

Android

To tell you a different story (maybe no longer in the context of MVC or architecture), Android has service, and when I hear the word service in MVC, I think it's DDD's service, and I don't give examples of different usage.

The utility doesn't seem to be used very actively (?) and is often used as a place to store functions and constants that are conveniently used in common throughout the project.The utility may be named Utility, or it may be just a folder named Utility.It is used to import related basic projects, and this basic project is sometimes referred to as a utility.
However, this is also about what functions are common and conveniently used, but it depends on the site.

From an architectural perspective, the following article was published:
Aiming for sustainable development~Domain use case driven (clean architecture) + unidirectional restricted processing+FRP

utility —For example, a library that can be used in any layer.Example: FRP Library

There is an idea of a layered architecture (or another architecture like a layered architecture), so it is explained that it can be used in any layer (layer.
"To put it bluntly, it is ""something that is conveniently used in common throughout the project."""

DDD, PofEAA, and Spring have different meanings, so I recommend that you deepen each word with your own understanding.

In the first place, MVC should be divided into Model, View, and Controller, so I think service and utility are different concepts.This is an image that puts DDD and other ideas into MVC.

No matter how much I looked into this question, I guessed that what you said was different depending on the article on the Internet, and I thought it might be helpful.

In the end, I'll write it down to this point, and I'll answer the questioner's reference

"Service" is confusing as a term, so let's understand it from the point of view

So I may have just written a similar meaning.
My impression is that when the speaker's sense of speech is taken in a different way, the conversation seems to have somehow been in harmony at the beginning of the conversation, but the conversation gets caught up in a whirlpool of confusion.On the other hand, if there is a relatively good context (prerequisite/context for speaker feeling), I feel that conversation often goes smoothly.


2022-09-30 21:58

My answer is very simple.

Utility classes consist of only static elements and have no state.If you pass the same value and run the utility class method, the return value is essentially the same.To put it bluntly, the method in the utility class is a function.

Conversely, the service can also have a state.It is responsible for undertaking specific operations on behalf of other classes and for facilitating connectivity between classes and programs.

In practice, utility classes can run about three times faster than services.
This depends on the language, because the utility class is static and only one entity is loaded into memory as soon as the program starts, while services are dynamically generated as needed.

However, these are one of the many ways of thinking, so it may vary greatly from community to community.For your information only.


2022-09-30 21:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.