2017-08-13 9 views
0

私のcompanysオブジェクトはプログラマーが関係を揺るがすことからセキュリティを必要とするので、ユーザー名を各オブジェクトに関連付ける必要があります。従来のコードをリファクタリングしてクリーンアップし、その周りに構造を持たせたいと考えています。私たちはいくつかののWebアプリケーションでは、巨大なユーザオブジェクトにアクションまたはアクションクラスが必要ですか?

$user = new User('usernameExample'); 
$profileData = $user->getProfile(12313); 
echo json_encode($profileData); 
$ID = $user->createProfile($_POST); 
$user->updateProfile($_POST); 
$user->refreshProfile(12321394); 
$user->deleteProfile(234242); 

$user = new User('usernameExample'); 
$batches = $user->getGroupList(); 
$user->updateGroup($_POST); 
$user->deleteGroup(23242); 
$newBatchID = $user->createGroup(); 

$user = new User('usernameExample'); 
$user->addSubuser($_POST); 
$user->deleteSubuser('usernameExample'); 
$user->updateSubuser($_POST); 
$user->getSubusers(); 

$user new User('usernameExample'); 
$user->updateSetting($_POST); 

例では、オブジェクトに対するアクションのようなものをCRUDは、ユーザによって呼び出されるべきであると信じている引数を行く上で非常ににユーザーオブジェクトのための50個の方法やそこらで投げているか、基礎ごとに、物事解散しなければならないとユーザー名または不変のユーザーオブジェクトを渡しますか?あなたのビジネスオブジェクトへの機能の多くをスタッフィング

$userProfiles = new UserProfile('usernameExample'); 
$profileData = $userProfile->getProfile(12313); 
+0

エンティティとデータパターンを使用します。参照:https://stackoverflow.com/questions/11942842/who-should-handle-the-conditions-in-complex-queries-the-data-mapper-or-the-serv/11943107#11943107 – jeremy

+0

[ドメイン-driven-design]はこれと関係がありますか? – jeremy

+0

私はddd経験のある人が彼らのアプローチを与えることを望んでいました。 – prettyHomePages

答えて

1

以下の例では、複雑な関係とロジックを扱っている場合は特に、高速な醜い得ることができ、他のオブジェクトとの相互作用を処理します。

この種のアーキテクチャを超える最初のステップは、通常、オブジェクト間のやりとりを容易にするサービスクラスを実装することです。

は、次の点を考慮

<?php 
/** 
* Class UserBasedServiceAbstract 
* Base class for our user based services. All services must be instantiated 
* with a valid user 
*/ 
abstract class UserBasedServiceAbstract 
{ 
    protected $user; 

    /** 
    * UserBasedServiceAbstract constructor. 
    * @param User $user 
    * @throws Exception 
    */ 
    public function __construct(User $user) 
    { 
     if($user->isNew()) 
     { 
      throw new Exception('User must be persisted before doing anything useful with it'); 
     } 

     $this->user = $user; 
    } 

    /** 
    * @param $message 
    */ 
    protected function logAction($message) 
    { 
     $formattedMessage = (is_array($message)) ? json_encode($message):$message; 
     echo 'User action for '.$this->user->getUsername().': '.$formattedMessage; 
    } 
} 

class GroupService extends UserBasedServiceAbstract 
{ 
    /** 
    * Get a list of groups that the current user belongs to 
    * @return array 
    */ 
    public function getGroupList() 
    { 
     // We always have a reference to our user 
     $userId = $this->user->getId(); 

     $this->logAction('Getting group list'); 

     //Fetch groups for user 
     $groupList = []; 

     return $groupList; 
    } 

    /** 
    * Update the specified group if the current user has permission to do so 
    * @param Group $group 
    * @param array $params 
    * @throws Exception 
    */ 
    public function updateGroup(Group $group, array $params) 
    { 
     if(!$this->_userCanUpdateGroup()) 
     { 
      throw new Exception('User does not have permission to update this group'); 
     } 

     $this->logAction('Updating group'); 

     //update group 
    } 

    /** 
    * Delete the specified group if the current user has permission to do so 
    * @param Group $group 
    * @throws Exception 
    */ 
    public function deleteGroup(Group $group) 
    { 
     if(!$this->_userCanDeleteGroup($group)) 
     { 
      throw new Exception('User does not have permission to delete this group'); 
     } 

     $this->logAction('Deleting group'); 

     //delete group 
    } 

    /** 
    * Determine whether or not the current user can delete the specified group 
    * @param Group $group 
    * @return bool 
    * @throws Exception 
    */ 
    private function _userCanDeleteGroup(Group $group) 
    { 
     //Maybe there is some logic we need to check on the group before we go further 
     if(!$group->isDeletable()) 
     { 
      throw new Exception('This group cannot be deleted'); 
     } 

     // Implement some user-specific logic 
     return ($this->user->hasPermission('group_admin') && $this->user->getKarma()>100); 
    } 

    /** 
    * Determine whether or not the current user can update the specified group 
    * @return bool 
    */ 
    private function _userCanUpdateGroup() 
    { 
     // Implement some user-specific logic 
     return ($this->user->hasPermission('group_moderator') && $this->user->getKarma()>50); 
    } 
} 

あなたが必要とする一般的な機能を持つ抽象クラスを作成し、確認し、ユーザーへの参照を保持します。ユーザーインスタンスに基づいている必要があるすべてのサービスは、このクラスを拡張し、ユーザーオブジェクトと関連オブジェクト間のやりとりを容易にします。パーミッションに関するあなたのロジックはすべて、これらのサービスクラスに入ります。これは、すべてをビジネスオブジェクトに詰め込むよりもずっとメンテナンスが容易です。

このアプローチはあなたを遠ざけることができます。 OOの宇宙飛行士は、このような種類のメディエーターパターンのようなデザインパターンを見てもらうように指示するかもしれませんが、それは間違いなく機能しますが、複雑さと使いやすさの間には常にトレードオフがあります。ほとんどのCRUD重いアプリケーションでは、私はこのサービスに基づくアプローチがスイートスポットであると感じています。

関連する問題