0

I wonder what is your approach on following subject:

let's say we have MVC structure with entities, repositories, controllers and service layer.

My approach to use above was pretty clear for me:

  1. controller uses services
  2. services perform manipulations to data when needed,
  3. services use repository classess to save and update data from db
  4. controller gets services output (when needed) and pushes it to view

However, yesterday I stumbled upon solution popular in Symfony2 world, in which

  1. controller has access to entityManager/sessionFactory
  2. controller uses services to perform manipulations to data
  3. controller uses entityManager/sessionFactory and repositories directly to perform database reads, saves and handle transactions.

At first I disagreed with second approach, I know it breaks some clean architecture principles, but... it is shown in a lot of official tutorials on Symfony2 website. So I thought it would be better to ask.

In other words: is it ok to make controller directly responsible for saving and retrieving data as well as handling transactions? What are ups and downs?

1 Answer 1

2

There's a risk that the second approach leads to a Fat Controller, which is often considered an antipattern. It's effectively a violation of "clean architecture" (as by Uncle Bob Martin) since your controller is now not only a "delivery mechanism" but also deals with applicative transactions/use cases, orchestrating calls to the repositories and services and making decisions based on current state of the application.

Drawbacks are on readability, bug proneness, maintainability and testability since UI logic is now intertwined with applicative logic. It can also favor code duplication (in contrast, code in a service can be reused by multiple controllers) and if you choose to add another UI layer (say, mobile) to your architecture, the use cases will have to be copied there. Same if you want to add a UI-less HTTP API.

The main advantage is simplicity. With a small CRUD application where each HTTP request results in the same basic steps (load entity, update/delete/display it, [save entity]), you don't necessarily need this layer of indirection. Applicative logic can even be factored out to a base controller class with template methods.

2
  • Thanks for your input, you basically confirmed my point of view. But I still wonder - why such a big company like SensioLabs encourage people to use this antipattern? Example: symfony.com/doc/current/book/…
    – ex3v
    Nov 19, 2014 at 10:47
  • 2
    I don't know Symfony well, but I suppose it's frequently the case with all-encompassing, "your app in one day" web frameworks such as Rails and the like, where productivity is put forward before maintainability. Nov 19, 2014 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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