2017-08-13 19 views
0

我的公司对象需要程序员搞安全关系,所以我们要求用户名绑定到每个对象。我们仍然想重构和清理遗留代码,并且有一个围绕这个的结构。我们要去的说法被一些人认为,像物体上的动作的CRUD应以身作则,在用户在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); 

是50层的方法或因此引发的用户对象多还是应该的事不欢而散每基础和被称为传入用户名或不可变的用户对象?下面

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

go with entity&datamapper pattern。请参阅:https://stackoverflow.com/questions/11942842/who-should-handle-the-conditions-in-complex-queries-the-data-mapper-or-the-serv/11943107#11943107 – jeremy

+0

什么[域驱动设计]与此有关? – 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的宇航员可能会告诉你去研究像Mediator模式这样的设计模式,这种模式肯定能够很好地工作,但是在复杂性和易用性之间总是存在折衷。对于大多数CRUD重度应用,我发现这种基于服务的方法是最佳选择。

相关问题