K
K
Kolya Vantukh2021-10-20 15:54:44
PHP
Kolya Vantukh, 2021-10-20 15:54:44

Is it possible to use services in rich models?

The main question is, can I use classes from the service layer through DI in rich models, while changing the state of the model only inside the object? If not, and you still need to write absolutely all the logic in the model object, then it will become just a god object with 100500 lines, and this is not good, or ideally, the service layer with rich models should be minimal (for example, only for sending email, etc. .) and not affect the domain logic? Thanks in advance for your replies

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maksim Fedorov, 2021-10-20
@vkolya

a? If not, and you still need to write absolutely all the logic in the model object, then it will simply become a god object with 100500 lines

Because you are doing the opposite!
Write your UseCase according to the business process, this is some handler for example
Example
final class HandleCheckOutShoppingCart
{
    public function __construct(Carts $carts, PaymentGateway $gateway)
    {
        $this->carts   = $carts;
        $this->gateway = $gateway;
    }

    public function __invoke(CheckOutShoppingCart $command) : void
    {
        $shoppingCart = $this->carts->get($command->shoppingCart());

        $payment = $this->gateway->captureCharge($command->charge());

        $shoppingCart->checkOut($payment);
    }
}

Then the boundaries of the entity appear in you, and in the entities there are already invariants that this entity controls. no god object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question