Getters / setters y problema de encapsulación en proyectos de Symfony

Más recientemente, he estado trabajando con Symfony (poco más de un año) y en todos los proyectos en los que tuve la oportunidad de trabajar: las entidades siempre se crearon de tal manera que contenían solo campos privados y setters / getters para ellos.

Habrá argumentos y ejemplos en los artículos de por qué este enfoque es peligroso, a saber: viola nuestra buena encapsulación, provoca escribir código con errores y aumenta la complejidad del sistema.
El artículo omitirá el tema de los creadores en varios constructores y el tema de la inyección de dependencia a través de los creadores (solo diré que no lo aprobamos). No habrá nada sobre temas complejos como DDD, Rich Model, sobre acoplamiento / cohesión y otras palabras inteligentes, solo hable sobre encapsulación. Bienvenido a cat.

Ve al código. Omitamos por un segundo la indicación de tipo y la indicación de retorno de tipo y pensemos cómo, cuando se trabaja con un objeto, el siguiente código difiere desde un punto de vista utilitario:

$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/469323/


All Articles