Symfony项目中的获取器/设置器和封装问题

最近,我一直在与Symfony合作(一年多一点),在所有我有机会从事的项目中-实体总是以这样的方式创建的:它们仅包含私有字段,并且只包含裸露的设置者/获取者。

本文将讨论并举例说明这种方法为何具有危险性,即:它违反了我们以前的良好封装,会引发编写带有错误的代码并增加系统的复杂性。
本文将省略各个构建器中的setter主题,以及通过setter进行依赖项注入的主题(我只会说我们不同意)。 关于DDD,Rich Model之类的复杂主题,关于耦合/内聚和其他巧妙的词,将一无所获-仅讨论封装。 欢迎来到猫。

转到代码。 让我们忽略一下第二种类型提示和类型返回指示,并思考一下在处理对象时,以下代码与实用主义的观点有何不同:

$userName = $user->name;
$user->name = $newUserName;
:
$userName = $user->getName();
$user->setName($newUserName);

, , .

C . — , .


? , , . , , , .

«» , :

class Order
{
     private const STATUS_OPEN = 'open';
     private const STATUS_DELIVERED = 'delivered';

     private ProductCollection $products;
     private string $deliveryStatus;
     private ?DateTime $deliveryDate = null;

     public function deliver()
     {
          if ($this->isDelivered()) {
               throw new OrderDomainException('Order already delivered.');
          }

          $this->deliveryDate = new DateTime();
          $this->deliveryStatus = self::STATUS_DELIVERED;
     }

     private function isDelivered(): bool
     {
         return $this->deliveryStatus === self::STATUS_DELIVERED;
     }

     //  
}

« » - ? ? — ? . — , , . (cohesion).

:

class Order
{
    private const STATUS_OPEN = 'open';
    private const STATUS_DELIVERED = 'delivered';

    private ProductCollection $products;
    private string $deliveryStatus;
    private ?DateTime $deliveryDate = null;

    public function getDeliveryStatus(): string
    {
        return $this->deliveryStatus;
    }

    public function setDeliveryStatus(string $deliveryStatus): void
    {
        $this->deliveryStatus = $deliveryStatus;
    }

    public function getDeliveryDate(): ?DateTime
    {
        return $this->deliveryDate;
    }

    public function setDeliveryDate(?DateTime $deliveryDate): void
    {
        $this->deliveryDate = $deliveryDate;
    }

    public function getProducts(): ProductCollection
    {
        return $this->products;
    }

    public function setProducts(ProductCollection $products): void
    {
        $this->products = $products;
    }
}

? ( ). — , , , , ? , ? . — , — . . . , — ?


, . — , .

— , , , , , .

. :

/**
 * @ORM\Entity
 */
class Project
{
    /**
     * @var Id
     * @ORM\GeneratedValue()
     * @ORM\Id
     */
    private $id;
    /**
     * @var string
     * @ORM\Column(type="string")
     */
    private $name;
    /**
     * @var string
     * @ORM\Column(type="string", nullable=false)
     */
    private $status;
    /**
     * @var int
     * @ORM\Column(type="integer", nullable=true)
     */
    private $sort;
    /**
     * @var User
     * @ORM\Column(type="user_type", nullable=false)
     */
    private $user;
    /**
     * @var Department
     * @ORM\OneToMany(targetEntity="Department")
     */
    private $department;
    /**
     * @var string
     * @ORM\Column(type="string", nullable=true)
     */
    private $membership;
    
    public function getId(): Id
    {
        return $this->id;
    }
    
    public function getName(): string
    {
        return $this->name;
    }
    
    public function setName(string $name): Project
    {
        $this->name = $name;
        return $this;
    }
    
    public function getStatus(): string
    {
        return $this->status;
    }
    
    public function setStatus(string $status): Project
    {
        $this->status = $status;
        return $this;
    }
    
    public function getSort(): int
    {
        return $this->sort;
    }
    
    public function setSort(int $sort): Project
    {
        $this->sort = $sort;
        return $this;
    }
    
    public function getUser(): User
    {
        return $this->user;
    }
    
    public function setUser(User $user): Project
    {
        $this->user = $user;
        return $this;
    }
    
    public function getDepartment(): Department
    {
        return $this->department;
    }
    
    public function setDepartment(Department $department): Project
    {
        $this->department = $department;
        return $this;
    }
    
    public function getMembership(): string
    {
        return $this->membership;
    }
    
    public function setMembership(string $membership): Project
    {
        $this->membership = $membership;
        return $this;
    }
}

not nullable — ( false). , — not nullable , ? :)
, — ( ). .

, Doctrine ( ) , Reflection API. Marco Pivetta , : ocramius.imtqy.com/blog/doctrine-orm-optimization-hydration


. .
, , ( DTO, ..).

, , - «» . , , .

-. ID, — :

$order = $this->orderRepository->find($orderId);
if ($order === null) {
     throw new OrderException('Order not found.');
}

if ($order->getStatus() === Order::STATUS_ACTIVE 
    && $order->getDeliveryDate() <= $date
) {
    $this->orderDeliveryService->handle($order);
}

— , :

$order = $this->orderRepository->find($orderId);
$cityFreeLimit = $this->cityOrderService->getCityFreeLimit($cityName);

if ($order->getCity() === $cityName 
    && $order->getTotalPrice() > $cityFreeLimit
) {
     $delivery = 0;
     $this->orderDeliveryService->deliveryOrder($order, $delivery);
     
     return;
}

//         

, - .

, - . , , , . , , , . «» «».


/ , , , , « » , — , , .

— Symfony (DAO), /. — - ( , ).

, — :)

. — .

Source: https://habr.com/ru/post/zh-CN469323/


All Articles