DDD: Whether the service is injected into the entity

I have a Zone object tree:

class Zone {
protected $parent;

public function __construct(Zone $parent) {
$this->parent = $parent;
}
}

There are no children or descendants in the zone Attribute, because I want to avoid the pain of managing these relationships in the domain model.

Instead, the domain service maintains a closure table in the database to map the region to all its descendants at any level.

p>

Now, I have a user who can assign one or more zones:

class User {
protected $zones;
< br /> public function assignZone(Zone $zone) {
$this->zones[] = $zone;
}
}

My question is, in Before assigning a new area to a user, I want to explicitly or implicitly check whether the area has been assigned through one of its descendants.

Therefore, I want my controller to temporarily inject the service into this method to Perform the necessary checks:

class User {
protected $zones;

public function assignZone(Zone $newZone, ZoneService $zoneService) {
foreach ($this->zones as $zone) {
if ($service->zoneHasDescendant($zone, $newZone)) {
throw new Exception('The user is already assigned this zone');
}
}

$this->zones[] = $zone;
}
}

Is this a good practice, or if not, what is the correct choice?

There are no children nor descendants property in the Zone, because I< br> want to avoid the pain of managing these relationships in the domain
model.

Instead, a domain service maintains a closure table in the database strong>,
to map a zone to all its descendants, at any level.

I added some important points because it seems a bit contradictory. You don’t want the “pain” in the domain “, but you manage the shutdown table in the domain service. The fact that you need to inject the service into the entity sometimes indicates that the design can be improved.

It looks like you have a regional hierarchy. This seems to be important for your domain name Component. The area has a parent and offspring, so maybe you should model it accordingly. The pain of managing this relationship is a “reasonable” pain because you do it for model expression. In this case, Domain-driven design. The zone itself will have the following content:

zone->hasDescendant($newZone)

And you don’t need to inject services. In fact, You don’t need the service at all. Because the only reason for this service is to maintain the closure table. This is not a domain concern, but a persistence issue.

If you still need the service for some reason, it’s better to change It is injected into the Zone class. This can solve the problem closer to its source.

I have a Zone object tree:

class Zone {
protected $parent;

public function __construct(Zone $parent) {
$this->parent = $parent ;
})

There are no children or descendant attributes in the area, because I want to avoid the pain of managing these relationships in the domain model.

Instead, the domain service maintains one in the database Closure table to map a region to all its descendants at any level.

Now, I have a user who can assign one or more regions:

< pre>class User {
protected $zones;

public function assignZone(Zone $zone) {
$this->zones[] = $zone;
}
}

My problem is that before assigning a new area to a user, I want to explicitly or implicitly check whether the area has been allocated by one of its descendants.

< p>Therefore, I want my controller to temporarily inject services into this method to perform the necessary checks:

class User {
protected $zones;

public function assignZone(Zone $newZone, ZoneService $zoneService) {
foreach ($this->zones as $zone) {
if ($service->zoneHasDescendant($zone, $newZone)) {
throw new Exception('The user is already assigned this zone');
}
}

$this->zones[] = $zone;
}
}

Is this a good practice, or if not, what is the correct choice?

There are no children nor descendants property in the Zone, because I
want to avoid the pain of managing these relationships in the domain
model.

Instead, a domain service maintains a closure table in the database,
to map a zone to all its descendants, at any level.

I added some important points because it seems a bit contradictory. You don’t want “pain” in the domain, but you manage the shutdown table in the domain service. The fact that you need to inject services into entities sometimes indicates that the design can be improved.

It seems that you have a zone hierarchy. This seems to be an important part of your domain name. Zones have parents and descendants, so maybe You should model it accordingly. The pain of managing this relationship is a “reasonable” pain because you do it for model expression. In this case, domain-driven design. The area itself will have the following: /p>

zone->hasDescendant($newZone)

And you don’t need to inject the service. In fact, you don’t need the service at all. Because the only thing about this service is The reason is to maintain the closure table. This is not a domain concern, but a persistence issue.

If for some reason you still need a service, it is best to inject it into the Zone class. This will allow you to update Solve the problem close to its source.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2286 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.