VilnaCRM-Org / user-service

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

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

Open paaneko opened 4 months ago

paaneko commented 4 months 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 4 months 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 4 months ago

Indeed, good approach. Didn't even think