duanba7498 2011-05-02 21:09
浏览 46

在哪里使用服务层在我的分层应用程序中抛出异常?

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 should 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?

  • 写回答

1条回答 默认 最新

  • douou9786 2011-07-05 09:31
    关注

    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).

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题