6

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
Allenph
  • 1,875
  • 27
  • 46
  • Welcome to Stack Overflow! I edited your question as far as I could guess your problem. However, add code and description so that more people with knowledge of the subject will see it. Please edit in the specific error-message you're encountering in case that's necessary to identify the specific problem. Good Luck! – Enamul Hassan Jul 26 '16 at 02:28
  • No, the task of the model classes only the (mostly persistent) data structure manipulations, validation is one of the most clear controller jobs. Of course a validator can make calls to the model, and in most practical cases it does it, but their place is on the controller side. If there is no particular support in the actually used framework, then in your case I would use them as helper classes for the validator. Another little remark (sorry sorry PHP programmers): to learn OOP and software architecture deeply, maybe PHP is not the best option. Java is, EJB+JSF is imho the best. – peterh Jul 28 '16 at 02:51
  • 1
    Ewww, Java. Second what you're describing is a fat controller. Models should contain the business logic, including validation, if I understand correctly. It's not very DRY to put repeated logic in controllers, and seems to defeat the purpose of MVC. – Allenph Jul 28 '16 at 07:18
  • 1
    @peterh complete disagreement here. Controllers should *control* other instances to do the job. The validation code has nothing to do in a controller. Sure, validators would make calls to the model, *they are actually part of it.* They are being passed data and validate it according to domain rules. Also oop is language agnostic. Php is perfectly fine to understand it's principles and has all the key constructs to be properly used. – Félix Adriyel Gagnon-Grenier Jul 30 '16 at 16:28

3 Answers3

6

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.

Community
  • 1
  • 1
2

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.

Community
  • 1
  • 1
Renan Taranto
  • 562
  • 4
  • 8
0

It`s clearly part of model layer, because it contains business logic. My validators are stored in /models/validators. And can be used inside of Forms/APIs/etc ...

pilec
  • 493
  • 4
  • 9