O
O
Oleg Tsvetkov2018-02-03 17:24:04
PHP
Oleg Tsvetkov, 2018-02-03 17:24:04

How should work with entity be organized?

Good day.
There is a PHP project with Yii2 and ~DDD. All data is provided to users in two languages: Russian and English.
Within the project, there is an entity Disease (for example) , tied to a specific user.

class Disease 
{
  /**
   * @var string
   */
  public $id;

  /**
   * @var int
   */
  public $userId;

  /**
   * @var string
   */
  public $icd10Code;

  /**
   * @var string
   */
  public $userBasedValue;
}

(ICD-10 - classifier of diseases of the World Health Organization)
This entity does not contain the name of the disease, because it will be different in different languages. Now the name (like all other localized data, a series of data is also tied to the user who performs the request ) is pulled up at the stage of generating the response (in the Fractal transformer , the presentation layer). But now there is a need to add a new data source that requires passing the language and user data required to form the response as a query parameter and, accordingly, returns fully localized data on the disease, taking into account the transferred user data.
The data from these two sources is combined and displayed in one list.
How to solve this problem correctly?There is no particular desire to use instanceof in the transformer...
So far, only this option comes to mind:
Make a LocalizedDisease entity (child of Disease with additional fields $title and $userBasedValueDescription ) for a new repository, then wrap getting the full list of diseases in some DiseaseService::getList () , which will pull both repositories and wrap each received object ( Disease or LocalizedDisease ) in an object of the corresponding class implementing the interface of the form:
interface DiseasePresenterInterface
{
  public getICD10Code(): string;

  public getTitle(): string;

  public getUserBasedValueDescription(): string;
}

Which already determines where to get data on the disease for a particular user. As a result, it turns out that the DiseasePresenter (for the old Disease class ) will pull the special. the disease repository (which is now pulled by the Fractal transformer) by Disease::$id , and the LocalizedDiseasePresenter (for LocalizedDisease ) will simply use the value of the LocalizedDisease::$title and LocalizedDisease::$userBasedDescription variables .
But something tells me that this is not the right way to solve this problem ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kn0ckn0ck, 2018-02-03
@kn0ckn0ck

I wouldn't do that. We need to think better about the problem area. In this example, the names are incorrectly used. By definition, the book is already localized. The translators and editors of a Russian-language book are not at all those who wrote and edited the original. Here it is clearly necessary to single out the original object of copyright (original edition) and derivatives - editions and translated editions. Each edition has its own name and other characteristics.
In this case, there is no question of localization. Here is the question of a more correct analysis and modeling of the subject area.
By the way, localization through add. entities in general somehow strange to solve. This is purely a presentational layer.

D
ddd329, 2018-03-04
@ddd329

Entities and aggregates are needed to implement business logic so that you cannot write unreliable and / or conflicting information to the database, and that's it!
On the screen, you display not entities and aggregates, but rather information about them ... When you think about displaying data on the screen, you should not even think in aggregates, because aggregates are units of change, not a portion for display.
Therefore, you can contact the database directly with a request to give you information, or create a separate Reading Model, and read the data through the ORM.
Create a LocalizedDisease entityIt doesn't make sense toIn general, the essence is ideally only behavior, and it doesn’t matter what data it has, that is, the principle here is “Speak and don’t ask”

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question