Arky9782/chat

Isolate flush as small service.

Closed this issue · 3 comments

fesor commented

EntityManager has fat interface. Try to not rely on it and wrap portion of needed functionality in your specialized interface. For example you could add service:

final class Flush
{
    // todo
    public function __invoke() {
        $this->em->flush();
    }
}

and use it in controller as:

public function someAction(Flush $flush) {
    // do stuff
    $flush();
    // send response.
}

What would you recommend in case you need to do some merge logic with $em->remove(). Isolate it in kind of *Repository, add final class Remove service or something else?

fesor commented

remove is your repository responsibility. But in practice, you rarely need just remove (you may need archive for example) but this is other topic.

fesor commented

https://github.com/Arky9782/chat/blob/master/src/AppBundle/Services/Flush.php#L29

Persist should not be there. It's repository responsibility.

Persist - marks entity as managed. Flush - makes diff off all managed entities's state and generate SQL queries as single transaction. Please read about unit of work.