2015-07-05 29 views
0

我目前正在開發一個個人項目,並實施了服務層。我更喜歡獨立存儲服務,所以我沒有一個文件內的大型圖書館。下面是否需要通過創建一個「容器」來抽象服務

services/ 
     user/ 
      authentication 
      login 
      logout 
      registration 
     news/ 
      articles 
      article 

文件結構的簡單的例子,我知道誰實施userService類,將是我現在有一羣一切幾個人。我更喜歡我的方法,以節省時間在未來的編輯+我有很多的用戶服務/功能,所以它最好保持分開。我最近建議在我的服務文件夾的根目錄中實現一個userService類,並使用它來調用/執行應用程序內所需的服務。下面是我的例子/理解

<?php 

/** 
*----------------------------------------------------------------- 
* 
* USER SERVICE CLASS 
* 
* Simplifies Service Usage Within Application 
* 
*/ 
namespace Service; 
use \Helper\ServiceAccess; 

class UserService extends ServiceAccess { 

    // Define Service Prefix Key 
    protected $prefix = 'user/'; 
} 

/////////////////////////////////////////////////////////////////// 
// Separate File (Helper Function) 
/////////////////////////////////////////////////////////////////// 

/** 
*----------------------------------------------------------------- 
* 
* SERVICE ACCESS LAYER 
* 
* Used to Simplify the Process of Executing Services, and Grouping 
* Alerts For Simple Front-end Error Messages 
* 
*/ 
namespace Helper; 

class ServiceAccess extends Base { 
    public $dependencies = ['factory']; 

    // Default Service Prefix 
    protected $prefix = ''; 

    // Alert Container Used By Controller to Set UI Alerts 
    protected $alerts = [ 
     'errors' => [], 
     'info'  => [], 
     'success' => [], 
     'warning' => [] 
    ]; 


    /** 
    * Service Execution Method 
    * 
    * Used Within Parent Service Classes Such as   UserServices 
    *              TournamentServices 
    *              etc. 
    * 
    * @param string $key   Refers to the Factory Key (Excluding Prefix) 
    * @param mixed $input  Any Type of Input to Be Passed to Execute Method of Child Service 
    */ 
    public function execute($key, $input = []) { 

     // Create Service Class Via Factory - Call Execute Method Within Service 
     $service = $this->factory->make($this->prefix . $key); 
     $execute = $service->execute($input); 

     // Get & Merge Alerts From Service 
     $this->setAlerts($service); 

     // Return Result From Service Execution 
     return $execute; 
    } 


    /** 
    * Set Alerts 
    * 
    * @param array $alerts  Front-End User Alerts Defined By Services 
    */ 
    private function setAlerts($service) { 
     $this->alerts = [ 
      'errors' => array_merge($this->alerts['errors'],  (array) $service->get('errors')), 
      'info'  => array_merge($this->alerts['info'],  (array) $service->get('info')), 
      'success' => array_merge($this->alerts['success'], (array) $service->get('success')), 
      'warning' => array_merge($this->alerts['warning'], (array) $service->get('warning')) 
     ]; 
    } 
} 

控制器示例

<?php 

/** 
*----------------------------------------------------------------- 
* 
* LOGIN CONTROLLER 
* 
*/ 
namespace Controller\www; 
use \Helper\Controller; 

class Login extends Controller { 
    public $dependencies = ['arena', 'login', 'notification', 'site', 'userservice']; 

    /** 
    * Login 
    * 
    * Login Runs Through Various Checks Including If User is Banned, Account is Locked, 
    * or Forgot Password Request Is Active. Then the Entered Password is Matched & if Valid 
    * User is Logged In 
    */ 
    public function index() { 

     // User Already Logged In Redirect 
     $this->user->get('id') ? $this->redirect->home() : '';                 


     /** 
     * User Login 
     * 
     * If  Successful, Login User, Redirect Home 
     * Else Set Error Alerts 
     */ 
     if ($this->form->post('login')) { 

      // Define and Sanitize Post Data 
      $input = $this->input->get(['username', 'password']); 

      // Execute Login Service Layer - Define Flash Alerts 
      $login = $this->userservice->execute('login', $input); 
      $this->alerts($this->userservice->get('alerts')); 

      // Redirect Home on Successful Login 
      $login === true ? $this->redirect->home() : ''; 
     } 
    } 
} 

ServiceAccess類中的execute方法是什麼,我被告知我是否添加了我的用戶錯誤處理其餘部分。我的問題如下

爲什麼這比直接在應用程序中調用服務更好? 它簡化了我的控制器中的警報服務/設置的執行(在控制器內將〜15行代碼轉換爲4行),但我有像user/transactions(處理用戶帳戶的信用卡/借記卡)服務,並且它們有單獨的方法需要使用。所以我想知道是否是UserService類或我的事務類需要更新。我正在考慮在事務中定義一個執行方法,只是在輸入中傳遞一個密鑰來定義正在使用的事務的類型。

這是在我的應用程序中訪問/實現服務的最佳路線嗎?

回答

0

抽象的目的是管理底層複雜性。如果系統的整體複雜性由於附加層而降低,請執行此操作。如果沒有,請重新考慮。架構中的每一層都應該有一個目標。這個目的必須傳達(記錄)給開發者,否則每個人都會在黑暗中刺探。

規劃架構的好方法是繪製架構圖併爲每個層分配角色。每個圖層可以是單個或一組類。如果您很難爲特定圖層找到一個好的,乾淨的角色,那麼您可能不需要該圖層。

我知道您在架構中使用了MVC模式,但這並不構成整個架構,只是一種廣泛的方法。想想你的服務和各個層次的消耗和執行。考慮你想在你的架構中使用的層次UserService類在你的架構中會有意義嗎?它屬於哪個層?該層的作用是什麼?有時候你想要一個額外的抽象層來減少重複的代碼 - 就像你在案例中似乎已經實現的一樣;有時你只是想以簡單,直觀的方式來做,而不是過於複雜的事情。

每個架構層,每個模式,每個抽象都可以很好地應用或嚴重濫用。添加這樣的圖層是否會讓您或團隊中的其他開發人員感到困惑?如果是這樣,拿出來。每一層(抽象)的應用應該是優雅而直觀的 - 它應該讓團隊中的其他人都去「哦,這很聰明。「但是,如果最終出現開發人員無法確定如何擴展/維護代碼的情況,那麼這種情況會適得其反。

如果您不瞭解整個應用程序的意圖以及開發人員的背景(您),所以不可能做出一個好的選擇或給出好的建議,所以雖然我明白你要求一個最佳實踐 - 實質上,「我們應該在我們的架構/框架中應用多少抽象?」,實際上並不存在正確的答案,這取決於太多的東西 - 開發人員的偏好,甚至包括編碼風格,有些人甚至會說MVC在服務器端是不對的。直接回答「是」或「否」,我認爲任何人都不可以爲一個架構框架:留DRY,但避免over-engineering

+0

謝謝你。我正在用「正確」的方式來完成任務。我幾個月前從意大利麪條程序代碼轉換到oop mvc,所以我仍然不確定我對特定問題的處理方法是否是正確的方式/方法。謝謝。 – ICJ

相關問題