4 ms·
The best practices I've seen for MVC apps isn't to put all the logic in the controllers. What you do is to create a "services" or "managers" layer that is calle
by guelo 14y ago
The best practices I've seen for MVC apps isn't to put all the logic in the controllers. What you do is to create a "services" or "managers" layer that is called on by the controllers. A userService, for example, might have a function
user = userService.login(name, password)
It's also nice to abstract this service layer with some clean interfaces so that you can replace the underlying implementation, for example to move from MySQL to Memcached.
Using events for everything has appeal but my experience is that the added synchronicity problems creates unneeded complexity and makes reasoning through the logic and debugging hard.
- bradwestness 14y agoYeah, I've always used MVC with a repository pattern for handling Model operations. The controller is still calling the operations here, and handling events triggered by the user. The O and E in MOVE sounds effectively the same as the C in MVC. Model objects are generally just POCOs (or POJOs, or whatever) and should not know about their storage mechanism or dealing with user events. Sounds like he's basically been doing MVC wrong, and thinks MOVE is something new, when really it's just closer to the way MVC should be done in the first place.
- rbanffy 14y agoI prefer to put anything ORM-related inside the model. This way, I free views and controllers from the specifics of the platform and, as long as I keep the model API the same, I can move from one platform to another easily. If userService.login method knows how to check a User's password, it's probably inextricably tied to the model and, therefore, part of it. Even if it's on a different module, it cannot be reused with a completely different model.
- jorgenhorstink 14y agoI also prefer this approach. The excellent book 'Domain Driven Design - Tackling Complexity in the Heart of Software' by Eric Evans has helped me a lot. I'd write something like: <?php class AuthController extends Controller { public function __construct(Request $request) { $this->service = AuthService::getInstance(); } // reflection calls a Module + Action attribute to {module}Controller public function login(Request $request) { if ($request->getRequestMethod() === WebRequest::POST) { if ($this->service->login($request->getAttribute('username'), $request->getAttribute('response')) { die('{ "success" : true }'); } else { die('{ "success" : false }'); } } } } class AuthService { // Application Service -> it just defines a clear API, not a Domain Driven Design service public function __construct(Session $session) { $this->session = $session; } public function login($username, $response) { try { $user = User::getRepository()->getByUsername($username); return $user->isValidChallengeResponse($this->session->getAttribute('challenge', 'auth'), $response); } catch (UserDoesNotExistException $e) { } return false; } } class User { protected static $repository; public static function setRepository(IUserRepository $repository) { self::$repository = $repository; } public function getRepository() { return self::$repository; } // yeah yeah, it is arguably if this belongs as a Domain method of the user… public function isValidChallengeResponse($challenge, $response) { // and yes, this is very weak challenge response… return md5($challenge . $this->getPassword()) === $response; } } ?> If anyone can tell me what's wrong with this type of MVC, except it isn't using an actual View in this limited example, I'd love to hear it. Controllers should be thin, models and services should be thick.
- juliano_q 14y agoI agree, I really like the "services" layer. I used it for years programming with Java/Spring and missed a lot when learning RoR. I think it is no coincidence that the two RoR real-world projects that I worked had enormous controllers. The service layer is great to orchestrate operations between models and I don´t know a reason for not using it.
- Daniel_Newby 14y agoDoes anything stop you from using a service layer with Rails?
- juliano_q 14y agoNo, but it doesn't seem to be a common practice. Actually on my personal projects the first thing I do is to create a service layer, but sadly the real world projects that I jumped into never had it.
- jacobsteringa 14y agoI believe this gem does something like that, https://github.com/karmajunkie/imperator https://github.com/karmajunkie/imperator
- einhverfr 14y agoOne thing I have started to note is that MVC is a fractal pattern. For a non-trivial app, you can define almost every component as an MVC framework. Not only is there an MVC on the web server and a MVC in the web browser, but there is an MVC in the db as well, if it is well designed and well encapsulated. For example a well normalized db may have a model (physical table structure), a view (logical table structure) and controller logic (which essentially maps these two together and may enforce other things like audit trails and app-level replication).