简体   繁体   English

ZF2:Zend DB,TableGateway,服务策略

[英]ZF2: Zend DB, TableGateway, Services Strategies

I have created a little Zend Framework 2 module called CMS to write simple articles in my site. 我创建了一个名为CMS的Zend Framework 2小模块,以在我的站点中编写简单的文章。 This module follow uses Zend Db and the TableGateway class in order to get the data from the database. 接下来的模块使用Zend DbTableGateway类,以便从数据库中获取数据。

I have read many website and books about the strategies to create a module and I prefer the short and fast way using these file structure: 我已经阅读了许多有关创建模块策略的网站和书籍,我更喜欢使用这些文件结构的快捷方式:

.
├── Module.php
├── config
│   └── module.config.php
├── data
│   └── data.sql
├── src
│   └── Cms
│       ├── Controller
│       │   ├── IndexController.php
│       │   ├── PageAdminController.php
│       │   └── PageCategoryAdminController.php
│       ├── Form
│       │   ├── Element
│       │   │   ├── PageCategories.php
│       │   │   └── ParentPages.php
│       │   ├── PageCategoryFilter.php
│       │   ├── PageCategoryForm.php
│       │   ├── PageFilter.php
│       │   └── PageForm.php
│       ├── Hydrator
│       │   └── Strategy
│       │       └── DateTimeStrategy.php
│       ├── Model
│       │   ├── Page.php
│       │   ├── PageCategory.php
│       │   ├── PageCategoryTable.php
│       │   ├── PageTable.php
│       │   └── UrlRewrites.php
│       └── View
│           └── Helper
│               ├── Extract.php
│               └── Tags.php
└── view
    └── cms
        ├── index
        │   ├── index.phtml
        │   ├── notfound.phtml
        │   └── page.phtml
        ├── page-admin
        │   ├── edit.phtml
        │   └── index.phtml
        ├── page-category-admin
        │   ├── edit.phtml
        │   └── index.phtml
        └── partial
            └── tags.phtml

TableGateway Method TableGateway方法

This file structure allows me to declare, for instance, the Page and PageTable class in the module.php and call the ServiceLocator to read and write the records from the database in this way: 这种文件结构使我可以在module.php中声明Page和PageTable类,并调用ServiceLocator以这种方式从数据库读取和写入记录:

$pageTable = $this->getServiceLocator()->get('PageTable');

In this case I can use this class to write the CRUD methods in the PageTable Class. 在这种情况下,我可以使用此类在PageTable类中编写CRUD方法。


Inject Service Method 注入服务方法

Then I have seen that there is a Service way to do the same thing where the CRUD actions are located in a Service Class that calls the TableGateway Class and inject by a Factory Class the Service into the __construct method of the Controller . 然后,我看到有一种Service方法可以执行相同的操作,其中CRUD操作位于Service类中该类调用TableGateway类 ,并由Factory类服务注入到Controller的__construct方法中。

Service > TableGateway > Factory > Controller 服务 > TableGateway > 工厂 > 控制器

Why have I to choose the Service strategy instead of the simple TableGateway? 为什么我要选择服务策略而不是简单的TableGateway?

Well your logic is a little off, the way is actually only 好吧,你的逻辑有点偏离,方法实际上只是

Controller calls Service calls TableGateway 控制器调用服务调用TableGateway

The Factory is just a pattern to properly inject the dependencies. Factory只是正确注入依赖项的一种模式。

Why use the Service 为什么要使用服务

To abstract the behavior. 抽象行为。 The Service generally speaking is the instrument for your controllers to get the data. 一般而言,该服务是控制器获取数据的工具。 The Service then interacts with a data-source. 然后,服务与数据源进行交互。 What the data-source is, your controller doesn't care - hell not even your service should care. 数据源是什么,您的控制器不在乎-甚至您的服务都不在乎。 The service should only care for an implementation of an interface. 该服务应仅在意接口的实现。 This way, whenever you feel that you don't like TableGateway anymore but you wanna go Doctrine2, you don't have to change your Service. 这样,每当您觉得自己不再喜欢TableGateway但想要使用Doctrine2时,就不必更改服务。 You don't have to change you Controller. 您不必更改控制器。 All you have to change is the dependency of your Service. 您只需更改服务的依赖项即可。 Instead of injecting a TableGateway class you inject your Doctrine2 class which matches the interface of the data-provider-dependency. 而不是注入TableGateway类,而是注入与数据提供者相关性的接口匹配的Doctrine2类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM