⬆️ ⬇️

Introduction to the design of entities, the problem of creating objects

When modeling such a concept of a subject-oriented design as an entity, some difficulties may arise due to business requirements or the technical part. In particular, sometimes there is a difficulty with the creation of an entity object.



This article describes two such problems, and discusses how to solve them. The same article is suitable as an introduction to the design of entities. To understand the material will need a basic understanding of the subject-oriented design.



So, we studied the subject area, formed a single language, identified limited contexts and decided on the requirements [2]. All this is beyond the scope of this article, here we will try to solve specific narrow problems:



  1. Creating and maintaining the consistency of complex entity objects.
  2. Creation of entity objects with the generation of the identifier on the auto-increment field of the database.


Introduction



We have a client that must be modeled as an entity (Entity) [2]. From the point of view of business, each client must have:

')



And there can also be all sorts of additional information.



In the simple case, the class that implements the entity being modeled may look like this.



namespace Domain;

final class Client
{
    public function getId(): int;

    public function setId($id): void;

    public function setCorporateForm($corporateForm): void;

    public function setName($name): void;

    public function setGeneralManager(Manager $manager): void;

    public function setCountry($country): void;

    public function setCity($city): void;

    public function setStreet($street): void;

    public function setSubway($subway): void;
}


, , .



, -. , , -, .



$client = new Client();
//            
//      ,   , ..    
$client->getId();
//     ()   ,      
$repository->save($client);


-.



. , [4]. , , . .



namespace Domain;

final class Client
{
    public function __construct(
        $id,
        $corporateForm,
        $name,
        $generalManager,
        $country,
        $city,
        $street,
        $subway = null
    );

    public function getId(): int;
}


, . 8 , . , , , , , .



? — - (Value Object) [3].



namespace Domain;

final class Address
{  
    public function __construct($country, $city, $street, $subway = null);
}


namespace Domain;

final class Client
{
    public function __construct(
        int $id,
        string $name,
        Enum $corporateForm,
        Manager $generalManager,
        Address $address
    );

    public function getId(): int;
}


, , , . — (Builder) [5].



namespace Application;

interface ClientBuilder
{
    public function buildClient(): Client;

    public function setId($id): ClientBuilder;

    public function setCorporateForm($corporateForm): ClientBuilder;

    public function setName($name): ClientBuilder;

    public function setGeneralManager(Manager $generalManager): ClientBuilder;

    public function setAddress(Address $address): ClientBuilder;
}


$client = $builder->setId($id)
    ->setName($name)
    ->setGeneralManagerId($generalManager)
    ->setCorporateForm($corporateForm)
    ->setAddress($address)
    ->buildClient();


, , .



- .



, .. . , , . , . [1].



, , - , .



C, , UUID . , .



, .



, .



namespace Infrastructure;

final class MySqlClientBuilder implements ClientBuilder
{
    private $connection;

    public function __construct(Connection $connection);

    public function buildClient()
    {
        $this->connection
            ->insert('clients_table', [
                $this->name,
                $this->corporateForm,
                $this->generalManager->getId(),
                $this->address
            ]);
        
        $id = $this->connection->lastInsertId();
        
        return new Client(
            $id,
            $this->name,
            $this->corporateForm,
            $this->generalManager,
            $this->address
        );
    }
}


, . .



$builder = $container->get(ClientBuilder::class);

$client = $builder->setId($id)
    ->setName($name)
    ->setGeneralManagerId($generalManager)
    ->setCorporateForm($corporateForm)
    ->setAddress($address)
    ->buildClient();

$repository->save($client);

$client->getId();


!



P.S.:



, « , ». , , , .



— , DDD .



.




:



  1. ., «- (DDD). .»
  2. ., « - .»
  3. . , Value Object
  4. . , Constructor Initialization
  5. . , . , . , . , « - . .»

Source: https://habr.com/ru/post/321340/



All Articles