by guelo · 14y ago · 0 comments
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.
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.
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.
<?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.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).