15

Why in some frameworks the logic layer is called "Model" whereas in some it is called "Service". Are they different from each other or just different by naming conventions?


UPDATE 1

The reason I'm asking is because in Zend Framework, a classical MVC framework, everybody uses the concept of Model. Now I'm learning AngularJS and it seems that the word Model disappeared and was replaced by the word service.

What I noticed is that a service is more like a singleton that can be reused again and again (example: a REST client) whereas a model is more related to the data manipulations coming from the controller in the MVC pattern.

  • Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Feb 25 '14 at 17:53
  • To paraphrase Shakespeare: what's in a name, a rose by any other name is still a rose. Your application model may well be implemented as a service. – jwenting Feb 28 '14 at 9:11
21

Model: Fields that belong to the object, methods that help to get/set data from the object (a fullname accessor that returns first + last name)

Service: Methods to perform operations with one or more models, see 'unit of work', transactions, etc...


Employee::create should just take a set of data, perform model validation if necessary, and return an Employee Object.

EmployeeService::hireEmployee might create the employee, send them a welcome email, create a mailbox, make them a sandwich, etc... it may return the set of data, or a result code, etc...


This can also affect validation:

Model Validation: Employee must have a id, first and last name, and birthday

Service Validation: Employees for the bartender position must be 21 or over, and approved by a manager.

3

According to my experience, the Model layer within the MVC design pattern refers to every software component involved with data manipulation (POJOs, DAO, all the way to SQL, JDBC, and so on).

Whereas the service layer is actually an addition to MVC:

We know that the Model layer components are invoked inside the Controller layer. Once the latter is built, you come to realize that it doesn't look concise (messy with dirty code); the Controller may not afford extra details (e.g. formatting request parameters before calling a DAO method that are going to consume them ...). Therefore, you might include this extra layer, namely the Service layer.

Eventually, you may encompass your dirty code inside static methods with a meaningful name, parameters and so forth, which would outcome a synthetic Controller layer.

Take a look at this link:

https://stackoverflow.com/questions/2762978/the-purpose-of-a-service-layer-and-asp-net-mvc-2

  • 1
    This is exactly how I see the service layer also. But then, more often than not, I see it being used to serve external API into the business model. Both scenarios I see valid. The issue here is just in the clash of nomenclature. – burntblark Nov 7 '16 at 7:03
2

Structurally these base classes are the same, however they are used to classify a different concerns of the Service and Model tiers of the MVCS implementation

Service:- A concrete service class defines the API of an external Service.

Model :- Defines the API of the applications data model.

So, whilst the base classes are alike, the concrete classes created by extending these base classes serve two entirely different purposes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.