开发者

Where to throw Exception in my layered application using a Service Layer?

I'm wondering where I should throw, catch exception in my Zend Framework + Doctrine 2 MVC application.

Here is my design:

Database - MySQL > ORM (Doctrine2) > Service > Controller

My Service take service object in argument and return Service response object for almost all methods.

Service response returns a status, messages and the data which can be whatever you want.

My controllers consumes those services.

I'm wondering where shou开发者_JS百科ld I throw my exception.

Example:

public function getAllMembers(ServiceObject $data)
{
  // do some mapping with $data
  $users = $userRepository->getAllMembers($data);
  $response = new ServiceResponse('success');
  $response->setData($users);

  return $response;
}

Should I check for parameters in my Service, and then throw an exception which my controller can catch, or should I throw in, let's say my repository for this example, and catch in my service to allow me to return a ServiceResponse with an error status?

Any feedback about this kind of architecture?


I usually through my exceptions as deep into the code as possible (in this case, it would be inside the repository), and then catch them wherever it would be most appropriate. In this case, I think the most appropriate thing to do would be catch it in the Service and then return a ServiceResponse with an error status (otherwise you'd only be returning success messages, which would make the ServiceResponse kind of redundant).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜