Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do services go in MVC? [closed]

I've asked this several developers, and gotten different answers every time.

Let's say I'm working in an MVC framework and I have a class called validator. Say this object has a bunch of methods which can be used to tell you if an email or phone number is valid, or if a given value actually has content in it.

Say I want to make this service a property of a model that I'm creating. I can simply inject it into the construction method of my model class. However, where does this service fit in in MVC? Is it a model?

Where should the file be stored? With the models? In it's own directory, perhaps called services?

like image 452
Allenph Avatar asked Jul 26 '16 00:07

Allenph


People also ask

Where do Services go in MVC?

These could go in the Controller layer. They can also enforce business rules like saying if "a phone number is valid". These should go in the Model layer.

What does service do in MVC?

A service layer is an additional layer in an ASP.NET MVC application that mediates communication between a controller and repository layer. The service layer contains business logic. In particular, it contains validation logic.

Where does business logic go in MVC?

A1: Business Logic goes to Model part in MVC . Role of Model is to contain data and business logic. Controller on the other hand is responsible to receive user input and decide what to do.

Where is business logic stored?

The business logic should be placed in the model, and we should be aiming for fat models and skinny controllers. As a start point, we should start from the controller logic. For example: on update, your controller should direct your code to the method/service that delivers your changes to the model.


2 Answers

I think I have a different view [sadly no pun was intended] of what the Model is in mvc, but services should definitely go in the Model layer.

First, a model shouldn't be a class. The model is a model, of one application. An application is modelized in different things (contained in the Model layer): Entities, Mappers, Services.

For exemple, that could be a file hierarchy representing this concept:

application
    Controller
    Model
        Entities
        Mappers
        Services
    View

Say I want to make this service a property of a model that I'm creating. I can simply inject it into the construction method of my model class. However, where does this service fit in in MVC? Is it a model?

I'll assume "model" as you say it is indeed an Entity, an object representing a domain concept. In that case, a service should not be a property of an entity. Services should be used by Controllers, to do whatever they are called to do, then Mappers would construct your Entities from the result of what the Services did.


My current understanding vastly comes from this answer, you should definitely read it for further understanding.

like image 177
Félix Adriyel Gagnon-Grenier Avatar answered Sep 25 '22 10:09

Félix Adriyel Gagnon-Grenier


It depends on what the service's responsibility is. You can have different types of services in your application. e.g.

  • A service that handles user input and returns an instantiated model class that will output something to be rendered by the view (should probably live in the controller layer);
  • A service that transfers money between accounts in a bank application (should live in the model layer), etc.

Let's say I'm working in an MVC framework and I have a class called validator. Say this object has a bunch of methods which can be used to tell you if an email or phone number is valid, or if a given value actually has content in it.

First things first. You don't want these super powerful classes since they are hard to maintain. They break the Single Responsibility Principle (see https://en.wikipedia.org/wiki/Single_responsibility_principle).

Validators can have different duties in your application.

  • They can just help the user telling if "a given value actually has content in it". These could go in the Controller layer.
  • They can also enforce business rules like saying if "a phone number is valid". These should go in the Model layer.

Say I want to make this service a property of a model that I'm creating...I can simply inject it into the construction method of my model class.

It might be better to validate first and then inject the real class dependencies like phone number and email. OOP is about abstracting real life concepts: a Person has a valid phone number and a valid e-mail, not a validator.

Where should the file be stored? With the models? In it's own directory, perhaps called services?

Services should be created in their own namespaces like 'ProjectName\Model\Service' and 'ProjectName\Controller\Service'.

Also, there's a lot of articles and threads covering MVC downfalls on building whole applications (like https://softwareengineering.stackexchange.com/questions/207620/what-are-the-downfalls-of-mvc) that are worth reading.

like image 35
Renan Taranto Avatar answered Sep 21 '22 10:09

Renan Taranto