Zend Action Controller-重构策略 [英] Zend Action Controller - refactoring strategy

查看:80
本文介绍了Zend Action Controller-重构策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Zend Framework(1.10)上构建了一个首次运行的Web服务,现在我正在寻找在我的动作控制器中重构某些逻辑的方法,以便对我和其他人来说更容易我的团队来扩展和维护服务.

I've built a first-run web service on Zend Framework (1.10), and now I'm looking at ways to refactor some of the logic in my Action Controllers so that it will be easier for me and the rest of my team to expand and maintain the service.

我可以看到哪里有重构的机会,但是我不清楚如何进行重构的最佳策略.关于控制器的最佳文档和教程仅讨论小型应用程序,而没有真正讨论如何抽象出越来越多的重复性代码以扩展到更大的规模.

I can see where there are opportunities for refactoring, but I'm not clear on the best strategies on how. The best documentation and tutorials on controllers only talk about small scale applications, and don't really discuss how to abstract the more repetitive code that creeps into larger scales.

我们的动作控制器的基本结构为:

The basic structure for our action controllers are:

  1. 从请求正文中提取XML消息-这包括针对特定于操作的RelaxNG模式进行验证
  2. 准备XML响应
  3. 验证请求消息中的数据(无效数据会引发异常-将消息添加到响应中并立即发送)
  4. 执行数据库操作(选择/插入/更新/删除)
  5. 返回成功或失败的操作,并提供必要的信息

此操作是一个简单的示例,该操作根据一组灵活的条件返回供应商列表:

A simple example is this action which returns a list of vendors based on a flexible set of criteria:

class Api_VendorController extends Lib_Controller_Action
{  
    public function getDetailsAction()
    {
        try {
            $request = new Lib_XML_Request('1.0');
            $request->load($this->getRequest()->getRawBody(), dirname(__FILE__) . '/../resources/xml/relaxng/vendor/getDetails.xml');
        } catch (Lib_XML_Request_Exception $e) {
            // Log exception, if logger available
            if ($log = $this->getLog()) {
                $log->warn('API/Vendor/getDetails: Error validating incoming request message', $e);
            }

            // Elevate as general error
            throw new Zend_Controller_Action_Exception($e->getMessage(), 400);
        }

        $response = new Lib_XML_Response('API/vendor/getDetails');

        try {
            $criteria = array();
            $fields = $request->getElementsByTagName('field');
            for ($i = 0; $i < $fields->length; $i++) {
                $name = trim($fields->item($i)->attributes->getNamedItem('name')->nodeValue);
                if (!isset($criteria[$name])) {
                    $criteria[$name] = array();
                }
                $criteria[$name][] = trim($fields->item($i)->childNodes->item(0)->nodeValue);
            }

            $vendors = $this->_mappers['vendor']->find($criteria);
            if (count($vendors) < 1) {
                throw new Api_VendorController_Exception('Could not find any vendors matching your criteria');
            }

            $response->append('success');
            foreach ($vendors as $vendor) {
                $v = $vendor->toArray();
                $response->append('vendor', $v);
            }

        } catch (Api_VendorController_Exception $e) {
            // Send failure message
            $error = $response->append('error');
            $response->appendChild($error, 'message', $e->getMessage());

            // Log exception, if logger available
            if ($log = $this->getLog()) {
                $log->warn('API/Account/GetDetails: ' . $e->getMessage(), $e);
            }
        }

        echo $response->save();
    }
}

所以-知道我的控制器中的共同点,什么是重构同时使其保持Zend样并可以用PHPUnit测试的最佳策略是什么?

So - knowing where the commonalities are in my controllers, what's the best strategy for refactoring while keeping it Zend-like and also testable with PHPUnit?

我确实考虑过将更多的控制器逻辑抽象到父类(Lib_Controller_Action)中,但这使单元测试变得更加复杂,在我看来这是错误的.

I did think about abstracting more of the controller logic into a parent class (Lib_Controller_Action), but this makes unit testing more complicated in a way that seems to me to be wrong.

推荐答案

两个想法(仅根据上面的评论创建答案):

Two ideas (just creating an answer from the comments above):

  1. 将通用性推入服务/存储库类中?这样的类将是可测试的,可以在控制器之间使用,并且可以使控制器代码更紧凑.

  1. Push commonality down into service/repository classes? Such classes would be testable, would be usable across controllers, and could make controller code more compact.

将通用性融入行动帮助者中.

Gather commonality into action helpers.

这篇关于Zend Action Controller-重构策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆