VilnaCRM-Org / user-service

Creative Commons Zero v1.0 Universal
5 stars 1 forks source link

Proposal: remove em `flush` methods from repositories and call them manually in handlers #42

Open paaneko opened 1 month ago

paaneko commented 1 month ago

Example

DeleteUserHandler

public function __invoke(DeleteUserCommand $command): void
    {
        //...

        $this->userRepository->delete($user);
        // Call the Flush method manually
        $this->em->flush();
        // Or
        // We can introduce Flusher that is passed in constructor
        $this->flusher->flush();

        $this->eventBus->publish(...$events);
    }

UserRepository

public function delete(object $user): void
    {
        $this->entityManager->remove($user);
    }

This change should improve experience with the doctrine Unit of Work, when we want to persist multiple entities at the same time.

Kravalg commented 1 month ago

We can add a new method to the repository for removing multiple entities simultaneously. For instance, we can introduce a removeBatch method that accepts an array or collection of users

I believe it's best to encapsulate data manipulation logic within the persistence layer and repository classes. This approach helps us adhere to the SOLID principles

paaneko commented 1 month ago

Indeed, good approach. Didn't even think